2424
2525
2626class AuthorizationCodeRequest (ClientAuthRequest ):
27- """
28- Model for the authorization code grant request parameters.
29-
30- Corresponds to AuthorizationCodeExchangeSchema in src/server/auth/handlers/token.ts
31- """
32-
3327 grant_type : Literal ["authorization_code" ]
3428 code : str = Field (..., description = "The authorization code" )
3529 code_verifier : str = Field (..., description = "PKCE code verifier" )
30+ # TODO: this should take redirect_uri
3631
3732
3833class RefreshTokenRequest (ClientAuthRequest ):
39- """
40- Model for the refresh token grant request parameters.
41-
42- Corresponds to RefreshTokenExchangeSchema in src/server/auth/handlers/token.ts
43- """
44-
4534 grant_type : Literal ["refresh_token" ]
4635 refresh_token : str = Field (..., description = "The refresh token" )
4736 scope : Optional [str ] = Field (None , description = "Optional scope parameter" )
@@ -54,48 +43,25 @@ class TokenRequest(RootModel):
5443 ]
5544
5645
57- # TokenRequest = RootModel(Annotated[Union[AuthorizationCodeRequest, RefreshTokenRequest], Field(discriminator="grant_type")])
58-
5946
6047def create_token_handler (
6148 provider : OAuthServerProvider , client_authenticator : ClientAuthenticator
6249) -> Callable :
63- """
64- Create a handler for the OAuth 2.0 Token endpoint.
65-
66- Corresponds to tokenHandler in src/server/auth/handlers/token.ts
67-
68- Args:
69- provider: The OAuth server provider
70-
71- Returns:
72- A Starlette endpoint handler function
73- """
74-
7550 async def token_handler (request : Request ):
76- """
77- Handler for the OAuth 2.0 Token endpoint.
78-
79- Args:
80- request: The Starlette request
81-
82- Returns:
83- JSON response with tokens or error
84- """
85- # Parse request body as form data or JSON
86- content_type = request .headers .get ("Content-Type" , "" )
87-
8851 try :
8952 token_request = TokenRequest .model_validate_json (await request .body ()).root
9053 except ValidationError as e :
9154 raise InvalidRequestError (f"Invalid request body: { e } " )
9255 client_info = await client_authenticator (token_request )
9356
57+ if token_request .grant_type not in client_info .grant_types :
58+ raise InvalidRequestError (f"Unsupported grant type (supported grant types are { client_info .grant_types } )" )
59+
9460 tokens : OAuthTokens
9561
9662 match token_request :
9763 case AuthorizationCodeRequest ():
98- # TODO: verify that the redirect URIs match; does the client actually provide this?
64+ # TODO: verify that the redirect URIs match
9965 # see https://datatracker.ietf.org/doc/html/rfc6749#section-10.6
10066 # TODO: enforce TTL on the authorization code
10167
0 commit comments