@@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
2
2
import { eventHandler , getQuery , sendRedirect , createError } from 'h3'
3
3
import { withQuery } from 'ufo'
4
4
import { defu } from 'defu'
5
- import { handleMissingConfiguration , handleAccessTokenErrorResponse , getOAuthRedirectURL , requestAccessToken } from '../utils'
5
+ import { getOAuthRedirectURL , handleAccessTokenErrorResponse , handleInvalidState , handleMissingConfiguration , handleState , requestAccessToken } from '../utils'
6
6
import { useRuntimeConfig } from '#imports'
7
7
import type { OAuthConfig } from '#auth-utils'
8
8
@@ -114,7 +114,7 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
114
114
authorizationParams : { } ,
115
115
} ) as OAuthGitHubConfig
116
116
117
- const query = getQuery < { code ?: string , error ?: string } > ( event )
117
+ const query = getQuery < { code ?: string , error ?: string , state ?: string } > ( event )
118
118
119
119
if ( query . error ) {
120
120
const error = createError ( {
@@ -131,6 +131,7 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
131
131
}
132
132
133
133
const redirectURL = config . redirectURL || getOAuthRedirectURL ( event )
134
+ const state = await handleState ( event )
134
135
135
136
if ( ! query . code ) {
136
137
config . scope = config . scope || [ ]
@@ -144,11 +145,16 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
144
145
client_id : config . clientId ,
145
146
redirect_uri : redirectURL ,
146
147
scope : config . scope . join ( ' ' ) ,
148
+ state,
147
149
...config . authorizationParams ,
148
150
} ) ,
149
151
)
150
152
}
151
153
154
+ if ( query . state !== state ) {
155
+ return handleInvalidState ( event , 'github' , onError )
156
+ }
157
+
152
158
const tokens = await requestAccessToken ( config . tokenURL as string , {
153
159
body : {
154
160
grant_type : 'authorization_code' ,
0 commit comments