-
Notifications
You must be signed in to change notification settings - Fork 39
Adding Auth0.AuthenticationApi
package dependency
#148
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
kailash-b
wants to merge
8
commits into
main
Choose a base branch
from
feature/SDK-5567
base: main
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
8 commits
Select commit
Hold shift + click to select a range
7cfda3a
Reference latest Auth0.AuthenticationApi package
kailash-b fb4ec7e
Reference IAuthenticationApiClient as a dependency
kailash-b ac8f7f0
Add IAuth0CibaService and its implementation
kailash-b 414fd03
Add test cases for Auth0CibaService
kailash-b bca3fc7
Register Auth0CibaService in DI
kailash-b 2af0fed
Updating Examples.md
kailash-b 4050199
Refactor to address review comments
kailash-b 7808873
Update Examples.md
kailash-b 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
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
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
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
131 changes: 131 additions & 0 deletions
131
...h0.AspNetCore.Authentication/ClientInitiatedBackChannelAuthentication/Auth0CibaService.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,131 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Auth0.AuthenticationApi; | ||
using Auth0.AuthenticationApi.Models.Ciba; | ||
using Auth0.Core.Exceptions; | ||
|
||
namespace Auth0.AspNetCore.Authentication.ClientInitiatedBackChannelAuthentication; | ||
|
||
internal class Auth0CibaService : IAuth0CibaService | ||
{ | ||
private readonly IAuthenticationApiClient _authenticationApiClient; | ||
private readonly Auth0WebAppOptions _options; | ||
private readonly ILogger<Auth0CibaService> _logger; | ||
|
||
/// <summary> | ||
/// Initiates an instance of Auth0CibaService which can be used to execute the CIBA workflow. | ||
/// </summary> | ||
/// <param name="authenticationApiClient">Instance of <see cref="Auth0.AuthenticationApi.IAuthenticationApiClient"/> </param> | ||
/// <param name="optionsAccessor"><see cref="Auth0WebAppOptions"/></param> | ||
/// <param name="logger"></param> | ||
public Auth0CibaService( | ||
IAuthenticationApiClient authenticationApiClient, | ||
IOptions<Auth0WebAppOptions> optionsAccessor, | ||
ILogger<Auth0CibaService> logger) | ||
{ | ||
_authenticationApiClient = authenticationApiClient; | ||
_options = optionsAccessor.Value; | ||
_logger = logger; | ||
} | ||
|
||
/// <inheritdoc cref="Auth0.AspNetCore.Authentication.ClientInitiatedBackChannelAuthentication.IAuth0CibaService.InitiateAuthenticationAsync"/> | ||
public async Task<CibaInitiationDetails> InitiateAuthenticationAsync( | ||
CibaInitiationRequest request) | ||
{ | ||
try | ||
{ | ||
var cibaRequest = new ClientInitiatedBackchannelAuthorizationRequest | ||
{ | ||
ClientId = _options.ClientId, | ||
ClientSecret = _options.ClientSecret, | ||
ClientAssertionSecurityKey = _options.ClientAssertionSecurityKey, | ||
ClientAssertionSecurityKeyAlgorithm = _options.ClientAssertionSecurityKeyAlgorithm, | ||
Audience = request.Audience, | ||
LoginHint = request.LoginHint, | ||
Scope = request.Scope, | ||
RequestExpiry = request.RequestExpiry, | ||
AdditionalProperties = request.AdditionalProperties, | ||
BindingMessage = request.BindingMessage, | ||
}; | ||
|
||
_logger.LogInformation("Initiating CIBA request!"); | ||
var response = await _authenticationApiClient.ClientInitiatedBackchannelAuthorization(cibaRequest); | ||
|
||
return new CibaInitiationDetails() | ||
{ | ||
AuthRequestId = response.AuthRequestId, | ||
ExpiresIn = response.ExpiresIn, | ||
Interval = response.Interval, | ||
IsSuccessful = true, | ||
ErrorMessage = null | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error initiating CIBA request"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="Auth0.AspNetCore.Authentication.ClientInitiatedBackChannelAuthentication.IAuth0CibaService.PollForTokensAsync"/> | ||
public async Task<CibaCompletionDetails> PollForTokensAsync( | ||
CibaInitiationDetails initDetails, CancellationToken cancellationToken) | ||
{ | ||
var request = new ClientInitiatedBackchannelAuthorizationTokenRequest() | ||
{ | ||
ClientId = _options.ClientId, | ||
ClientSecret = _options.ClientSecret, | ||
ClientAssertionSecurityKey = _options.ClientAssertionSecurityKey, | ||
ClientAssertionSecurityKeyAlgorithm = _options.ClientAssertionSecurityKeyAlgorithm, | ||
AuthRequestId = initDetails.AuthRequestId | ||
}; | ||
|
||
var completionDetails = new CibaCompletionDetails() | ||
{ | ||
IsSuccessful = false, | ||
IsAuthenticationPending = true | ||
}; | ||
|
||
while (completionDetails is { IsAuthenticationPending: true, IsSuccessful: false }) | ||
{ | ||
_logger.LogDebug($"Polling CIBA token endpoint for auth_req_id: {initDetails.AuthRequestId} "); | ||
try | ||
{ | ||
var response = await _authenticationApiClient.GetTokenAsync(request, cancellationToken); | ||
|
||
completionDetails.AccessToken = response.AccessToken; | ||
completionDetails.IdToken = response.IdToken; | ||
completionDetails.TokenType = response.TokenType; | ||
completionDetails.Scope = response.Scope; | ||
completionDetails.ExpiresIn = response.ExpiresIn; | ||
completionDetails.RefreshToken = response.RefreshToken; | ||
completionDetails.IsSuccessful = true; | ||
completionDetails.IsAuthenticationPending = false; | ||
} | ||
catch (ErrorApiException ex) | ||
{ | ||
_logger.LogWarning( | ||
ex, | ||
$"CIBA polling error for auth_req_id: {initDetails.AuthRequestId}." + | ||
$" Error: {ex.ApiError.Error}, Description: {ex.ApiError.Message}"); | ||
|
||
if (ex.ApiError.Error.Contains("authorization_pending", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(initDetails.Interval)); | ||
continue; | ||
} | ||
|
||
completionDetails.IsAuthenticationPending = false; | ||
completionDetails.Error = ex.ApiError.Error; | ||
completionDetails.ErrorMessage = ex.ApiError.Message; | ||
completionDetails.IsSuccessful = false; | ||
} | ||
} | ||
return completionDetails; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...0.AspNetCore.Authentication/ClientInitiatedBackChannelAuthentication/IAuth0CibaService.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,81 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Auth0.AuthenticationApi.Models.Ciba; | ||
|
||
namespace Auth0.AspNetCore.Authentication.ClientInitiatedBackChannelAuthentication; | ||
|
||
public class CibaInitiationDetails : ClientInitiatedBackchannelAuthorizationResponse | ||
{ | ||
/// <summary> | ||
/// Indicates whether the polling was successful. | ||
/// </summary> | ||
public bool IsSuccessful { get; init; } = true; | ||
|
||
/// <summary> | ||
/// Indicates any errors that occurred during the initiation of the CIBA request. | ||
/// </summary> | ||
public string? ErrorMessage { get; init; } | ||
} | ||
|
||
public class CibaInitiationRequest | ||
{ | ||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.ClientInitiatedBackchannelAuthorizationRequest.BindingMessage"/> | ||
public string? BindingMessage { get; set; } | ||
|
||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.LoginHint"/> | ||
public LoginHint? LoginHint { get; set; } | ||
|
||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.ClientInitiatedBackchannelAuthorizationRequest.Scope"/> | ||
public string? Scope { get; set; } | ||
|
||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.ClientInitiatedBackchannelAuthorizationRequest.Audience"/> | ||
public string? Audience { get; set; } | ||
|
||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.ClientInitiatedBackchannelAuthorizationRequest.RequestExpiry"/> | ||
public int? RequestExpiry { get; set; } | ||
|
||
/// <inheritdoc cref="Auth0.AuthenticationApi.Models.Ciba.ClientInitiatedBackchannelAuthorizationRequest.AdditionalProperties"/> | ||
public IDictionary<string, string> AdditionalProperties { get; set; } = new Dictionary<string, string>(); | ||
} | ||
|
||
public class CibaCompletionDetails : ClientInitiatedBackchannelAuthorizationTokenResponse | ||
{ | ||
/// <summary> | ||
/// Signifies if the authentication is pending. | ||
/// </summary> | ||
public bool IsAuthenticationPending { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Signifies if the authentication is successful. | ||
/// </summary> | ||
public bool IsSuccessful { get; set; } = false; | ||
|
||
/// <summary> | ||
/// The error received in case of expiry or consent rejection | ||
/// </summary> | ||
public string? Error { get; set; } | ||
|
||
/// <summary> | ||
/// The error message received in case of expiry or consent rejection | ||
/// </summary> | ||
public string? ErrorMessage { get; set; } | ||
} | ||
|
||
public interface IAuth0CibaService | ||
{ | ||
/// <summary> | ||
/// Initiates a Client-Initiated Backchannel Authentication (CIBA) flow. | ||
/// </summary> | ||
/// <param name="request">Contains the information required for initiating the CIBA request.</param> | ||
Task<CibaInitiationDetails> InitiateAuthenticationAsync(CibaInitiationRequest request); | ||
|
||
/// <summary> | ||
/// Polls the token endpoint to check the status of a CIBA request and retrieve tokens upon completion. | ||
/// </summary> | ||
/// <param name="cibaInitiationDetails">The information required to poll for the CIBA status.</param> | ||
/// <param name="cancellationToken"><see cref="CancellationToken"/></param> | ||
/// <returns>Details about the CIBA completion status or the retrieved tokens.</returns> | ||
Task<CibaCompletionDetails> PollForTokensAsync(CibaInitiationDetails cibaInitiationDetails, CancellationToken cancellationToken = default); | ||
} |
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.
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.