1
1
import https from 'node:https' ;
2
- import { Client , Issuer } from 'openid-client' ;
2
+ import * as oidc from 'openid-client' ;
3
3
import { base64url } from 'rfc4648' ;
4
4
5
5
import { Authenticator } from './auth' ;
@@ -11,6 +11,29 @@ interface JwtObj {
11
11
signature : string ;
12
12
}
13
13
14
+ interface Token {
15
+ id_token : string ;
16
+ refresh_token : string ;
17
+ expires_at : number ;
18
+ }
19
+
20
+ interface Client {
21
+ refresh ( token : string ) : Promise < Token > ;
22
+ }
23
+
24
+ class OidcClient implements Client {
25
+ public constructor ( readonly config : oidc . Configuration ) { }
26
+
27
+ public async refresh ( token : string ) : Promise < Token > {
28
+ const newToken = await oidc . refreshTokenGrant ( this . config , token ) ;
29
+ return {
30
+ id_token : newToken . id_token ,
31
+ refresh_token : newToken . refresh_token ,
32
+ expires_at : newToken . expiresIn ( ) ,
33
+ } as Token ;
34
+ }
35
+ }
36
+
14
37
export class OpenIDConnectAuth implements Authenticator {
15
38
public static decodeJWT ( token : string ) : JwtObj | null {
16
39
const parts = token . split ( '.' ) ;
@@ -95,16 +118,16 @@ export class OpenIDConnectAuth implements Authenticator {
95
118
const newToken = await client . refresh ( user . authProvider . config [ 'refresh-token' ] ) ;
96
119
user . authProvider . config [ 'id-token' ] = newToken . id_token ;
97
120
user . authProvider . config [ 'refresh-token' ] = newToken . refresh_token ;
98
- this . currentTokenExpiration = newToken . expires_at || 0 ;
121
+ this . currentTokenExpiration = newToken . expires_at ;
99
122
}
100
123
return user . authProvider . config [ 'id-token' ] ;
101
124
}
102
125
103
126
private async getClient ( user : User ) : Promise < Client > {
104
- const oidcIssuer = await Issuer . discover ( user . authProvider . config [ 'idp-issuer-url' ] ) ;
105
- return new oidcIssuer . Client ( {
106
- client_id : user . authProvider . config [ 'client-id' ] ,
107
- client_secret : user . authProvider . config [ 'client-secret' ] ,
108
- } ) ;
127
+ const configuration = await oidc . discovery (
128
+ user . authProvider . config [ 'idp-issuer-url' ] ,
129
+ user . authProvider . config [ 'client-id' ] ,
130
+ ) ;
131
+ return new OidcClient ( configuration ) ;
109
132
}
110
133
}
0 commit comments