Exemplo n.º 1
0
        private async Task <OpenIdConnectMessage> ValidateAuthorizationCode(
            IDictionary <string, string[]> requestParameters,
            string clientId,
            AuthorizationGrant consentGrant)
        {
            if (!(consentGrant.Token is AuthorizationCode code))
            {
                throw new InvalidOperationException("Granted token must be an authorization code.");
            }

            var(redirectUri, redirectUriError) = RequestParametersHelper.ValidateOptionalParameterIsUnique(requestParameters, OpenIdConnectParameterNames.RedirectUri, _errorProvider);
            if (redirectUriError != null)
            {
                return(redirectUriError);
            }

            var tokenRedirectUri = code.RedirectUri;

            if (redirectUri == null && tokenRedirectUri != null)
            {
                return(_errorProvider.MissingRequiredParameter(OpenIdConnectParameterNames.RedirectUri));
            }

            if (!string.Equals(redirectUri, tokenRedirectUri, StringComparison.Ordinal))
            {
                return(_errorProvider.MismatchedRedirectUrl(redirectUri));
            }

            var resolution = await _redirectUriValidator.ResolveRedirectUriAsync(clientId, redirectUri);

            if (!resolution.IsValid)
            {
                return(_errorProvider.InvalidRedirectUri(redirectUri));
            }

            if (code.CodeChallenge != null)
            {
                if (!ProofOfKeyForCodeExchangeChallengeMethods.SHA256.Equals(code.CodeChallengeMethod, StringComparison.Ordinal))
                {
                    throw new InvalidOperationException("Unsupported code challenge method.");
                }

                var(verifier, verifierError) = RequestParametersHelper.ValidateParameterIsUnique(requestParameters, ProofOfKeyForCodeExchangeParameterNames.CodeVerifier, _errorProvider);
                if (verifierError != null)
                {
                    return(verifierError);
                }

                // code-verifier = [a-zA-Z0-9\-._~]{43,128}
                if (verifier.Length < 43 || verifier.Length > 128)
                {
                    return(_errorProvider.InvalidCodeVerifier());
                }

                for (var i = 0; i < verifier.Length; i++)
                {
                    if (verifier[i] > 127 || !ValidCodeVerifierCharacters[verifier[i]])
                    {
                        return(_errorProvider.InvalidCodeVerifier());
                    }
                }

                if (!string.Equals(code.CodeChallenge, GetComputedChallenge(verifier), StringComparison.Ordinal))
                {
                    return(_errorProvider.InvalidCodeVerifier());
                }
            }

            return(null);
        }