@@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
22import { eventHandler , getQuery , sendRedirect , createError } from 'h3'
33import { withQuery } from 'ufo'
44import { defu } from 'defu'
5- import { handleMissingConfiguration , handleAccessTokenErrorResponse , getOAuthRedirectURL , requestAccessToken } from '../utils'
5+ import { getOAuthRedirectURL , handleAccessTokenErrorResponse , handleInvalidState , handleMissingConfiguration , handleState , requestAccessToken } from '../utils'
66import { useRuntimeConfig } from '#imports'
77import type { OAuthConfig } from '#auth-utils'
88
@@ -114,7 +114,7 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
114114 authorizationParams : { } ,
115115 } ) as OAuthGitHubConfig
116116
117- const query = getQuery < { code ?: string , error ?: string } > ( event )
117+ const query = getQuery < { code ?: string , error ?: string , state ?: string } > ( event )
118118
119119 if ( query . error ) {
120120 const error = createError ( {
@@ -131,6 +131,7 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
131131 }
132132
133133 const redirectURL = config . redirectURL || getOAuthRedirectURL ( event )
134+ const state = await handleState ( event )
134135
135136 if ( ! query . code ) {
136137 config . scope = config . scope || [ ]
@@ -144,11 +145,16 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
144145 client_id : config . clientId ,
145146 redirect_uri : redirectURL ,
146147 scope : config . scope . join ( ' ' ) ,
148+ state,
147149 ...config . authorizationParams ,
148150 } ) ,
149151 )
150152 }
151153
154+ if ( query . state !== state ) {
155+ return handleInvalidState ( event , 'github' , onError )
156+ }
157+
152158 const tokens = await requestAccessToken ( config . tokenURL as string , {
153159 body : {
154160 grant_type : 'authorization_code' ,
0 commit comments