1
1
import https = require( 'https' ) ;
2
- import { Client , ClientMetadata , Issuer } from 'openid-client' ;
2
+ import * as oidc from 'openid-client' ;
3
+ import { ClientMetadata } from 'openid-client' ;
3
4
import request = require( 'request' ) ;
4
5
import { base64url } from 'rfc4648' ;
5
6
import { TextDecoder } from 'util' ;
@@ -13,6 +14,29 @@ interface JwtObj {
13
14
signature : string ;
14
15
}
15
16
17
+ interface Token {
18
+ id_token : string ;
19
+ refresh_token : string ;
20
+ expires_at : number ;
21
+ }
22
+
23
+ interface Client {
24
+ refresh ( token : string ) : Promise < Token > ;
25
+ }
26
+
27
+ class OidcClient implements Client {
28
+ public constructor ( readonly config : oidc . Configuration ) { }
29
+
30
+ public async refresh ( token : string ) : Promise < Token > {
31
+ const newToken = await oidc . refreshTokenGrant ( this . config , token ) ;
32
+ return {
33
+ id_token : newToken . id_token ,
34
+ refresh_token : newToken . refresh_token ,
35
+ expires_at : newToken . expiresIn ( ) ,
36
+ } as Token ;
37
+ }
38
+ }
39
+
16
40
export class OpenIDConnectAuth implements Authenticator {
17
41
public static decodeJWT ( token : string ) : JwtObj | null {
18
42
const parts = token . split ( '.' ) ;
@@ -97,22 +121,24 @@ export class OpenIDConnectAuth implements Authenticator {
97
121
const newToken = await client . refresh ( user . authProvider . config [ 'refresh-token' ] ) ;
98
122
user . authProvider . config [ 'id-token' ] = newToken . id_token ;
99
123
user . authProvider . config [ 'refresh-token' ] = newToken . refresh_token ;
100
- this . currentTokenExpiration = newToken . expires_at || 0 ;
124
+ this . currentTokenExpiration = newToken . expires_at ;
101
125
}
102
126
return user . authProvider . config [ 'id-token' ] ;
103
127
}
104
128
105
129
private async getClient ( user : User ) : Promise < Client > {
106
- const oidcIssuer = await Issuer . discover ( user . authProvider . config [ 'idp-issuer-url' ] ) ;
107
130
const metadata : ClientMetadata = {
108
131
client_id : user . authProvider . config [ 'client-id' ] ,
109
132
client_secret : user . authProvider . config [ 'client-secret' ] ,
110
133
} ;
111
-
112
134
if ( ! user . authProvider . config [ 'client-secret' ] ) {
113
135
metadata . token_endpoint_auth_method = 'none' ;
114
136
}
115
-
116
- return new oidcIssuer . Client ( metadata ) ;
137
+ const configuration = await oidc . discovery (
138
+ user . authProvider . config [ 'idp-issuer-url' ] ,
139
+ user . authProvider . config [ 'client-id' ] ,
140
+ metadata ,
141
+ ) ;
142
+ return new OidcClient ( configuration ) ;
117
143
}
118
144
}
0 commit comments