Skip to content

Commit d701e80

Browse files
tomlewis0atinux
andauthored
feat: add GitHub state param (#423)
Co-authored-by: Sébastien Chopin <[email protected]> Co-authored-by: Sébastien Chopin <[email protected]>
1 parent 03f67eb commit d701e80

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/runtime/server/lib/oauth/github.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
22
import { eventHandler, getQuery, sendRedirect, createError } from 'h3'
33
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
5+
import { getOAuthRedirectURL, handleAccessTokenErrorResponse, handleInvalidState, handleMissingConfiguration, handleState, requestAccessToken } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import 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

Comments
 (0)