Skip to content

Commit 4bbbc91

Browse files
committed
Redirect to dedicated URLs on error to avoid residual query parameters
1 parent 5c47232 commit 4bbbc91

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/github-oauth.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
1010

1111
const urls = {
1212
localAuthorize: "/login/oauth/authorize",
13+
localMembershipError: "/login/oauth/error-membership",
14+
localGenericError: "/login/oauth/error",
1315
githubAuthorize: "https://github.com/login/oauth/authorize",
1416
githubToken: "https://github.com/login/oauth/access_token",
1517
githubOrgMembers: `https://api.github.com/orgs/${config.githubOrgName}/members`,
@@ -144,18 +146,26 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
144146
//
145147
server.addHook<RoutePrams>("preValidation", async (req, res) => {
146148
try {
149+
if (req.url === urls.localMembershipError) {
150+
return denyAccess(res, "It appears you are not a member of the required GitHub organization.")
151+
}
152+
153+
if (req.url === urls.localGenericError) {
154+
return denyAccess(res, "It appears that the authentication request was initiated or processed incorrectly.")
155+
}
156+
157+
if (req.url === urls.localAuthorize) {
158+
return redirectToGitHub(req, res)
159+
}
160+
147161
if (req.cookies[cookieNames.state] && req.cookies[cookieNames.user]) {
148-
if (req.query.state) {
162+
if (req.query.state || req.query.code) {
149163
const state = retrieveState(req, res)
150164
return res.redirect(302, state.path)
151165
}
152166
return
153167
}
154168

155-
if (req.url === urls.localAuthorize) {
156-
return redirectToGitHub(req, res)
157-
}
158-
159169
const code = req.query.code
160170

161171
if (!code) {
@@ -168,13 +178,13 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
168178
const members = await getGitHubOrgMemberships()
169179

170180
if (!members.find(member => member.id === user.id)) {
171-
return denyAccess(res, "It appears you are not a member of the required GitHub organization.")
181+
return res.redirect(302, urls.localMembershipError)
172182
}
173183

174184
return succeed(res, user, state.path)
175185
} catch (error) {
176186
console.error(error)
177-
return denyAccess(res, "It appears that the authentication request was initiated or processed incorrectly.")
187+
return res.redirect(302, urls.localGenericError)
178188
}
179189
})
180190
}

0 commit comments

Comments
 (0)