@@ -22,6 +22,11 @@ export type TokenHandlerOptions = {
2222 * Set to false to disable rate limiting for this endpoint.
2323 */
2424 rateLimit ?: Partial < RateLimitOptions > | false ;
25+
26+ /**
27+ * Set to true to throw errors to the express error handler
28+ */
29+ throwErrors ?: boolean ;
2530} ;
2631
2732const TokenRequestSchema = z . object ( {
@@ -41,7 +46,7 @@ const RefreshTokenGrantSchema = z.object({
4146 resource : z . string ( ) . url ( ) . optional ( )
4247} ) ;
4348
44- export function tokenHandler ( { provider, rateLimit : rateLimitConfig } : TokenHandlerOptions ) : RequestHandler {
49+ export function tokenHandler ( { provider, rateLimit : rateLimitConfig , throwErrors } : TokenHandlerOptions ) : RequestHandler {
4550 // Nested router so we can configure middleware and restrict HTTP method
4651 const router = express . Router ( ) ;
4752
@@ -66,7 +71,7 @@ export function tokenHandler({ provider, rateLimit: rateLimitConfig }: TokenHand
6671 }
6772
6873 // Authenticate and extract client details
69- router . use ( authenticateClient ( { clientsStore : provider . clientsStore } ) ) ;
74+ router . use ( authenticateClient ( { clientsStore : provider . clientsStore , throwErrors } ) ) ;
7075
7176 router . post ( '/' , async ( req , res ) => {
7277 res . setHeader ( 'Cache-Control' , 'no-store' ) ;
@@ -140,7 +145,7 @@ export function tokenHandler({ provider, rateLimit: rateLimitConfig }: TokenHand
140145 //case "client_credentials":
141146
142147 default :
143- throw new UnsupportedGrantTypeError ( ' The grant type is not supported by this authorization server.' ) ;
148+ throw new UnsupportedGrantTypeError ( ` The grant type ' ${ grant_type } ' is not supported by this authorization server.` ) ;
144149 }
145150 } catch ( error ) {
146151 if ( error instanceof OAuthError ) {
@@ -150,6 +155,10 @@ export function tokenHandler({ provider, rateLimit: rateLimitConfig }: TokenHand
150155 const serverError = new ServerError ( 'Internal Server Error' ) ;
151156 res . status ( 500 ) . json ( serverError . toResponseObject ( ) ) ;
152157 }
158+
159+ if ( throwErrors ) {
160+ throw error ;
161+ }
153162 }
154163 } ) ;
155164
0 commit comments