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