@@ -3,7 +3,14 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"
33import { nanoid } from "nanoid"
44import { URLSearchParams } from "url"
55
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"
714
815export function registerGitHubOAuth ( server : FastifyInstance , config : Config ) {
916 const secureCookies = ! ! process . env . VERCEL_URL
@@ -59,7 +66,10 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
5966 //
6067 // https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#web-application-flow
6168 //
62- const redirectToGitHub = async ( req : FastifyRequest < RoutePrams > , res : FastifyReply ) => {
69+ const redirectToGitHub = async (
70+ req : FastifyRequest < RoutePrams > ,
71+ res : FastifyReply ,
72+ ) => {
6373 const query = formatQueryParams ( {
6474 client_id : config . githubClientId ,
6575 scope : "read:user" ,
@@ -78,7 +88,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
7888 } )
7989 }
8090
81- const getGitHubAccessToken = async ( code : string ) : Promise < GitHubAccessToken > => {
91+ const getGitHubAccessToken = async (
92+ code : string ,
93+ ) : Promise < GitHubAccessToken > => {
8294 const url = urls . githubToken
8395 const headers = {
8496 Accept : "application/json" ,
@@ -94,7 +106,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
94106 return data
95107 }
96108
97- const getGitHubUser = async ( tokenData : GitHubAccessToken ) : Promise < GitHubUser > => {
109+ const getGitHubUser = async (
110+ tokenData : GitHubAccessToken ,
111+ ) : Promise < GitHubUser > => {
98112 const url = urls . githubUserDetails
99113 const headers = {
100114 Accept : "application/json" ,
@@ -118,11 +132,20 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
118132 return data
119133 }
120134
121- const retrieveState = ( req : FastifyRequest < RoutePrams > , res : FastifyReply ) => {
135+ const retrieveState = (
136+ req : FastifyRequest < RoutePrams > ,
137+ res : FastifyReply ,
138+ ) => {
122139 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+ ) {
126149 throw new Error ( "State mismatch" )
127150 }
128151
@@ -147,11 +170,17 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
147170 server . addHook < RoutePrams > ( "preValidation" , async ( req , res ) => {
148171 try {
149172 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+ )
151177 }
152178
153179 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+ )
155184 }
156185
157186 if ( req . url === urls . localAuthorize ) {
@@ -177,7 +206,7 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
177206 const user = await getGitHubUser ( tokenData )
178207 const members = await getGitHubOrgMemberships ( )
179208
180- if ( ! members . find ( member => member . id === user . id ) ) {
209+ if ( ! members . find ( ( member ) => member . id === user . id ) ) {
181210 return res . redirect ( 302 , urls . localMembershipError )
182211 }
183212
0 commit comments