示例#1
0
        public async Task UpdateAsync(IAuthenticateResult authenticateResult, CancellationToken cancellationToken)
        {
            var httpContext = this.httpContextAccessor.HttpContext;

            await httpContext.SignOutAsync(this.authenticationScheme).ConfigureAwait(false);

            await httpContext.SignInAsync(this.authenticationScheme, authenticateResult.Principal, authenticateResult.Properties).ConfigureAwait(false);
        }
示例#2
0
            public AuthenticationTicket(IAuthenticateResult authenticateResult)
            {
                if (!SpotifyRequiredTokensUtils.TryGet(authenticateResult.Properties, out var tokens))
                {
                    throw new SpotifyMalformedAuthenticationTicketException("Unable to get authentication tokens.");
                }

                this.RefreshToken = tokens.Value.RefreshToken;

                this.AccessToken = new AccessToken(
                    tokens.Value.AccessToken,
                    tokens.Value.ExpiresAt);

                this.User = new PrincipalUser(authenticateResult.Principal);
            }
示例#3
0
            public void SetAuthenticateResult(string userId, string refreshToken, string accessToken, DateTimeOffset expiresAt)
            {
                var principal = new ClaimsPrincipal();

                principal.AddIdentity(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, userId) }));

                var properties = new AuthenticationProperties(new Dictionary <string, string>()
                {
                    [".Token.refresh_token"] = refreshToken,
                    [".Token.access_token"]  = accessToken,
                    [".Token.expires_at"]    = expiresAt.ToString("o", CultureInfo.InvariantCulture)
                });

                this.authenticateResult = new AuthenticateResult {
                    Principal = principal, Properties = properties
                };
            }
示例#4
0
            public AuthenticationTicket(IAuthenticateResult authenticateResult)
            {
                var refreshToken = authenticateResult.Properties.GetTokenValue(RefreshTokenKey);
                var accessToken  = authenticateResult.Properties.GetTokenValue(AccessTokenKey);
                var expiresAt    = authenticateResult.Properties.GetTokenValue(ExpiresAtKey);

                if (string.IsNullOrEmpty(refreshToken) || string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(expiresAt))
                {
                    throw new InvalidOperationException("Authentication tokens were not found.");
                }

                this.RefreshToken = refreshToken;

                this.AccessToken = new AccessToken(
                    accessToken,
                    DateTimeOffset.ParseExact(expiresAt, "o", CultureInfo.InvariantCulture));

                this.User = new PrincipalUser(authenticateResult.Principal);
            }
示例#5
0
        private async Task <AuthenticationTicket> GetNewAccessTokenAsync(AuthenticationTicket authenticationTicket, IAuthenticateResult authenticateResult, CancellationToken cancellationToken)
        {
            var accessTokenResponse = await this.tokenClient.GetAccessTokenFromRefreshTokenAsync(authenticationTicket.RefreshToken, cancellationToken).ConfigureAwait(false);

            var accessToken = accessTokenResponse.GetAccessTokenModel(this.clock);

            authenticateResult.Properties.UpdateTokenValue(TokenNames.AccessToken, accessToken.Token);
            authenticateResult.Properties.UpdateTokenValue(TokenNames.ExpiresAt, accessToken.ExpiresAt.ToString("o", CultureInfo.InvariantCulture));

            await this.authenticationManager.UpdateAsync(authenticateResult, cancellationToken).ConfigureAwait(false);

            return(new AuthenticationTicket(authenticateResult));
        }
示例#6
0
            Task IAuthenticationManager.UpdateAsync(IAuthenticateResult authenticateResult, CancellationToken cancellationToken)
            {
                this.authenticateResult = authenticateResult;

                return(Task.CompletedTask);
            }
示例#7
0
        private async Task <AuthenticationTicket> GetNewAccessTokenAsync(AuthenticationTicket authenticationTicket, IAuthenticateResult authenticateResult, CancellationToken cancellationToken)
        {
            var accessTokenDto = await this.tokenHttpClient
                                 .GetAccessTokenOrThrowInvalidRefreshTokenExceptionAsync(authenticationTicket.RefreshToken, cancellationToken)
                                 .ConfigureAwait(false);

            var accessToken = accessTokenDto.ToModelToken(this.dateTimeOffsetProvider);

            authenticateResult.Properties.UpdateTokenValue(AccessTokenKey, accessToken.Token);
            authenticateResult.Properties.UpdateTokenValue(ExpiresAtKey, accessToken.ExpiresAt.ToString("o", CultureInfo.InvariantCulture));

            await this.authenticationManager.UpdateAsync(authenticateResult, cancellationToken).ConfigureAwait(false);

            return(new AuthenticationTicket(authenticateResult));
        }