public async Task <IActionResult> ValidateAsync([FromRoute] string audience,
                                                        [FromBody] TokenValidationRequest validationRequest,
                                                        CancellationToken cancellationToken = default)
        {
            var tokenModel = _mapper.Map <TokenModel>(validationRequest);

            _logger.LogInformation("Validation request");

            var result = await _tokenValidationService.ValidateAsync(audience, tokenModel, cancellationToken);

            return(Ok(_mapper.Map <TokenValidationResponse>(result)));
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey(AuthorizationHeaderKey))
            {
                Logger.LogDebug("Request doesn't contain Authorization header");
                return(AuthenticateResult.NoResult());
            }

            if (!AuthenticationHeaderValue.TryParse(Request.Headers[AuthorizationHeaderKey], out AuthenticationHeaderValue headerValue))
            {
                Logger.LogDebug($"Authorization header has invalid format {Request.Headers[AuthorizationHeaderKey]}");
                return(AuthenticateResult.NoResult());
            }

            if (!Scheme.Name.Equals(headerValue.Scheme, StringComparison.OrdinalIgnoreCase))
            {
                Logger.LogDebug($"Authorization header doesn't contain {headerValue.Scheme} scheme {headerValue}");
                return(AuthenticateResult.NoResult());
            }

            try
            {
                var result = await _tokenValidationService.ValidateAsync(_audience, new TokenModel(headerValue.Parameter));

                if (result.Expiration > 0)
                {
                    var identities = new List <ClaimsIdentity>
                    {
                        new ClaimsIdentity("Orange Jwt authorization")
                    };
                    var ticket = new AuthenticationTicket(new ClaimsPrincipal(identities), Scheme.Name);

                    return(AuthenticateResult.Success(ticket));
                }

                Logger.LogDebug($"Token is expired {headerValue}");
                return(AuthenticateResult.NoResult());
            }
            catch (TokenValidationException ex)
            {
                Logger.LogDebug(ex, "Authenticate exception:");

                return(AuthenticateResult.NoResult());
            }
        }
예제 #3
0
        public async Task ValidateTokenTest_Successful()
        {
            // Arrange

            // Act
            var result = await _tokenService.ValidateAsync(_audience, _tokenModel);

            // Assert
            result.Should().NotBeNull();
            _tokenRepository.VerifyAll();
        }