Class BrowserAuthenticatedAccessTokenProvider
An IAuthenticator implementation that expects an access token from a browser environment.
Inherited Members
Namespace: Unity.Cloud.Identity
Syntax
public class BrowserAuthenticatedAccessTokenProvider : IAuthenticator, IAccessTokenProvider, IAuthenticatedUserInfoProvider, IAuthenticationStateProvider
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>)
Returns an IAccessTokenProviderimplementation that expects an access token from a browser environment.
Declaration
public BrowserAuthenticatedAccessTokenProvider(PkceAuthenticatorSettings pkceAuthenticatorSettings, Dictionary<string, string> localStorageKeyNames = 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. |
Remarks
The BrowserAuthenticatedAccessTokenProvider tries to match running host location with location provided in
Properties
AuthenticationState
Holds the current AuthenticationState.
Declaration
public AuthenticationState AuthenticationState { get; }
Property Value
| Type | Description |
|---|---|
| AuthenticationState |
Implements
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
GetAccessTokenAsync()
Declaration
public async Task<string> GetAccessTokenAsync()
Returns
| Type | Description |
|---|---|
| Task<String> |
Implements
GetUserInfo(String)
Declaration
public string GetUserInfo(string key)
Parameters
| Type | Name | Description |
|---|---|---|
| String | key |
Returns
| Type | Description |
|---|---|
| String |
Implements
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<Boolean> | If the BrowserAuthenticatedAccessTokenProvider running instance has access to an access token. |
Implements
InitializeAsync()
A task to initialize the AuthenticationState from either cache or direct injection value.
Declaration
public async Task InitializeAsync()
Returns
| Type | Description |
|---|---|
| Task |
Implements
Events
AuthenticationStateChanged
Triggers when the AuthenticationState of the current user changes.
Declaration
public event Action<AuthenticationState> AuthenticationStateChanged
Event Type
| Type | Description |
|---|---|
| Action<AuthenticationState> |
Implements
Remarks
Subscribers of this event should restrict or allow access to available resources and features based on the returned value.