/// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="options">The options to be passed down to the underlying Authentication library handling the authentication operations.</param>
 /// <param name="tokenCache">The token cache to use to store tokens.</param>
 /// <param name="protectedStorage">The protect storage where refresh tokens will be stored.</param>
 /// <param name="accountClaimsPrincipalFactory">The <see cref="AccountClaimsPrincipalFactory{TAccount}"/> used to generate the <see cref="ClaimsPrincipal"/> for the user.</param>
 public TizenAuthenticationService(
     IOptionsSnapshot <RemoteAuthenticationOptions <TProviderOptions> > options,
     ITokenCache tokenCache,
     IProtectedStorage protectedStorage,
     AccountClaimsPrincipalFactory <TAccount> accountClaimsPrincipalFactory) : base(options, tokenCache, protectedStorage, accountClaimsPrincipalFactory)
 {
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the current authenticated user using JavaScript interop.
        /// </summary>
        /// <returns>A <see cref="Task{ClaimsPrincipal}"/>that will return the current authenticated user when completes.</returns>
        protected override async Task <ClaimsPrincipal> GetAuthenticatedUser()
        {
            var accessTokenResult = await RequestAccessToken();

            if (accessTokenResult.Status == AccessTokenResultStatus.Success && accessTokenResult.TryGetToken(out var accessToken))
            {
                using var userInfoClient = CreateClient(Client.Options);
                using var request        = new UserInfoRequest
                      {
                          Address = Client.Options.ProviderInformation.UserInfoEndpoint,
                          Token   = accessToken.Value,
                      };

                var userInfoResponse = await userInfoClient.GetUserInfoAsync(request).ConfigureAwait(true);

                if (userInfoResponse.Exception != null)
                {
                    throw userInfoResponse.Exception;
                }

                var account = JsonSerializer.Deserialize <TAccount>(userInfoResponse.Raw);

                await MergeIdTokenClaims(account);

                return(await AccountClaimsPrincipalFactory.CreateUserAsync(account, Options.UserOptions));
            }
            else
            {
                return(new ClaimsPrincipal(new ClaimsIdentity()));
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="options">The options to be passed down to the underlying Authentication library handling the authentication operations.</param>
 /// <param name="tokenCache">The token cache to use to store tokens.</param>
 /// <param name="protectedStorage">The protect storage where refresh tokens will be stored.</param>
 /// <param name="accountClaimsPrincipalFactory">The <see cref="AccountClaimsPrincipalFactory{TAccount}"/> used to generate the <see cref="ClaimsPrincipal"/> for the user.</param>
 protected OidcAuthenticationService(
     IOptionsSnapshot <RemoteAuthenticationOptions <TProviderOptions> > options,
     ITokenCache tokenCache,
     IProtectedStorage protectedStorage,
     AccountClaimsPrincipalFactory <TAccount> accountClaimsPrincipalFactory) : base(options?.Value, accountClaimsPrincipalFactory)
 {
     TokenCache        = tokenCache ?? throw new ArgumentNullException(nameof(tokenCache));
     _protectedStorage = protectedStorage;
 }
        /// <summary>
        /// Gets the users and converts to into a <see cref="ClaimsPrincipal"/>
        /// </summary>
        /// <param name="useCache"></param>
        /// <returns></returns>
        protected async Task <ClaimsPrincipal> GetUser(bool useCache = false)
        {
            var now = DateTimeOffset.Now;

            if (useCache && now < _userLastCheck + _userCacheRefreshInterval)
            {
                return(_cachedUser);
            }

            try
            {
                var authenticatedUser = await GetAuthenticatedUser();

                if (!ClaimsPrincipalEqualityComparer.Equals(_cachedUser, authenticatedUser))
                {
                    NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(authenticatedUser)));
                }
                _cachedUser    = authenticatedUser;
                _userLastCheck = now;
                return(_cachedUser);
            }
            catch
            {
                // only throw when not using the cache (e.g. after a sign in or sign-out operation).
                if (!useCache)
                {
                    throw;
                }
                else
                {
                    // pass through the AccountClaimsPrincipalFactory to facilitate possible restore from storage.
                    // this is equal to silent fail of the authentication in the javascript version where a user
                    // is returned from javascript.
                    var recreatedUser = await AccountClaimsPrincipalFactory.CreateUserAsync(null, Options.UserOptions);

                    if (!ClaimsPrincipalEqualityComparer.Equals(_cachedUser, recreatedUser))
                    {
                        NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(recreatedUser)));
                    }
                    _cachedUser    = recreatedUser;
                    _userLastCheck = now;
                    return(_cachedUser);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AuthenticationServiceBase{TAccount, TProviderOptions}"/> class.
        /// </summary>
        /// <param name="options">The options for this <see cref="AuthenticationServiceBase{TAccount, TProviderOptions}"/></param>
        /// <param name="accountClaimsPrincipalFactory">The factory to convert the <typeparamref name="TAccount"/> into a <see cref="ClaimsPrincipal"/></param>
        public AuthenticationServiceBase(RemoteAuthenticationOptions <TProviderOptions> options, AccountClaimsPrincipalFactory <TAccount> accountClaimsPrincipalFactory)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            AccountClaimsPrincipalFactory = accountClaimsPrincipalFactory ?? throw new ArgumentNullException(nameof(accountClaimsPrincipalFactory));
            Options = options;
        }