-
Notifications
You must be signed in to change notification settings - Fork 6
Add support for managing organizations in Keycloak #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
5 commits merged into
Black-Cockpit:master
from
ghaith100994:feature/add-organizations-client
Apr 1, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd4dc32
Add support for managing organizations in Keycloak
dfe0064
Refactor Keycloak models and enhance documentation
9c190dd
Improve Keycloak client documentation and consistency
c4cb15f
Refactor BuildQuery in KcOrganizationFilter
c2cbfa4
Refactor Domains property in KcOrganization class
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
NETCore.Keycloak.Client/HttpClients/Abstraction/IKcOrganizations.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| using NETCore.Keycloak.Client.Models; | ||
| using NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| namespace NETCore.Keycloak.Client.HttpClients.Abstraction; | ||
|
|
||
| /// <summary> | ||
| /// Keycloak organizations REST client | ||
| /// </summary> | ||
| public interface IKcOrganizations | ||
| { | ||
| /// <summary> | ||
| /// Creates a new organization in a specified Keycloak realm. | ||
| /// | ||
| /// POST /{realm}/organizations | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm where the organization will be created.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="organization">The organization representation to create.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{T}"/> indicating the result.</returns> | ||
| Task<KcResponse<object>> CreateAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganization organization, | ||
| CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Updates an existing organization in a specified Keycloak realm. | ||
| /// | ||
| /// PUT /{realm}/organizations/{organizationId} | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm where the organization exists.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="organizationId">The ID of the organization to update.</param> | ||
| /// <param name="organization">The updated organization representation.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{T}"/> indicating the result.</returns> | ||
| Task<KcResponse<object>> UpdateAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| KcOrganization organization, | ||
| CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Deletes an organization from a specified Keycloak realm. | ||
| /// | ||
| /// DELETE /{realm}/organizations/{organizationId} | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm where the organization exists.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="organizationId">The ID of the organization to delete.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{T}"/> indicating the result.</returns> | ||
| Task<KcResponse<object>> DeleteAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves a specific organization by its ID from a specified Keycloak realm. | ||
| /// | ||
| /// GET /{realm}/organizations/{organizationId} | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm to query.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="organizationId">The ID of the organization to retrieve.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{OrganizationRepresentation}"/> with the organization details.</returns> | ||
| Task<KcResponse<KcOrganization>> GetAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves a list of organizations from a specified Keycloak realm, optionally filtered by criteria. | ||
| /// | ||
| /// GET /{realm}/organizations | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm from which organizations will be listed.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="filter">Optional filter criteria.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{T}"/> containing an enumerable of organizations.</returns> | ||
| Task<KcResponse<IEnumerable<KcOrganization>>> ListAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganizationFilter filter = null, | ||
| CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the count of organizations in a specified Keycloak realm, optionally filtered. | ||
| /// | ||
| /// GET /{realm}/organizations/count | ||
| /// </summary> | ||
| /// <param name="realm">The Keycloak realm to query.</param> | ||
| /// <param name="accessToken">The access token used for authentication.</param> | ||
| /// <param name="filter">Optional filter criteria.</param> | ||
| /// <param name="cancellationToken">Optional cancellation token.</param> | ||
| /// <returns>A <see cref="KcResponse{long}"/> with the count of organizations.</returns> | ||
| Task<KcResponse<long>> CountAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganizationFilter filter = null, | ||
| CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
NETCore.Keycloak.Client/HttpClients/Implementation/KcOrganizations.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using NETCore.Keycloak.Client.HttpClients.Abstraction; | ||
| using NETCore.Keycloak.Client.Models; | ||
| using NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| namespace NETCore.Keycloak.Client.HttpClients.Implementation; | ||
|
|
||
| /// <summary> | ||
| /// Organization service API for managing organizations in Keycloak. | ||
| /// </summary> | ||
| internal sealed class KcOrganizations(string baseUrl, ILogger logger) : KcHttpClientBase(logger, baseUrl), IKcOrganizations | ||
| { | ||
| // Primary constructor on the class declaration is used; no explicit ctor body required. | ||
|
|
||
| public Task<KcResponse<object>> CreateAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganization organization, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| ValidateNotNull(nameof(organization), organization); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations"; | ||
| return ProcessRequestAsync<object>( | ||
| url, | ||
| HttpMethod.Post, | ||
| accessToken, | ||
| "Unable to create organization", | ||
| organization, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task<KcResponse<object>> UpdateAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| KcOrganization organization, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| ValidateRequiredString(nameof(organizationId), organizationId); | ||
| ValidateNotNull(nameof(organization), organization); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations/{organizationId}"; | ||
| return ProcessRequestAsync<object>( | ||
| url, | ||
| HttpMethod.Put, | ||
| accessToken, | ||
| "Unable to update organization", | ||
| organization, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task<KcResponse<object>> DeleteAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| ValidateRequiredString(nameof(organizationId), organizationId); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations/{organizationId}"; | ||
| return ProcessRequestAsync<object>( | ||
| url, | ||
| HttpMethod.Delete, | ||
| accessToken, | ||
| "Unable to delete organization", | ||
| null, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task<KcResponse<KcOrganization>> GetAsync( | ||
| string realm, | ||
| string accessToken, | ||
| string organizationId, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| ValidateRequiredString(nameof(organizationId), organizationId); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations/{organizationId}"; | ||
| return ProcessRequestAsync<KcOrganization>( | ||
| url, | ||
| HttpMethod.Get, | ||
| accessToken, | ||
| "Unable to get organization", | ||
| null, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task<KcResponse<IEnumerable<KcOrganization>>> ListAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganizationFilter filter = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| filter ??= new KcOrganizationFilter(); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations{filter.BuildQuery()}"; | ||
| return ProcessRequestAsync<IEnumerable<KcOrganization>>( | ||
| url, | ||
| HttpMethod.Get, | ||
| accessToken, | ||
| "Unable to list organizations", | ||
| null, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task<KcResponse<long>> CountAsync( | ||
| string realm, | ||
| string accessToken, | ||
| KcOrganizationFilter filter = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| ValidateAccess(realm, accessToken); | ||
| filter ??= new KcOrganizationFilter(); | ||
|
|
||
| var url = $"{BaseUrl}/{realm}/organizations/count{filter.BuildQuery()}"; | ||
| return ProcessRequestAsync<long>( | ||
| url, | ||
| HttpMethod.Get, | ||
| accessToken, | ||
| "Unable to count organizations", | ||
| null, | ||
| "application/json", | ||
| cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
NETCore.Keycloak.Client/Models/Organizations/KcOrganization.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| /// <summary> | ||
| /// Represents an organization resource. | ||
| /// </summary> | ||
| public sealed class KcOrganization | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the organization id. | ||
| /// </summary> | ||
| [JsonPropertyName("id")] | ||
| public string Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization name. | ||
| /// </summary> | ||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization alias. | ||
| /// </summary> | ||
| [JsonPropertyName("alias")] | ||
| public string Alias { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the organization is enabled. | ||
| /// </summary> | ||
| [JsonPropertyName("enabled")] | ||
| public bool? Enabled { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization description. | ||
| /// </summary> | ||
| [JsonPropertyName("description")] | ||
| public string Description { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the redirect URL for the organization. | ||
| /// </summary> | ||
| [JsonPropertyName("redirectUrl")] | ||
| public string RedirectUrl { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Custom attributes. | ||
| /// Key = attribute name | ||
| /// Value = list of values (Keycloak style) | ||
| /// </summary> | ||
| [JsonPropertyName("attributes")] | ||
| public Dictionary<string, List<string>> Attributes { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization domains. | ||
| /// </summary> | ||
| [JsonPropertyName("domains")] | ||
| public List<KcOrganizationDomain> Domains { get; set; } = new(); | ||
| } |
21 changes: 21 additions & 0 deletions
21
NETCore.Keycloak.Client/Models/Organizations/KcOrganizationDomain.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| /// <summary> | ||
| /// Represents organization domain information. | ||
| /// </summary> | ||
| public sealed class KcOrganizationDomain | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the domain name. | ||
| /// </summary> | ||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the domain is verified. | ||
| /// </summary> | ||
| [JsonPropertyName("verified")] | ||
| public bool? Verified { get; set; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.