@@ -70,7 +70,8 @@ export class GenericAuthProvider implements AuthProvider {
70
70
71
71
@postConstruct ( )
72
72
init ( ) {
73
- this . strategy = new GenericOAuth2Strategy ( this . strategyName , { ...this . defaultStrategyOptions } , this . verify . bind ( this ) ) ;
73
+ this . strategy = new GenericOAuth2Strategy ( this . strategyName , { ...this . defaultStrategyOptions } ,
74
+ async ( req , accessToken , refreshToken , tokenResponse , _profile , done ) => await this . verify ( req , accessToken , refreshToken , tokenResponse , _profile , done ) ) ;
74
75
this . initAuthUserSetup ( ) ;
75
76
log . info ( `(${ this . strategyName } ) Initialized.` , { defaultStrategyOptions : this . defaultStrategyOptions } ) ;
76
77
}
@@ -251,16 +252,16 @@ export class GenericAuthProvider implements AuthProvider {
251
252
* - (3) the result of the "verify" function is first handled by passport internally and then passed to the
252
253
* callback from the `passport.authenticate` call (1)
253
254
*/
254
- readonly callback : express . RequestHandler = async ( request , response , next ) = > {
255
+ async callback ( request : express . Request , response : express . Response , next : express . NextFunction ) : Promise < void > {
255
256
const authProviderId = this . authProviderId ;
256
257
const strategyName = this . strategyName ;
257
258
const clientInfo = getRequestingClientInfo ( request ) ;
258
- const cxt = LogContext . from ( { user : request . user , request } ) ;
259
+ const cxt = LogContext . from ( { user : request . user } ) ;
259
260
if ( response . headersSent ) {
260
261
log . warn ( cxt , `(${ strategyName } ) Callback called repeatedly.` , { request, clientInfo } ) ;
261
262
return ;
262
263
}
263
- log . info ( cxt , `(${ strategyName } ) OAuth2 callback call. ` , { clientInfo, authProviderId, requestUrl : request . originalUrl , session : request . session } ) ;
264
+ log . info ( cxt , `(${ strategyName } ) OAuth2 callback call. ` , { clientInfo, authProviderId, requestUrl : request . originalUrl , request } ) ;
264
265
265
266
const isAlreadyLoggedIn = request . isAuthenticated ( ) && User . is ( request . user ) ;
266
267
const authFlow = AuthFlow . get ( request . session ) ;
@@ -279,7 +280,7 @@ export class GenericAuthProvider implements AuthProvider {
279
280
return ;
280
281
}
281
282
282
- const defaultLogPayload = { authFlow, clientInfo, authProviderId } ;
283
+ const defaultLogPayload = { authFlow, clientInfo, authProviderId, request } ;
283
284
284
285
// check OAuth2 errors
285
286
const error = new URL ( formatURL ( { protocol : request . protocol , host : request . get ( 'host' ) , pathname : request . originalUrl } ) ) . searchParams . get ( "error" ) ;
@@ -296,13 +297,15 @@ export class GenericAuthProvider implements AuthProvider {
296
297
let result : Parameters < VerifyCallback > ;
297
298
try {
298
299
result = await new Promise ( ( resolve ) => {
299
- passport . authenticate ( this . strategy as any , ( ...params : Parameters < VerifyCallback > ) => resolve ( params ) ) ( request , response , next ) ;
300
+ const authenticate = passport . authenticate ( this . strategy as any , ( ...params : Parameters < VerifyCallback > ) => resolve ( params ) ) ;
301
+ authenticate ( request , response , next ) ;
300
302
} )
301
303
} catch ( error ) {
302
304
response . redirect ( this . getSorryUrl ( `OAuth2 error. (${ error } )` ) ) ;
303
305
return ;
304
306
}
305
307
const [ err , user , flowContext ] = result ;
308
+
306
309
/*
307
310
* (3) this callback function is called after the "verify" function as the final step in the authentication process in passport.
308
311
*
@@ -341,15 +344,15 @@ export class GenericAuthProvider implements AuthProvider {
341
344
342
345
if ( TosFlow . WithIdentity . is ( flowContext ) ) {
343
346
if ( User . is ( request . user ) ) {
344
- log . error ( context , `(${ strategyName } ) Invariant violated. Unexpected user.` , { ...defaultLogPayload , session : request . session } ) ;
347
+ log . error ( context , `(${ strategyName } ) Invariant violated. Unexpected user.` , { ...defaultLogPayload , ... defaultLogPayload } ) ;
345
348
}
346
349
}
347
350
348
351
if ( TosFlow . WithIdentity . is ( flowContext ) || ( TosFlow . WithUser . is ( flowContext ) && flowContext . termsAcceptanceRequired ) ) {
349
352
350
353
// This is the regular path on sign up. We just went through the OAuth2 flow but didn't create a Gitpod
351
354
// account yet, as we require to accept the terms first.
352
- log . info ( context , `(${ strategyName } ) Redirect to /api/tos` , { info : flowContext , session : request . session } ) ;
355
+ log . info ( context , `(${ strategyName } ) Redirect to /api/tos` , { info : flowContext , ... defaultLogPayload } ) ;
353
356
354
357
// attach the sign up info to the session, in order to proceed after acceptance of terms
355
358
await TosFlow . attach ( request . session ! , flowContext ) ;
@@ -358,7 +361,7 @@ export class GenericAuthProvider implements AuthProvider {
358
361
return ;
359
362
} else {
360
363
const { user, elevateScopes } = flowContext as TosFlow . WithUser ;
361
- log . info ( context , `(${ strategyName } ) Directly log in and proceed.` , { info : flowContext , session : request . session } ) ;
364
+ log . info ( context , `(${ strategyName } ) Directly log in and proceed.` , { info : flowContext , ... defaultLogPayload } ) ;
362
365
363
366
// Complete login
364
367
const { host, returnTo } = authFlow ;
@@ -386,9 +389,6 @@ export class GenericAuthProvider implements AuthProvider {
386
389
let currentGitpodUser : User | undefined = User . is ( req . user ) ? req . user : undefined ;
387
390
let candidate : Identity ;
388
391
389
- const fail = ( err : any ) => done ( err , currentGitpodUser || candidate , flowContext ) ;
390
- const complete = ( ) => done ( undefined , currentGitpodUser || candidate , flowContext ) ;
391
-
392
392
const isIdentityLinked = ( user : User , candidate : Identity ) => user . identities . some ( i => Identity . equals ( i , candidate ) ) ;
393
393
394
394
try {
@@ -445,10 +445,10 @@ export class GenericAuthProvider implements AuthProvider {
445
445
isBlocked
446
446
}
447
447
}
448
- complete ( )
448
+ done ( undefined , currentGitpodUser || candidate , flowContext ) ;
449
449
} catch ( err ) {
450
450
log . error ( `(${ strategyName } ) Exception in verify function` , err , { ...defaultLogPayload , err, authFlow } ) ;
451
- fail ( err ) ;
451
+ done ( err , undefined ) ;
452
452
}
453
453
}
454
454
0 commit comments