11import { RequestHandler } from "express" ;
22import { z } from "zod" ;
3- import { OAuthRegisteredClientsStore } from "../clients.js" ;
43import { isValidUrl } from "../validation.js" ;
4+ import { OAuthServerProvider } from "../provider.js" ;
55
66export type AuthorizationHandlerOptions = {
7- /**
8- * A store used to read information about registered OAuth clients.
9- */
10- store : OAuthRegisteredClientsStore ;
7+ provider : OAuthServerProvider ;
118} ;
129
10+ // Parameters that must be validated in order to issue redirects.
1311const ClientAuthorizationParamsSchema = z . object ( {
1412 client_id : z . string ( ) ,
1513 redirect_uri : z . string ( ) . optional ( ) . refine ( ( value ) => value === undefined || isValidUrl ( value ) , { message : "redirect_uri must be a valid URL" } ) ,
1614} ) ;
1715
16+ // Parameters that must be validated for a successful authorization request. Failure can be reported to the redirect URI.
1817const RequestAuthorizationParamsSchema = z . object ( {
1918 response_type : z . literal ( "code" ) ,
2019 code_challenge : z . string ( ) ,
@@ -23,7 +22,7 @@ const RequestAuthorizationParamsSchema = z.object({
2322 state : z . string ( ) . optional ( ) ,
2423} ) ;
2524
26- export function authorizationHandler ( { store } : AuthorizationHandlerOptions ) : RequestHandler {
25+ export function authorizationHandler ( { provider } : AuthorizationHandlerOptions ) : RequestHandler {
2726 return async ( req , res ) => {
2827 if ( req . method !== "GET" && req . method !== "POST" ) {
2928 res . status ( 405 ) . end ( "Method Not Allowed" ) ;
@@ -38,7 +37,7 @@ export function authorizationHandler({ store }: AuthorizationHandlerOptions): Re
3837 return ;
3938 }
4039
41- const client = await store . getClient ( client_id ) ;
40+ const client = await provider . clientsStore . getClient ( client_id ) ;
4241 if ( ! client ) {
4342 res . status ( 400 ) . end ( "Bad Request: invalid client_id" ) ;
4443 return ;
@@ -67,8 +66,9 @@ export function authorizationHandler({ store }: AuthorizationHandlerOptions): Re
6766 return ;
6867 }
6968
69+ let requestedScopes : string [ ] = [ ] ;
7070 if ( params . scope !== undefined && client . scope !== undefined ) {
71- const requestedScopes = params . scope . split ( " " ) ;
71+ requestedScopes = params . scope . split ( " " ) ;
7272 const allowedScopes = new Set ( client . scope . split ( " " ) ) ;
7373
7474 // If any requested scope is not in the client's registered scopes, error out
@@ -83,8 +83,12 @@ export function authorizationHandler({ store }: AuthorizationHandlerOptions): Re
8383 }
8484 }
8585
86- // TODO: Store code challenge
87- // TODO: Generate authorization code
88- // TODO: Redirect to redirect_uri (handle in calling code)
86+ await provider . authorize ( {
87+ client,
88+ state : params . state ,
89+ scopes : requestedScopes ,
90+ redirectUri : redirect_uri ,
91+ codeChallenge : params . code_challenge ,
92+ } , res ) ;
8993 } ;
9094}
0 commit comments