@@ -66,12 +66,8 @@ export class GenericAuthProvider implements AuthProvider {
66
66
@inject ( AuthProviderService ) protected readonly authProviderService : AuthProviderService ;
67
67
@inject ( LoginCompletionHandler ) protected readonly loginCompletionHandler : LoginCompletionHandler ;
68
68
69
- protected strategy : GenericOAuth2Strategy ;
70
-
71
69
@postConstruct ( )
72
70
init ( ) {
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 ) ) ;
75
71
this . initAuthUserSetup ( ) ;
76
72
log . info ( `(${ this . strategyName } ) Initialized.` , { defaultStrategyOptions : this . defaultStrategyOptions } ) ;
77
73
}
@@ -131,10 +127,15 @@ export class GenericAuthProvider implements AuthProvider {
131
127
protected readAuthUserSetup ?: ( accessToken : string , tokenResponse : object ) => Promise < AuthUserSetup > ;
132
128
133
129
authorize ( req : express . Request , res : express . Response , next : express . NextFunction , scope ?: string [ ] ) : void {
134
- const handler = passport . authenticate ( this . strategy as any , { ...this . defaultStrategyOptions , ...{ scope } } ) ;
130
+ const handler = passport . authenticate ( this . getStrategy ( ) as any , { ...this . defaultStrategyOptions , ...{ scope } } ) ;
135
131
handler ( req , res , next ) ;
136
132
}
137
133
134
+ protected getStrategy ( ) {
135
+ return new GenericOAuth2Strategy ( this . strategyName , { ...this . defaultStrategyOptions } ,
136
+ async ( req , accessToken , refreshToken , tokenResponse , _profile , done ) => await this . verify ( req , accessToken , refreshToken , tokenResponse , _profile , done ) ) ;
137
+ }
138
+
138
139
async refreshToken ( user : User ) {
139
140
log . info ( `(${ this . strategyName } ) Token to be refreshed.` , { userId : user . id } ) ;
140
141
const { authProviderId } = this ;
@@ -152,7 +153,7 @@ export class GenericAuthProvider implements AuthProvider {
152
153
}
153
154
try {
154
155
const refreshResult = await new Promise < { access_token : string , refresh_token : string , result : any } > ( ( resolve , reject ) => {
155
- this . strategy . requestNewAccessToken ( refreshToken , { } , ( error , access_token , refresh_token , result ) => {
156
+ this . getStrategy ( ) . requestNewAccessToken ( refreshToken , { } , ( error , access_token , refresh_token , result ) => {
156
157
if ( error ) {
157
158
reject ( error ) ;
158
159
return ;
@@ -297,7 +298,7 @@ export class GenericAuthProvider implements AuthProvider {
297
298
let result : Parameters < VerifyCallback > ;
298
299
try {
299
300
result = await new Promise ( ( resolve ) => {
300
- const authenticate = passport . authenticate ( this . strategy as any , ( ...params : Parameters < VerifyCallback > ) => resolve ( params ) ) ;
301
+ const authenticate = passport . authenticate ( this . getStrategy ( ) as any , ( ...params : Parameters < VerifyCallback > ) => resolve ( params ) ) ;
301
302
authenticate ( request , response , next ) ;
302
303
} )
303
304
} catch ( error ) {
0 commit comments