-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
117af72
commit e04de87
Showing
15 changed files
with
1,294 additions
and
267 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
58 changes: 58 additions & 0 deletions
58
src/Keycloak.AuthServices.Sdk/KeycloakHttpClientException.cs
This file contains 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,58 @@ | ||
namespace Keycloak.AuthServices.Sdk; | ||
|
||
using System.Globalization; | ||
|
||
/// <summary> | ||
/// Represents an exception that is thrown when an HTTP request to the Keycloak fails. | ||
/// </summary> | ||
public partial class KeycloakHttpClientException : Exception | ||
{ | ||
/// <summary> | ||
/// Gets the status code of the HTTP response. | ||
/// </summary> | ||
public int StatusCode { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the raw HTTP response. | ||
/// </summary> | ||
public string HttpResponse { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the deserialized error response from the Keycloak server. | ||
/// </summary> | ||
public ErrorResponse Response { get; private set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="KeycloakHttpClientException"/> class with the specified parameters. | ||
/// </summary> | ||
/// <param name="message">The error message.</param> | ||
/// <param name="statusCode">The status code of the HTTP response.</param> | ||
/// <param name="httpResponse">The raw HTTP response.</param> | ||
/// <param name="response">The deserialized error response from the Keycloak server.</param> | ||
/// <param name="innerException">The inner exception that caused this exception, or <c>null</c> if no inner exception is specified.</param> | ||
public KeycloakHttpClientException( | ||
string message, | ||
int statusCode, | ||
string httpResponse, | ||
ErrorResponse response, | ||
Exception? innerException | ||
) | ||
: base(message + "\n\nStatus: " + statusCode, innerException) | ||
{ | ||
this.StatusCode = statusCode; | ||
this.HttpResponse = httpResponse; | ||
this.Response = response; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a string that represents the current object, including the HTTP response. | ||
/// </summary> | ||
/// <returns>A string that represents the current object, including the HTTP response.</returns> | ||
public override string ToString() => | ||
string.Format( | ||
CultureInfo.InvariantCulture, | ||
"HTTP Response: \n\n{0}\n\n{1}", | ||
this.HttpResponse, | ||
base.ToString() | ||
); | ||
} |
This file contains 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
209 changes: 209 additions & 0 deletions
209
src/Keycloak.AuthServices.Sdk/Protection/IKeycloakPolicyClient.cs
This file contains 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,209 @@ | ||
namespace Keycloak.AuthServices.Sdk.Protection; | ||
|
||
using Keycloak.AuthServices.Sdk.Protection.Models; | ||
using Keycloak.AuthServices.Sdk.Protection.Requests; | ||
|
||
/// <summary> | ||
/// Must be used by the owner of the resource for whom the policy is being created. | ||
/// </summary> | ||
/// <remarks> | ||
/// https://www.keycloak.org/docs/latest/authorization_services/index.html#_service_authorization_uma_policy_api | ||
/// </remarks> | ||
public interface IKeycloakPolicyClient | ||
{ | ||
/// <summary> | ||
/// Gets all Policies | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#querying-permission | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="parameters"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The HttpResponseMessage of the request</returns> | ||
Task<HttpResponseMessage> GetPoliciesWithResponseAsync( | ||
string realm, | ||
GetPoliciesRequestParameters? parameters = default, | ||
CancellationToken cancellationToken = default | ||
); | ||
|
||
/// <summary> | ||
/// Gets all Policies | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#querying-permission | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="parameters"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>A list of policy representations <see cref="Policy"/></returns> | ||
async Task<IEnumerable<Policy>> GetPoliciesAsync( | ||
string realm, | ||
GetPoliciesRequestParameters? parameters = default, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
var response = await this.GetPoliciesWithResponseAsync( | ||
realm, | ||
parameters, | ||
cancellationToken | ||
); | ||
|
||
return await response.GetResponseAsync<IEnumerable<Policy>>(cancellationToken) | ||
?? Enumerable.Empty<Policy>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a Policy | ||
/// </summary> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The HttpResponseMessage of the request</returns> | ||
Task<HttpResponseMessage> GetPolicyWithResponseAsync( | ||
string realm, | ||
string policyId, | ||
CancellationToken cancellationToken = default | ||
); | ||
|
||
/// <summary> | ||
/// Get representation of a Policy | ||
/// </summary> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The policy representation <see cref="Policy"/></returns> | ||
async Task<Policy> GetPolicyAsync( | ||
string realm, | ||
string policyId, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
var response = await this.GetPolicyWithResponseAsync(realm, policyId, cancellationToken); | ||
|
||
return await response.GetResponseAsync<Policy>(cancellationToken) ?? new(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a policy | ||
/// </summary> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="resourceId">The resource ID to create the policy for.</param> | ||
/// <param name="policy">Policy representation</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The HttpResponseMessage of the request</returns> | ||
Task<HttpResponseMessage> CreatePolicyWithResponseAsync( | ||
string realm, | ||
string resourceId, | ||
Policy policy, | ||
CancellationToken cancellationToken | ||
); | ||
|
||
/// <summary> | ||
/// Creates a policy | ||
/// </summary> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="resourceId">The resource ID to create the policy for.</param> | ||
/// <param name="policy">Policy representation</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The created policy representation <see cref="Policy"/></returns> | ||
async Task CreatePolicyAsync( | ||
string realm, | ||
string resourceId, | ||
Policy policy, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
var response = await this.CreatePolicyWithResponseAsync( | ||
realm, | ||
resourceId, | ||
policy, | ||
cancellationToken | ||
); | ||
|
||
await response.EnsureResponseAsync(cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Updates a policy | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#managing-resource-permissions-using-the-policy-api | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID.</param> | ||
/// <param name="policy">Policy object.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task<HttpResponseMessage> UpdatePolicyWithResponseAsync( | ||
string realm, | ||
string policyId, | ||
Policy policy, | ||
CancellationToken cancellationToken = default | ||
); | ||
|
||
/// <summary> | ||
/// Updates a policy | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#managing-resource-permissions-using-the-policy-api | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID.</param> | ||
/// <param name="policy">Policy object.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
async Task UpdatePolicyAsync( | ||
string realm, | ||
string policyId, | ||
Policy policy, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
var response = await this.UpdatePolicyWithResponseAsync( | ||
realm, | ||
policyId, | ||
policy, | ||
cancellationToken | ||
); | ||
|
||
await response.EnsureResponseAsync(cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes a policy | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#removing-a-permission | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task<HttpResponseMessage> DeletePolicyWithResponseAsync( | ||
string realm, | ||
string policyId, | ||
CancellationToken cancellationToken = default | ||
); | ||
|
||
/// <summary> | ||
/// Deletes a policy | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/keycloak/keycloak/blob/main/docs/documentation/authorization_services/topics/service-protection-policy-api.adoc#removing-a-permission | ||
/// </remarks> | ||
/// <param name="realm">Realm name (not ID).</param> | ||
/// <param name="policyId">Policy ID.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
async Task DeletePolicyAsync( | ||
string realm, | ||
string policyId, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
var response = await this.DeletePolicyWithResponseAsync(realm, policyId, cancellationToken); | ||
|
||
await response.EnsureResponseAsync(cancellationToken); | ||
} | ||
} |
Oops, something went wrong.