MCPcopy Create free account
hub / github.com/authlib/authlib / validate_code_verifier

Method validate_code_verifier

authlib/oauth2/rfc7636/challenge.py:91–134  ·  view source on GitHub ↗
(self, grant, result)

Source from the content-addressed store, hash-verified

89 raise InvalidRequestError("Multiple 'code_challenge_method' in request.")
90
91 def validate_code_verifier(self, grant, result):
92 request: OAuth2Request = grant.request
93 verifier = request.form.get("code_verifier")
94
95 # public client MUST verify code challenge
96 if self.required and request.auth_method == "none" and not verifier:
97 raise InvalidRequestError("Missing 'code_verifier'")
98
99 authorization_code = request.authorization_code
100 challenge = self.get_authorization_code_challenge(authorization_code)
101
102 # ignore, it is the normal RFC6749 authorization_code request
103 if not challenge and not verifier:
104 return
105
106 # RFC 9700 Section 4.8: the authorization server MUST ensure that if
107 # there was no code_challenge in the authorization request, a request
108 # to the token endpoint containing a code_verifier is rejected.
109 if not challenge and verifier:
110 raise InvalidRequestError(
111 "The authorization request had no 'code_challenge', "
112 "but a 'code_verifier' was provided."
113 )
114
115 # challenge exists, code_verifier is required
116 if not verifier:
117 raise InvalidRequestError("Missing 'code_verifier'")
118
119 if not CODE_VERIFIER_PATTERN.match(verifier):
120 raise InvalidRequestError("Invalid 'code_verifier'")
121
122 # 4.6. Server Verifies code_verifier before Returning the Tokens
123 method = self.get_authorization_code_challenge_method(authorization_code)
124 if method is None:
125 method = self.DEFAULT_CODE_CHALLENGE_METHOD
126
127 func = self.CODE_CHALLENGE_METHODS.get(method)
128 if not func:
129 raise RuntimeError(f"No verify method for '{method}'")
130
131 # If the values are not equal, an error response indicating
132 # "invalid_grant" MUST be returned.
133 if not func(verifier, challenge):
134 raise InvalidGrantError(description="Code challenge failed.")
135
136 def get_authorization_code_challenge(self, authorization_code):
137 """Get "code_challenge" associated with this authorization code.

Callers

nothing calls this directly

Calls 5

InvalidGrantErrorClass · 0.85
InvalidRequestErrorClass · 0.50
getMethod · 0.45

Tested by

no test coverage detected