@@ -3,7 +3,14 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"
3
3
import { nanoid } from "nanoid"
4
4
import { URLSearchParams } from "url"
5
5
6
- import { Config , GitHubAccessToken , GitHubOrgMembership , GitHubUser , OAuthState , RoutePrams } from "./types"
6
+ import {
7
+ Config ,
8
+ GitHubAccessToken ,
9
+ GitHubOrgMembership ,
10
+ GitHubUser ,
11
+ OAuthState ,
12
+ RoutePrams ,
13
+ } from "./types"
7
14
8
15
export function registerGitHubOAuth ( server : FastifyInstance , config : Config ) {
9
16
const secureCookies = ! ! process . env . VERCEL_URL
@@ -59,7 +66,10 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
59
66
//
60
67
// https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#web-application-flow
61
68
//
62
- const redirectToGitHub = async ( req : FastifyRequest < RoutePrams > , res : FastifyReply ) => {
69
+ const redirectToGitHub = async (
70
+ req : FastifyRequest < RoutePrams > ,
71
+ res : FastifyReply ,
72
+ ) => {
63
73
const query = formatQueryParams ( {
64
74
client_id : config . githubClientId ,
65
75
scope : "read:user" ,
@@ -78,7 +88,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
78
88
} )
79
89
}
80
90
81
- const getGitHubAccessToken = async ( code : string ) : Promise < GitHubAccessToken > => {
91
+ const getGitHubAccessToken = async (
92
+ code : string ,
93
+ ) : Promise < GitHubAccessToken > => {
82
94
const url = urls . githubToken
83
95
const headers = {
84
96
Accept : "application/json" ,
@@ -94,7 +106,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
94
106
return data
95
107
}
96
108
97
- const getGitHubUser = async ( tokenData : GitHubAccessToken ) : Promise < GitHubUser > => {
109
+ const getGitHubUser = async (
110
+ tokenData : GitHubAccessToken ,
111
+ ) : Promise < GitHubUser > => {
98
112
const url = urls . githubUserDetails
99
113
const headers = {
100
114
Accept : "application/json" ,
@@ -118,11 +132,20 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
118
132
return data
119
133
}
120
134
121
- const retrieveState = ( req : FastifyRequest < RoutePrams > , res : FastifyReply ) => {
135
+ const retrieveState = (
136
+ req : FastifyRequest < RoutePrams > ,
137
+ res : FastifyReply ,
138
+ ) => {
122
139
const state : OAuthState = unsignCookie ( res , req . query . state || "" )
123
- const expectedState : OAuthState = unsignCookie ( res , req . cookies [ cookieNames . state ] || "" )
124
-
125
- if ( ! state ?. randomToken || state . randomToken !== expectedState ?. randomToken ) {
140
+ const expectedState : OAuthState = unsignCookie (
141
+ res ,
142
+ req . cookies [ cookieNames . state ] || "" ,
143
+ )
144
+
145
+ if (
146
+ ! state ?. randomToken ||
147
+ state . randomToken !== expectedState ?. randomToken
148
+ ) {
126
149
throw new Error ( "State mismatch" )
127
150
}
128
151
@@ -147,11 +170,17 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
147
170
server . addHook < RoutePrams > ( "preValidation" , async ( req , res ) => {
148
171
try {
149
172
if ( req . url === urls . localMembershipError ) {
150
- return denyAccess ( res , "It appears you are not a member of the required GitHub organization." )
173
+ return denyAccess (
174
+ res ,
175
+ "It appears you are not a member of the required GitHub organization." ,
176
+ )
151
177
}
152
178
153
179
if ( req . url === urls . localGenericError ) {
154
- return denyAccess ( res , "It appears that the authentication request was initiated or processed incorrectly." )
180
+ return denyAccess (
181
+ res ,
182
+ "It appears that the authentication request was initiated or processed incorrectly." ,
183
+ )
155
184
}
156
185
157
186
if ( req . url === urls . localAuthorize ) {
@@ -177,7 +206,7 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
177
206
const user = await getGitHubUser ( tokenData )
178
207
const members = await getGitHubOrgMemberships ( )
179
208
180
- if ( ! members . find ( member => member . id === user . id ) ) {
209
+ if ( ! members . find ( ( member ) => member . id === user . id ) ) {
181
210
return res . redirect ( 302 , urls . localMembershipError )
182
211
}
183
212
0 commit comments