Exemple #1
0
        private void Act()
        {
            var config = new JwtBindingConfiguration
            {
                SymmetricSecuritySigningKey = _symmetricSigningKey,
                Scopes            = _scopes,
                Audience          = _audience,
                Issuer            = _issuer,
                IssuerPattern     = _issuerPattern,
                AllowedIdentities = _allowedIdentities
            };

            _service.ValidateToken(
                new AuthenticationHeaderValue(_scheme, _token),
                config);
        }
        private AuthorizedModel BuildItemFromAttribute(JwtBindingAttribute arg)
        {
            var configuration = GetFunctionConfiguration(arg);

            if ((configuration.DebugConfiguration?.Enabled).GetValueOrDefault())
            {
                _logger.LogWarning("## WARNING ## - The JWT Validation Binding is running in DEBUG mode and currently returns fixed values!");
                return(new AuthorizedModel
                {
                    Name = configuration.DebugConfiguration?.Name,
                    Subject = configuration.DebugConfiguration?.Subject
                });
            }

            if (string.IsNullOrWhiteSpace(configuration.Issuer))
            {
                _logger.LogWarning("No valid issuer configured, cannot validate token");
                throw new ArgumentNullException(nameof(arg.Issuer), "The JwtBinding requires an issuer to validate JWT Tokens");
            }

            if (_http.HttpContext != null)
            {
                var authHeaderValue = _http.HttpContext.Request.Headers["Authorization"];

                if (AuthenticationHeaderValue.TryParse(authHeaderValue, out AuthenticationHeaderValue headerValue))
                {
                    _logger.LogInformation("Now validating token");

                    return(_service.ValidateToken(headerValue, configuration));
                }

                throw new AuthorizationFailedException(
                          new Exception("Authorization header is missing, add a bearer token to the header of your HTTP request")
                          );
            }

            throw new AuthorizationOperationException();
        }