Ejemplo n.º 1
0
        /// <inheritdoc />
        protected override async Task <HandleRequestResult> HandleRemoteAuthenticateAsync()
        {
            var query = Request.Query;
            var error = query["error"];

            if (!StringValues.IsNullOrEmpty(error))
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append(error);
                var errorDescription = query["error_description"];
                if (!StringValues.IsNullOrEmpty(errorDescription))
                {
                    stringBuilder.Append(";Description=").Append(errorDescription);
                }
                var errorUri = query["error_uri"];
                if (!StringValues.IsNullOrEmpty(errorUri))
                {
                    stringBuilder.Append(";Uri=").Append(errorUri);
                }
                return(HandleRequestResult.Fail(stringBuilder.ToString()));
            }
            var code       = query["code"];
            var state      = query["state"];
            var properties = Options.StateDataFormat.Unprotect(state) ?? new AuthenticationProperties();

            if (StringValues.IsNullOrEmpty(code))
            {
                return(HandleRequestResult.Fail("Code was not found."));
            }
            var tok = await ExchangeCodeAsync(code, BuildRedirectUri(Options.CallbackPath));

            var tokens = EHealthOAuthTokenResponse.Success(tok.Response);

            if (tokens.Error != null)
            {
                return(HandleRequestResult.Fail(tokens.Error));
            }
            if (string.IsNullOrEmpty(tokens.AccessToken))
            {
                return(HandleRequestResult.Fail("Failed to retrieve access token."));
            }
            var identity = new ClaimsIdentity(ClaimsIssuer);

            if (Options.SaveTokens)
            {
                var authenticationTokenList = new List <AuthenticationToken>
                {
                    new AuthenticationToken {
                        Name = "access_token", Value = tokens.AccessToken
                    }
                };
                if (!string.IsNullOrEmpty(tokens.RefreshToken))
                {
                    authenticationTokenList.Add(new AuthenticationToken {
                        Name = "refresh_token", Value = tokens.RefreshToken
                    });
                }
                if (!string.IsNullOrEmpty(tokens.TokenType))
                {
                    authenticationTokenList.Add(new AuthenticationToken {
                        Name = "token_type", Value = tokens.TokenType
                    });
                }
                if (!string.IsNullOrEmpty(tokens.ExpiresIn) && int.TryParse(tokens.ExpiresIn, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
                {
                    var dateTimeOffset = Clock.UtcNow + TimeSpan.FromSeconds(result);
                    authenticationTokenList.Add(new AuthenticationToken
                    {
                        Name  = "expires_at",
                        Value = dateTimeOffset.ToString("o", CultureInfo.InvariantCulture)
                    });
                }
                properties.StoreTokens(authenticationTokenList);
            }
            var ticketAsync = await CreateTicketAsync(identity, properties, tokens.Response);

            return(ticketAsync == null
                ? HandleRequestResult.Fail("Failed to retrieve user information from remote server.")
                : HandleRequestResult.Success(ticketAsync));
        }