-
Notifications
You must be signed in to change notification settings - Fork 5
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
Open
ghaith100994
wants to merge
5
commits into
Black-Cockpit:master
Choose a base branch
from
ghaith100994:feature/add-organizations-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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
128 changes: 128 additions & 0 deletions
128
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,128 @@ | ||
| using NETCore.Keycloak.Client.Exceptions; | ||
| using NETCore.Keycloak.Client.Models; | ||
| using NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| namespace NETCore.Keycloak.Client.HttpClients.Abstraction; | ||
|
|
||
| /// <summary> | ||
| /// Keycloak organizations REST client. | ||
| /// <see href="https://www.keycloak.org/docs-api/latest/rest-api/index.html#_organizations"/> | ||
| /// </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 of the operation. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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 of the operation. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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 of the operation. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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{T}"/> containing the <see cref="KcOrganization"/> details. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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 <see cref="KcOrganization"/> objects. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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{T}"/> with the count of organizations. | ||
| /// </returns> | ||
| /// <exception cref="KcException">Thrown if any required parameter is null, empty, or invalid.</exception> | ||
| 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
141 changes: 141 additions & 0 deletions
141
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,141 @@ | ||
| 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; | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations"/> | ||
| 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. | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.CreateAsync"/> | ||
| 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); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.UpdateAsync"/> | ||
| 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); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.DeleteAsync"/> | ||
| 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); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.GetAsync"/> | ||
| 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); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.ListAsync"/> | ||
| 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); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IKcOrganizations.CountAsync"/> | ||
| 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
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NETCore.Keycloak.Client.Models.Organizations; | ||
|
|
||
| /// <summary> | ||
| /// Represents an organization resource in Keycloak. | ||
| /// <see href="https://www.keycloak.org/docs-api/latest/rest-api/index.html#OrganizationRepresentation"/> | ||
| /// </summary> | ||
| public sealed class KcOrganization | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the organization id. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A string representing the unique identifier of the organization. | ||
| /// </value> | ||
| [JsonPropertyName("id")] | ||
| public string Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization name. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A string representing the display name of the organization. | ||
| /// </value> | ||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization alias. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A string representing an alternate identifier or alias for the organization. | ||
| /// </value> | ||
| [JsonPropertyName("alias")] | ||
| public string Alias { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the organization is enabled. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A nullable boolean that is true when the organization is enabled, false when disabled, | ||
| /// or null if the enabled state is not set. | ||
| /// </value> | ||
| [JsonPropertyName("enabled")] | ||
| public bool? Enabled { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization description. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A string containing a human-readable description for the organization. | ||
| /// </value> | ||
| [JsonPropertyName("description")] | ||
| public string Description { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the redirect URL for the organization. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A string representing the redirect URL associated with the organization (for example, after login or registration flows). | ||
| /// </value> | ||
| [JsonPropertyName("redirectUrl")] | ||
| public string RedirectUrl { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Custom attributes associated with the organization. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A dictionary where the key is the attribute name and the value is a list of values for that attribute (Keycloak style). | ||
| /// </value> | ||
| [JsonPropertyName("attributes")] | ||
| public Dictionary<string, List<string>> Attributes { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the organization domains. | ||
| /// </summary> | ||
| /// <value> | ||
| /// A collection of <see cref="KcOrganizationDomain"/> representing domains associated with the organization. | ||
| /// </value> | ||
| [JsonPropertyName("domains")] | ||
| public ICollection<KcOrganizationDomain> Domains { get; set; } = []; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the documentation to ensure it is accurate, consistent with the existing style, and free of any ambiguities.