Class AzureADOboTokenProvider
- Namespace
- Arc4u.OAuth2.TokenProviders
- Assembly
- Arc4u.OAuth2.AspNetCore.Authentication.dll
An ITokenProvider that exchanges the access token of the current user for a token for another API, with the on-behalf-of flow
(urn:ietf:params:oauth:grant-type:jwt-bearer). The user token is the access token of the scoped TokenRefreshInfo (the OpenID Connect session) or, if empty, the BootstrapContext of the current principal (the bearer token of the request).
The ClientId, ClientSecret and Scope come from the settings (see OnBehalfOfSettingsOptions) and the request goes to the token endpoint of the Default authority.
The resulting token is cached until it expires.
[Export("Obo", typeof(ITokenProvider))]
public class AzureADOboTokenProvider : ISignOutTokenProvider, ITokenProvider
- Inheritance
-
AzureADOboTokenProvider
- Implements
- Inherited Members
Constructors
AzureADOboTokenProvider(TokenRefreshInfo, ICacheHelper, IActivitySourceFactory, IApplicationContext, IOptionsMonitor<AuthorityOptions>, ILogger<AzureADOboTokenProvider>, IUserObjectIdentifier?, IHttpClientFactory?, TokenCacheResolver?)
An ITokenProvider that exchanges the access token of the current user for a token for another API, with the on-behalf-of flow
(urn:ietf:params:oauth:grant-type:jwt-bearer). The user token is the access token of the scoped TokenRefreshInfo (the OpenID Connect session) or, if empty, the BootstrapContext of the current principal (the bearer token of the request).
The ClientId, ClientSecret and Scope come from the settings (see OnBehalfOfSettingsOptions) and the request goes to the token endpoint of the Default authority.
The resulting token is cached until it expires.
public AzureADOboTokenProvider(TokenRefreshInfo tokenRefreshInfo, ICacheHelper cacheHelper, IActivitySourceFactory activitySourceFactory, IApplicationContext applicationContext, IOptionsMonitor<AuthorityOptions> authorities, ILogger<AzureADOboTokenProvider> logger, IUserObjectIdentifier? userObjectIdentifier = null, IHttpClientFactory? httpClientFactory = null, TokenCacheResolver? tokenCacheResolver = null)
Parameters
tokenRefreshInfoTokenRefreshInfoThe scoped access and refresh tokens of the user.
cacheHelperICacheHelperThe helper giving access to the token cache.
activitySourceFactoryIActivitySourceFactoryThe factory of the activity source used for telemetry.
applicationContextIApplicationContextThe application context giving the current principal.
authoritiesIOptionsMonitor<AuthorityOptions>The named authority options.
loggerILogger<AzureADOboTokenProvider>The logger.
userObjectIdentifierIUserObjectIdentifierWhen registered, identifies the user of the index of the cached tokens from the claims configured in ClaimsIdentifierOption, as the claims cache does; otherwise the user is identified by the
oid(orsub) claim of the token.httpClientFactoryIHttpClientFactoryWhen registered, creates the HttpClient that calls the token endpoint: the one named by the
HttpClientNamesetting, or the default one. Otherwise a shared HttpClient is used.tokenCacheResolverTokenCacheResolverResolves the cache of the token cache options. The
TokenCacheOptionsNamesetting selects named options (see TokenCache): the tokens are stored in their cache, for at most their MaxTime. When null, the tokens are stored in the token cache of the ICacheHelper until they expire. The index of the tokens of a user is always stored in the token cache of the ICacheHelper.
Fields
ProviderName
The key (Obo) under which the provider is registered.
public const string ProviderName = "Obo"
Field Value
Methods
GetTokenAsync(IKeyValueSettings?, object?)
Gets a token for the API designated by the settings, on behalf of the current user.
public ValueTask<Result<TokenInfo>> GetTokenAsync(IKeyValueSettings? settings, object? platformParameters)
Parameters
settingsIKeyValueSettingsThe on-behalf-of settings (client id, client secret and scope).
platformParametersobjectNot used.
Returns
- ValueTask<Result<TokenInfo>>
The token, or a failed result when there is no principal or no token for the user, a setting is missing, the token endpoint does not answer with a success status code, or its response has no access token.
Exceptions
- ArgumentNullException
settingsis null.
SignOutAsync(IKeyValueSettings, CancellationToken)
Removes from the cache all the on-behalf-of tokens obtained for the current user, whatever the settings (API) and the user access token (before or after a refresh) they were obtained with: the keys are read from the OboTokenCacheIndex of the user. Nothing is done when there is no token for the user. A key that cannot be removed is logged and kept in the index, so a next sign-out of the user removes it.
public ValueTask SignOutAsync(IKeyValueSettings settings, CancellationToken cancellationToken)
Parameters
settingsIKeyValueSettingsNot used: the tokens of every on-behalf-of settings are removed.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- ValueTask
A task that completes when the tokens are removed from the cache.