Class BrowserAuthenticatedAccessTokenProvider
An IAuthenticator implementation that expects an access token from a browser environment.
Implements
Inherited Members
Namespace: Unity.Cloud.Identity
Assembly: Unity.Cloud.Identity.dll
Syntax
public class BrowserAuthenticatedAccessTokenProvider : IAuthenticator, IServiceAuthorizer, IAuthenticationStateProvider, IUserInfoProvider, IOrganizationRepository
Examples
public class BrowserAuthenticatedAccessTokenProviderExample : MonoBehaviour
{
IAuthenticationStateProvider m_AuthenticationStateProvider => m_BrowserAuthenticatedAccessTokenProvider;
IAuthenticator m_BrowserAuthenticatedAccessTokenProvider;
void Awake()
{
var httpClient = new UnityHttpClient();
var playerSettings = UnityCloudPlayerSettings.Instance;
var authenticationPlatformSupport = PlatformSupportFactory.GetAuthenticationPlatformSupport();
var serviceHostResolver = UnityRuntimeServiceHostResolverFactory.Create();
var localStorageKeyNames = new Dictionary<string, string>() { { "dashboard.unity3d.com", "genesis-access-token" } };
var pkceAuthenticatorSettingsBuilder = new PkceAuthenticatorSettingsBuilder(authenticationPlatformSupport, serviceHostResolver);
pkceAuthenticatorSettingsBuilder.AddDefaultConfigurationProviderAndRequestHandler(httpClient, playerSettings)
.AddDefaultAccessTokenExchanger(httpClient);
var pkceAuthenticatorSettings = pkceAuthenticatorSettingsBuilder.Build();
m_BrowserAuthenticatedAccessTokenProvider = new BrowserAuthenticatedAccessTokenProvider(pkceAuthenticatorSettings, localStorageKeyNames);
m_AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
}
async Task Start()
{
await m_BrowserAuthenticatedAccessTokenProvider.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:
// Initial await time to retrieve the access token from the browser
break;
case AuthenticationState.AwaitingLogin:
break;
case AuthenticationState.LoggedIn:
// The access token provided is ready to use
break;
case AuthenticationState.AwaitingLogout:
break;
case AuthenticationState.LoggedOut:
break;
}
}
}
Constructors
BrowserAuthenticatedAccessTokenProvider(PkceAuthenticatorSettings, Dictionary<string, string>, IOrganizationRepository, IUserInfoProvider)
Returns an IAuthenticator implementation that expects an access token from a browser environment.
Declaration
public BrowserAuthenticatedAccessTokenProvider(PkceAuthenticatorSettings pkceAuthenticatorSettings, Dictionary<string, string> localStorageKeyNames = null, IOrganizationRepository organizationRepository = null, IUserInfoProvider userInfoProvider = null)
Parameters
| Type | Name | Description |
|---|---|---|
| PkceAuthenticatorSettings | pkceAuthenticatorSettings | The PkceAuthenticatorSettings that contains all PKCE authentication classes |
| Dictionary<string, string> | localStorageKeyNames | A dictionary with browser locations as keys and local storage key names as values. |
| IOrganizationRepository | organizationRepository | An optional IOrganizationRepository. |
| IUserInfoProvider | userInfoProvider | An optional IUserInfoProvider. |
Remarks
The BrowserAuthenticatedAccessTokenProvider tries to match the running host location with the location provided in the localStorageKeyNames dictionary. Use a single wildcard character (*) to match any host location.
Properties
AuthenticationState
Holds the current AuthenticationState.
Declaration
public AuthenticationState AuthenticationState { get; }
Property Value
| Type | Description |
|---|---|
| AuthenticationState |
DefaultLocalStorageKeyNames
Default list of supported domains with corresponding localStorage key names.
Declaration
public static Dictionary<string, string> DefaultLocalStorageKeyNames { get; }
Property Value
| Type | Description |
|---|---|
| Dictionary<string, string> |
Methods
AddAuthorization(HttpHeaders)
Declaration
public Task AddAuthorization(HttpHeaders headers)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpHeaders | headers |
Returns
| Type | Description |
|---|---|
| Task |
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 BrowserAuthenticatedAccessTokenProvider running instance has access to an access token from the browser environment.
Declaration
public Task<bool> HasValidPreconditionsAsync()
Returns
| Type | Description |
|---|---|
| Task<bool> | If the BrowserAuthenticatedAccessTokenProvider running instance has access to an access token. |
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. |
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;
}
}
}