Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .fossa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 1

analysis:
# Paths to exclude from license/security analysis (relative to repo root)
ignorePaths:
- "NETCore.Keycloak.Client.Tests/**"

# Specific package locators to ignore (use exact locator shown by FOSSA)
ignorePackages:
- "pip+ansible$13.2.0"
- "pip+ansible-core$2.17.7"
- "nuget+Microsoft.NET.Test.Sdk$17.6.0"

# Notes:
# - Commit and push this file and then trigger a new FOSSA scan so the server-side
# analysis picks up the repo-level exclusions. Local changes alone won't modify
# the existing project findings until FOSSA re-analyzes the repository.
# - If your organization manages FOSSA centrally, you may need to update project
# settings in the FOSSA UI instead of relying on this file.
27 changes: 13 additions & 14 deletions NETCore.Keycloak.Client.Tests/NETCore.Keycloak.Client.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
Expand All @@ -15,23 +15,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Cake" Version="1.3.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4"/>
<PackageReference Include="MSTest.TestFramework" Version="3.0.4"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="PasswordGenerator" Version="2.1.0" />
<PackageReference Include="AutoFixture" Version="4.18.1" PrivateAssets="all" />
<PackageReference Include="Bogus" Version="35.6.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" PrivateAssets="all" />
<PackageReference Include="Moq" Version="4.20.72" PrivateAssets="all" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" PrivateAssets="all" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" PrivateAssets="all" />
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" />
<PackageReference Include="PasswordGenerator" Version="2.1.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NETCore.Keycloak.Client\NETCore.Keycloak.Client.csproj"/>
<ProjectReference Include="..\NETCore.Keycloak.Client\NETCore.Keycloak.Client.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="**/*.venv/**/*.cs"/>
<Compile Remove="**/*.venv/**/*.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions NETCore.Keycloak.Client.Tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
ansible==8.7.0
ansible-core==2.15.13
ansible>=13.2.0
ansible-core>=2.17.7
certifi==2024.12.14
cffi==1.17.1
charset-normalizer==3.4.1
cryptography==44.0.0
cryptography==44.0.1
deprecation==2.1.0
ecdsa==0.19.0
idna==3.10
importlib-resources==5.0.7
Jinja2==3.1.5
MarkupSafe==3.0.2
packaging==24.2
pyasn1==0.6.1
pyasn1==0.6.2
pycparser==2.22
python-jose==3.3.0
python-keycloak==3.3.0
python-jose==3.4.0
python-keycloak==7.0.2
PyYAML==6.0.2
requests==2.32.3
requests==2.32.4
requests-toolbelt==1.0.0
resolvelib==1.0.1
rsa==4.9
six==1.17.0
urllib3==2.3.0
urllib3==2.6.3
128 changes: 128 additions & 0 deletions NETCore.Keycloak.Client/HttpClients/Abstraction/IKcOrganizations.cs
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ public interface IKeycloakClient
/// See <see cref="IKcScopeMappings"/> for detailed operations.
/// </summary>
public IKcScopeMappings ScopeMappings { get; }

/// <summary>
/// Gets the organizations REST client for managing organizations.
/// </summary>
public IKcOrganizations Organizations { get; }
}
141 changes: 141 additions & 0 deletions NETCore.Keycloak.Client/HttpClients/Implementation/KcOrganizations.cs
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);
}
}
Loading