Class CompositeAuthenticator
Provides an IAccessTokenProvider implementation that's adapted to different runtime execution contexts.
Implements
Inherited Members
Namespace: Unity.DigitalTwins.Identity.Runtime
Assembly: solution.dll
Syntax
public class CompositeAuthenticator : IInteractiveAuthenticator, IAuthenticator, IPreAuthenticatedAccessTokenProvider, IAccessTokenProvider, IDisposable
Remarks
The execution context detection logic prioritizes the authentication injection setup, looking for an override value in the Environment variables and then in the runtime launch arguments. If no override is found, it returns the default user interactive PKCE Authentication implementation.
Examples
using System;
using Unity.DigitalTwins.Identity;
using Unity.DigitalTwins.Identity.Runtime;
using Unity.DigitalTwins.HttpClient;
using Unity.DigitalTwins.HttpClient.Runtime;
using Unity.DigitalTwins.AppSettings.Runtime;
using UnityEngine;
/// <summary>
/// A sample Monobehaviour class that displays the minimal requirements to use the CompositeAuthenticator.
/// </summary>
public class CompositeAuthenticatorSample : MonoBehaviour
{
IHttpClient m_HttpClient;
IInteractiveAuthenticator m_InteractiveAuthenticator;
IPreAuthenticatedAccessTokenProvider m_PreAuthenticatedAccessTokenProvider;
void Start()
{
var playerSettings = DigitalTwinsPlayerSettings.Instance;
m_HttpClient = new UnityHttpClient();
var compositeAuthenticator = new CompositeAuthenticator(m_HttpClient, playerSettings, playerSettings);
m_InteractiveAuthenticator = compositeAuthenticator;
m_PreAuthenticatedAccessTokenProvider = compositeAuthenticator;
}
void OnDisable()
{
m_InteractiveAuthenticator?.Dispose();
m_HttpClient?.Dispose();
}
}
Constructors
CompositeAuthenticator(IHttpClient, IAppIdProvider, IAppNameProvider, IPkceConfigurationProvider, IPkcePlatformSupport, IPkceRequestHandler, IUrlRedirectionInterceptor)
Provides an IAccessTokenProvider implementation that's adapted to different runtime execution contexts.
Declaration
public CompositeAuthenticator(IHttpClient httpClient, IAppIdProvider appIdProvider, IAppNameProvider appNameProvider, IPkceConfigurationProvider pkceConfigurationProvider = null, IPkcePlatformSupport pkcePlatformSupport = null, IPkceRequestHandler pkceRequestHandler = null, IUrlRedirectionInterceptor urlRedirectionInterceptor = null)
Parameters
Type | Name | Description |
---|---|---|
Unity.DigitalTwins.Common.IHttpClient | httpClient | An IHttpClient to reach cloud endpoints in the authentication flow. |
Unity.DigitalTwins.Common.IAppIdProvider | appIdProvider | An IAppIdProvider to inject the app identifier required on the app settings' cloud endpoints. |
Unity.DigitalTwins.Common.IAppNameProvider | appNameProvider | An IAppNameProvider to build the unique URI scheme that binds the app to the browser response in a login operation. |
IPkceConfigurationProvider | pkceConfigurationProvider | An alternate IPkceConfigurationProvider to override the built-in IPkceConfigurationProvider. |
IPkcePlatformSupport | pkcePlatformSupport | An alternate IPkcePlatformSupport to override the built-in IPkcePlatformSupport. |
IPkceRequestHandler | pkceRequestHandler | An alternate IPkceRequestHandler to override the built-in IPkceRequestHandler, which you can use to customize HTTP requests in the authentication flow. |
Unity.DigitalTwins.Common.IUrlRedirectionInterceptor | urlRedirectionInterceptor | An alternate IUrlRedirectionInterceptor to override the built-in IUrlRedirectionInterceptor. If a pkcePlatformSupport value is provided, this parameter isn't used. |
Remarks
The execution context detection logic prioritizes the authentication injection setup, looking for an override value in the environment variables and then in the runtime launch arguments. If no override is found, it returns the default user interactive PKCE authentication implementation.
Properties
AuthenticationState
Holds the current AuthenticationState.
Declaration
public AuthenticationState AuthenticationState { get; }
Property Value
Type | Description |
---|---|
AuthenticationState |
Interactive
Indicates whether the CompositeAuthenticator instance requires user interaction to achieve authentication.
Declaration
public bool Interactive { get; }
Property Value
Type | Description |
---|---|
Boolean | True if authentication requires user interaction. False if either injection performs or a pre-authenticated host provides authentication. |
Methods
Dispose()
Disposes IDisposable references.
Declaration
public void Dispose()
Dispose(Boolean)
Disposes IDisposable references internally.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
Boolean | disposing | The Boolean value received from the public Dispose method. |
GetAccessTokenAsync()
Asynchronously fetches the user access token.
Declaration
public Task<string> GetAccessTokenAsync()
Returns
Type | Description |
---|---|
Task<String> | A Task that results in the user access token's string value after completion. |
InitializeAsync()
Initializes the CompositeAuthenticator and its resulting AuthenticationState from the Interface implementation and initial configuration.
Declaration
public Task InitializeAsync()
Returns
Type | Description |
---|---|
Task | A Task. |
LoginAsync()
Performs a login operation.
Declaration
public Task LoginAsync()
Returns
Type | Description |
---|---|
Task | A Task. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if the CompositeAuthenticator wasn't properly initialized, if the AuthenticationState isn't LoggedOut, or the Interactive value is false. |
Unity.DigitalTwins.Common.AuthenticationFailedException |
LogoutAsync()
Performs a logout operation.
Declaration
public Task LogoutAsync()
Returns
Type | Description |
---|---|
Task | A Task. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if the CompositeAuthenticator wasn't properly initialized, if the AuthenticationState isn't LoggedIn, or the Interactive value is false. |
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 the AuthenticationStateChanged event should restrict or allow access to available resources and features based on the returned value.
Explicit Interface Implementations
IPreAuthenticatedAccessTokenProvider.SetAccessToken(String)
Sets the access token from a pre-authenticated host environment.
Declaration
void IPreAuthenticatedAccessTokenProvider.SetAccessToken(string accessToken)
Parameters
Type | Name | Description |
---|---|---|
String | accessToken | The string value of the pre-authenticated access token. |
Remarks
Triggers the AuthenticationStateChanged event. Assigning an empty or null string value sets the AuthenticationState to AuthenticationState.LoggedOut. Any other string value sets the AuthenticationState to AuthenticationState.LoggedIn.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown when the internal IAccessTokenProvider instance doesn't support setting the access token from the host environment. |