@@ -4,7 +4,7 @@ import { withQuery, parsePath } from 'ufo'
44import { ofetch } from 'ofetch'
55import { defu } from 'defu'
66import { useRuntimeConfig } from '#imports'
7- import crypto from 'crypto '
7+ import { type OAuthChecks , checks } from '../../utils/security '
88
99export interface OAuthAuth0Config {
1010 /**
@@ -48,28 +48,17 @@ export interface OAuthAuth0Config {
4848 checks ?: OAuthChecks [ ]
4949}
5050
51- type OAuthChecks = 'pkce' | 'state'
5251interface OAuthConfig {
5352 config ?: OAuthAuth0Config
5453 onSuccess : ( event : H3Event , result : { user : any , tokens : any } ) => Promise < void > | void
5554 onError ?: ( event : H3Event , error : H3Error ) => Promise < void > | void
5655}
5756
58- function base64URLEncode ( str : string ) {
59- return str . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) . replace ( / = / g, '' )
60- }
61- function randomBytes ( length : number ) {
62- return crypto . randomBytes ( length ) . toString ( 'base64' )
63- }
64- function sha256 ( buffer : string ) {
65- return crypto . createHash ( 'sha256' ) . update ( buffer ) . digest ( 'base64' )
66- }
67-
6857export function auth0EventHandler ( { config, onSuccess, onError } : OAuthConfig ) {
6958 return eventHandler ( async ( event : H3Event ) => {
7059 // @ts -ignore
7160 config = defu ( config , useRuntimeConfig ( event ) . oauth ?. auth0 ) as OAuthAuth0Config
72- const { code, state } = getQuery ( event )
61+ const { code } = getQuery ( event )
7362
7463 if ( ! config . clientId || ! config . clientSecret || ! config . domain ) {
7564 const error = createError ( {
@@ -84,19 +73,7 @@ export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig) {
8473
8574 const redirectUrl = getRequestURL ( event ) . href
8675 if ( ! code ) {
87- // Initialize checks
88- const checks : Record < string , string > = { }
89- if ( config . checks ?. includes ( 'pkce' ) ) {
90- const pkceVerifier = base64URLEncode ( randomBytes ( 32 ) )
91- const pkceChallenge = base64URLEncode ( sha256 ( pkceVerifier ) )
92- checks [ 'code_challenge' ] = pkceChallenge
93- checks [ 'code_challenge_method' ] = 'S256'
94- setCookie ( event , 'nuxt-auth-util-verifier' , pkceVerifier , { maxAge : 60 * 15 , secure : true , httpOnly : true } )
95- }
96- if ( config . checks ?. includes ( 'state' ) ) {
97- checks [ 'state' ] = base64URLEncode ( randomBytes ( 32 ) )
98- setCookie ( event , 'nuxt-auth-util-state' , checks [ 'state' ] , { maxAge : 60 * 15 , secure : true , httpOnly : true } )
99- }
76+ const authParam = await checks . create ( event , config . checks ) // Initialize checks
10077 config . scope = config . scope || [ 'openid' , 'offline_access' ]
10178 if ( config . emailRequired && ! config . scope . includes ( 'email' ) ) {
10279 config . scope . push ( 'email' )
@@ -110,33 +87,18 @@ export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig) {
11087 redirect_uri : redirectUrl ,
11188 scope : config . scope . join ( ' ' ) ,
11289 audience : config . audience || '' ,
113- ...checks
90+ ...authParam
11491 } )
11592 )
11693 }
11794
11895 // Verify checks
119- const pkceVerifier = getCookie ( event , 'nuxt-auth-util-verifier' )
120- setCookie ( event , 'nuxt-auth-util-verifier' , '' , { maxAge : - 1 } )
121- const stateInCookie = getCookie ( event , 'nuxt-auth-util-state' )
122- setCookie ( event , 'nuxt-auth-util-state' , '' , { maxAge : - 1 } )
123- if ( config . checks ?. includes ( 'state' ) ) {
124- if ( ! state || ! stateInCookie ) {
125- const error = createError ( {
126- statusCode : 401 ,
127- message : 'Auth0 login failed: state is missing'
128- } )
129- if ( ! onError ) throw error
130- return onError ( event , error )
131- }
132- if ( state !== stateInCookie ) {
133- const error = createError ( {
134- statusCode : 401 ,
135- message : 'Auth0 login failed: state does not match'
136- } )
137- if ( ! onError ) throw error
138- return onError ( event , error )
139- }
96+ let checkResult
97+ try {
98+ checkResult = await checks . use ( event , config . checks )
99+ } catch ( error ) {
100+ if ( ! onError ) throw error
101+ return onError ( event , error as H3Error )
140102 }
141103
142104 const tokens : any = await ofetch (
@@ -152,7 +114,7 @@ export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig) {
152114 client_secret : config . clientSecret ,
153115 redirect_uri : parsePath ( redirectUrl ) . pathname ,
154116 code,
155- code_verifier : pkceVerifier
117+ ... checkResult
156118 }
157119 }
158120 ) . catch ( error => {
0 commit comments