public override async Task <AuthorizationFlowCredential> OnGetToken(string code, string redirectUri, string state)
        {
            var googleOptions = AuthenticationOptions.Get(this.SchemeName);

            try
            {
                var requestId             = $"GoogleAuthorizationFlow-{Guid.NewGuid()}";
                var authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
                {
                    ClientSecrets = new ClientSecrets()
                    {
                        ClientId     = googleOptions.ClientId,
                        ClientSecret = googleOptions.ClientSecret,
                    },
                    IncludeGrantedScopes = true,
                });
                var tokenResponse = await authorizationCodeFlow.ExchangeCodeForTokenAsync(
                    requestId,
                    code,
                    redirectUri,
                    CancellationToken.None);

                var payload = await GoogleJsonWebSignature.ValidateAsync(tokenResponse.IdToken);

                return(new AuthorizationFlowCredential
                {
                    UserId = payload.Subject,
                    Email = payload.Email,
                    DisplayName = payload.Name,
                    GivenName = payload.GivenName,
                    FamilyName = payload.FamilyName,
                    TokenType = tokenResponse.TokenType,
                    IdToken = tokenResponse.IdToken,
                    AccessToken = tokenResponse.AccessToken,
                    RefreshToken = tokenResponse.RefreshToken,
                    IssuedUtc = tokenResponse.IssuedUtc,
                    ExpiresInSeconds = tokenResponse.ExpiresInSeconds,
                    Scope = tokenResponse.Scope,
                });
            }
            catch (TokenResponseException ex)
            {
                Logger.LogError(ex, ex.Message);
                throw new AuthorizationFlowException(ex.Error.Error, ex.Error.ErrorDescription, ex.Error.ErrorUri);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.Message);
                throw;
            }
        }
Exemple #2
0
        public override void OnAuthorization(
            string response_type, string scopes, string redirectUri, string state)
        {
            var microsoftOptions  = AuthenticationOptions.Get(this.SchemeName);
            var scopes_definitive = string.Join(" ",
                                                (scopes?.Trim().Split(" ") ?? microsoftOptions.Scope)
                                                .Union(new string[] { "openid", "offline_access" })
                                                .Distinct());

            var authorizeUrl = $"{microsoftOptions.AuthorizationEndpoint}" +
                               $"?client_id={microsoftOptions.ClientId}" +
                               $"&response_type={response_type}" +
                               $"&redirect_uri={Uri.EscapeDataString(redirectUri)}" +
                               $"&response_mode=query" +
                               $"&scope={Uri.EscapeDataString(scopes_definitive)}" +
                               $"{(state == null ? "" : $"&state={state}")}";