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 name as values. |
IOrganizationRepository | organizationRepository | An optional IOrganizationRepository. |
IUserInfoProvider | userInfoProvider | An optional IUserInfoProvider. |
Remarks
The BrowserAuthenticatedAccessTokenProvider
tries to match running host location with location provided in localStorageKeyNames. 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)
Applies authorization information to a given set of HttpHeaders.
Declaration
public Task AddAuthorization(HttpHeaders headers)
Parameters
Type | Name | Description |
---|---|---|
HttpHeaders | headers | The HttpHeaders to add authorization information to. |
Returns
Type | Description |
---|---|
Task | A Task for the operation. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
GetOrganizationAsync(OrganizationId)
Gets a single IOrganization.
Declaration
public Task<IOrganization> GetOrganizationAsync(OrganizationId organizationId)
Parameters
Type | Name | Description |
---|---|---|
OrganizationId | organizationId | The Unity.Cloud.Common.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