Class CompositeAuthenticator
Uses a list of injected IAuthenticator to provide support for multiple authentication flows.
Implements
Inherited Members
Namespace: Unity.Cloud.Identity
Assembly: Unity.Cloud.Identity.dll
Syntax
public class CompositeAuthenticator : ICompositeAuthenticator, IUrlRedirectionAuthenticator, IAuthenticator, IServiceAuthorizer, IAuthenticationStateProvider, IUserInfoProvider, IOrganizationRepository, IDisposable
Remarks
The CompositeAuthenticator uses internally the first IAuthenticator from the injected list that returns a true value when invoking HasValidPreconditionsAsync() method.
Depending on the validated IAuthenticator, the authentication flow can require manual interaction with a UI. Use the RequiresGUI value to decide if you need to enable manual login features.
Examples
public class CompositeAuthenticatorExample : MonoBehaviour
{
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);
}
async Task Start()
{
await m_CompositeAuthenticator.InitializeAsync();
}
}
Constructors
CompositeAuthenticator(CompositeAuthenticatorSettings)
Provides a ICompositeAuthenticator that accepts a CompositeAuthenticatorSettings to handle and prioritize runtime execution contexts.
Declaration
public CompositeAuthenticator(CompositeAuthenticatorSettings compositeAuthenticatorSettings)
Parameters
| Type | Name | Description |
|---|---|---|
| CompositeAuthenticatorSettings | compositeAuthenticatorSettings | A CompositeAuthenticatorSettings that contains the prioritized list of IAuthenticator. |
Properties
AuthenticationState
Holds the current AuthenticationState.
Declaration
public AuthenticationState AuthenticationState { get; }
Property Value
| Type | Description |
|---|---|
| AuthenticationState |
RequiresGUI
Whether the ICompositeAuthenticator implementation requires a graphical user interface (GUI) for the authentication flow.
Declaration
public bool RequiresGUI { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
A GUI is required for a user to click login and logout buttons and provide credentials when requested. When no GUI is required, the login is done automatically in the InitializeAsync() method.
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 IDisposable references.
Declaration
public void Dispose()
Dispose(bool)
Disposes IDisposable references internally.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | The Boolean value received from the public |
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 |
Remarks
The CompositeAuthenticator always returns false to prevent nested ICompositeAuthenticator architecture.
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;
}
}
}