@@ -7,51 +7,69 @@ import { User } from './config_types';
7
7
8
8
export class CloudAuth implements Authenticator {
9
9
public isAuthProvider ( user : User ) : boolean {
10
- return user . authProvider . name === 'azure' ||
11
- user . authProvider . name === 'gcp' ;
10
+ return (
11
+ user . authProvider . name === 'azure' ||
12
+ user . authProvider . name === 'gcp'
13
+ ) ;
12
14
}
13
15
14
16
public getToken ( user : User ) : string | null {
15
17
const config = user . authProvider . config ;
16
- // This should probably be extracted as auth-provider specific plugins...
17
- let token : string = 'Bearer ' + config [ 'access-token' ] ;
18
+ if ( this . isExpired ( config ) ) {
19
+ this . updateAccessToken ( config ) ;
20
+ }
21
+ return 'Bearer ' + config [ 'access-token' ] ;
22
+ }
23
+
24
+ private isExpired ( config ) {
25
+ const token = config [ 'access-token' ]
18
26
const expiry = config . expiry ;
27
+ if ( ! token ) {
28
+ return true ;
29
+ }
30
+ if ( ! expiry ) {
31
+ return false
32
+ }
33
+
34
+ const expiration = Date . parse ( expiry ) ;
35
+ if ( expiration < Date . now ( ) ) {
36
+ return true ;
37
+ }
38
+ return false ;
39
+ }
19
40
20
- if ( expiry ) {
21
- const expiration = Date . parse ( expiry ) ;
22
- if ( expiration < Date . now ( ) ) {
23
- if ( config [ 'cmd-path' ] ) {
24
- const args = config [ 'cmd-args' ] ;
25
- // TODO: Cache to file?
26
- // TODO: do this asynchronously
27
- let result : any ;
28
- try {
29
- let cmd = config [ 'cmd-path' ] ;
30
- if ( args ) {
31
- cmd = `${ cmd } ${ args } ` ;
32
- }
33
- result = shelljs . exec ( cmd ) ;
34
- if ( result . code !== 0 ) {
35
- throw new Error ( result . stderr ) ;
36
- }
37
- } catch ( err ) {
38
- throw new Error ( 'Failed to refresh token: ' + err . message ) ;
39
- }
40
-
41
- const output = result . stdout . toString ( ) ;
42
- const resultObj = JSON . parse ( output ) ;
43
-
44
- let pathKey = config [ 'token-key' ] ;
45
- // Format in file is {<query>}, so slice it out and add '$'
46
- pathKey = '$' + pathKey . slice ( 1 , - 1 ) ;
47
-
48
- config [ 'access-token' ] = jsonpath . query ( resultObj , pathKey ) ;
49
- token = 'Bearer ' + config [ 'access-token' ] ;
50
- } else {
51
- throw new Error ( 'Token is expired!' ) ;
52
- }
41
+ private updateAccessToken ( config ) {
42
+ if ( ! config [ 'cmd-path' ] ) {
43
+ throw new Error ( 'Token is expired!' ) ;
44
+ }
45
+ const args = config [ 'cmd-args' ] ;
46
+ // TODO: Cache to file?
47
+ // TODO: do this asynchronously
48
+ let result : any ;
49
+ try {
50
+ let cmd = config [ 'cmd-path' ] ;
51
+ if ( args ) {
52
+ cmd = `${ cmd } ${ args } ` ;
53
53
}
54
+ result = shelljs . exec ( cmd ) ;
55
+ if ( result . code !== 0 ) {
56
+ throw new Error ( result . stderr ) ;
57
+ }
58
+ } catch ( err ) {
59
+ throw new Error ( 'Failed to refresh token: ' + err . message ) ;
54
60
}
55
- return token ;
61
+
62
+ const output = result . stdout . toString ( ) ;
63
+ const resultObj = JSON . parse ( output ) ;
64
+
65
+ let tokenPathKey = config [ 'token-key' ] ;
66
+
67
+ let expiryPathKey = config [ 'token-key' ] ;
68
+ // Format in file is {<query>}, so slice it out and add '$'
69
+ tokenPathKey = '$' + tokenPathKey . slice ( 1 , - 1 ) ;
70
+ expiryPathKey = '$' + expiryPathKey . slice ( 1 , - 1 ) ;
71
+
72
+ config [ 'access-token' ] = jsonpath . query ( resultObj , tokenPathKey ) ;
73
+ config [ 'expiry' ] = jsonpath . query ( resultObj , expiryPathKey ) ;
56
74
}
57
75
}
0 commit comments