@@ -34,20 +34,42 @@ class GithubSSO extends SSOBase {
3434 scope : [ 'user:email' ]
3535 } ,
3636 async ( accessToken : string , refreshToken : string , profile : Profile , done : any ) => {
37- // Fetch emails from GitHub API using the access token.
38- const emailResponse = await fetch ( 'https://api.github.com/user/emails' , {
39- headers : {
40- Authorization : `token ${ accessToken } ` ,
41- 'User-Agent' : 'Node.js'
37+ try {
38+ // Fetch emails from GitHub API using the access token.
39+ const emailResponse = await fetch ( 'https://api.github.com/user/emails' , {
40+ headers : {
41+ Authorization : `token ${ accessToken } ` ,
42+ 'User-Agent' : 'Node.js'
43+ }
44+ } )
45+ if ( ! emailResponse . ok ) {
46+ return done (
47+ {
48+ name : 'SSO_LOGIN_FAILED' ,
49+ message : `Failed to fetch emails from GitHub: ${ emailResponse . status } ${ emailResponse . statusText } `
50+ } ,
51+ undefined
52+ )
4253 }
43- } )
44- const emails = await emailResponse . json ( )
45- // Look for a verified primary email.
46- let primaryEmail = emails . find ( ( email : any ) => email . primary && email . verified ) ?. email
47- if ( ! primaryEmail && Array . isArray ( emails ) && emails . length > 0 ) {
48- primaryEmail = emails [ 0 ] . email
54+ const emails = await emailResponse . json ( )
55+ if ( ! Array . isArray ( emails ) ) {
56+ return done (
57+ { name : 'SSO_LOGIN_FAILED' , message : 'Unexpected response from GitHub emails API' } ,
58+ undefined
59+ )
60+ }
61+ // Look for a verified primary email.
62+ let primaryEmail = emails . find ( ( email : any ) => email . primary && email . verified ) ?. email
63+ if ( ! primaryEmail && emails . length > 0 ) {
64+ primaryEmail = emails [ 0 ] . email
65+ }
66+ return this . verifyAndLogin ( this . app , primaryEmail , done , profile , accessToken , refreshToken )
67+ } catch ( error ) {
68+ return done (
69+ { name : 'SSO_LOGIN_FAILED' , message : 'Failed to complete GitHub authentication' } ,
70+ undefined
71+ )
4972 }
50- return this . verifyAndLogin ( this . app , primaryEmail , done , profile , accessToken , refreshToken )
5173 }
5274 )
5375 )
@@ -115,6 +137,9 @@ class GithubSSO extends SSOBase {
115137 code : 'dummy_code_for_testing'
116138 } )
117139 } )
140+ if ( ! response . ok ) {
141+ return { error : `GitHub API error: ${ response . status } ${ response . statusText } ` }
142+ }
118143 const data = await response . json ( )
119144 if ( data . error === 'bad_verification_code' ) {
120145 return { message : 'ClientID and clientSecret are valid.' }
@@ -143,6 +168,9 @@ class GithubSSO extends SSOBase {
143168 refresh_token : currentRefreshToken
144169 } )
145170 } )
171+ if ( ! response . ok ) {
172+ return { error : `GitHub token refresh failed: ${ response . status } ${ response . statusText } ` }
173+ }
146174 const data = await response . json ( )
147175 if ( data . error || ! data . access_token ) {
148176 return { error : 'Failed to get refreshToken from Github.' }
0 commit comments