Class PkceAuthenticator
Provides authentication through PKCE (Proof Key Code Exchange) standards.
Implements
Inherited Members
Namespace: Unity.Cloud.Identity
Assembly: Unity.Cloud.Identity.dll
Syntax
public class PkceAuthenticator : IUrlRedirectionAuthenticator, IAuthenticator, IServiceAuthorizer, IAuthenticationStateProvider, IUserInfoProvider, IOrganizationRepository, IDisposable
Constructors
PkceAuthenticator(PkceAuthenticatorSettings, IOrganizationRepository, IUserInfoProvider)
Provides a PkceAuthenticator that accepts a PkceAuthenticatorSettings to handle Proof Key Code Exchange (PKCE) authentication contexts.
Declaration
public PkceAuthenticator(PkceAuthenticatorSettings pkceAuthenticatorSettings, IOrganizationRepository organizationRepository = null, IUserInfoProvider userInfoProvider = null)
Parameters
| Type | Name | Description |
|---|---|---|
| PkceAuthenticatorSettings | pkceAuthenticatorSettings | A PkceAuthenticatorSettings that contains the parameters required for constructing the authenticator. |
| IOrganizationRepository | organizationRepository | An optional IOrganizationRepository. |
| IUserInfoProvider | userInfoProvider | An optional IUserInfoProvider. |
Properties
AuthenticationState
Holds the current AuthenticationState.
Declaration
public AuthenticationState AuthenticationState { get; }
Property Value
| Type | Description |
|---|---|
| AuthenticationState |
Methods
AddAuthorization(HttpHeaders)
Declaration
public Task AddAuthorization(HttpHeaders headers)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpHeaders | headers |
Returns
| Type | Description |
|---|---|
| Task |
CancelLogin()
Cancels the awaiting login operation.
Declaration
public void CancelLogin()
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
Dispose()
Disposes of any IDisposable references.
Declaration
public void Dispose()
Dispose(bool)
Disposes of any IDisposable references.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | Dispose pattern boolean value received from public Dispose method. |
GetOrganizationAsync(OrganizationId)
Gets a single IOrganization.
Declaration
public Task<IOrganization> GetOrganizationAsync(OrganizationId organizationId)
Parameters
| Type | Name | Description |
|---|---|---|
| OrganizationId | organizationId | The OrganizationId of the IOrganization to get. |
Returns
| Type | Description |
|---|---|
| Task<IOrganization> | A task that once completed returns an IOrganization. |
Exceptions
| Type | Condition |
|---|---|
| NotFoundException |
GetUserInfoAsync()
A task to fetch asynchronously user information.
Declaration
public Task<IUserInfo> GetUserInfoAsync()
Returns
| Type | Description |
|---|---|
| Task<IUserInfo> | An IUserInfo instance. |
HasValidPreconditionsAsync()
Indicates if the IAuthenticator has valid preconditions to provide authentication in the current execution context.
Declaration
public Task<bool> HasValidPreconditionsAsync()
Returns
| Type | Description |
|---|---|
| Task<bool> | A task that when completed indicates if the |
InitializeAsync()
A task to initialize the AuthenticationState from either cache or direct injection value.
Declaration
public Task InitializeAsync()
Returns
| Type | Description |
|---|---|
| Task | A task. |
ListOrganizationsAsync(Range, CancellationToken)
Lists IOrganization.
Declaration
public IAsyncEnumerable<IOrganization> ListOrganizationsAsync(Range range, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Range | range | A range of IOrganization to request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<IOrganization> | An IAsyncEnumerable<T> of Type IOrganization. |
LoginAsync()
Performs a login operation.
Declaration
public Task LoginAsync()
Returns
| Type | Description |
|---|---|
| Task | A task. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | |
| AuthenticationFailedException |
LogoutAsync(bool)
Performs a logout operation.
Declaration
public Task LogoutAsync(bool clearBrowserCache = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | clearBrowserCache | An optional boolean value that, if set to true, triggers a navigation to the OS default browser to clear any cached session. |
Returns
| Type | Description |
|---|---|
| Task | A task. |
Remarks
A logout operation clears the user session in the application only. Unless the user manually clears the session in the browser that is used for authentication, a user can get automatically logged in again from cached values, without entering any credentials. Use the clearBrowserCache boolean to also clear the session in the browser to prevent automatic login from a cached session.
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
Events
AuthenticationStateChanged
Triggers when the AuthenticationState of the current user changes.
Declaration
public event Action<AuthenticationState> AuthenticationStateChanged
Event Type
| Type | Description |
|---|---|
| Action<AuthenticationState> |
Remarks
Subscribers of this event should restrict or allow access to available resources and features based on the returned value.
Examples
public class AuthenticationStateProviderExample : MonoBehaviour
{
IAuthenticationStateProvider m_AuthenticationStateProvider => m_CompositeAuthenticator;
CompositeAuthenticator m_CompositeAuthenticator;
void Awake()
{
var playerSettings = UnityCloudPlayerSettings.Instance;
var httpClient = new UnityHttpClient();
var serviceHostResolver = UnityRuntimeServiceHostResolverFactory.Create();
var compositeAuthenticatorSettings = new CompositeAuthenticatorSettingsBuilder(httpClient, PlatformSupportFactory.GetAuthenticationPlatformSupport(), serviceHostResolver, playerSettings)
.AddDefaultBrowserAuthenticatedAccessTokenProvider(playerSettings)
.AddDefaultPkceAuthenticator(playerSettings)
.Build();
m_CompositeAuthenticator = new CompositeAuthenticator(compositeAuthenticatorSettings);
m_AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
}
async Task Start()
{
await m_CompositeAuthenticator.InitializeAsync();
// After initialize, Update UI with current state
ApplyAuthenticationState(m_AuthenticationStateProvider.AuthenticationState);
}
void OnDisable()
{
m_AuthenticationStateProvider.AuthenticationStateChanged -= OnAuthenticationStateChanged;
}
void OnAuthenticationStateChanged(AuthenticationState state)
{
ApplyAuthenticationState(state);
}
void ApplyAuthenticationState(AuthenticationState newAuthenticationState)
{
switch (newAuthenticationState)
{
case AuthenticationState.AwaitingInitialization:
break;
case AuthenticationState.AwaitingLogin:
break;
case AuthenticationState.LoggedIn:
break;
case AuthenticationState.AwaitingLogout:
break;
case AuthenticationState.LoggedOut:
break;
}
}
}