forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_auth.ts
More file actions
57 lines (50 loc) · 2.16 KB
/
cloud_auth.ts
File metadata and controls
57 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// workaround for issue https://github.com/dchester/jsonpath/issues/96
import jsonpath = require('jsonpath/jsonpath.min');
import * as shelljs from 'shelljs';
import { Authenticator } from './auth';
import { User } from './config_types';
export class CloudAuth implements Authenticator {
public isAuthProvider(user: User): boolean {
return user.authProvider.name === 'azure' ||
user.authProvider.name === 'gcp';
}
public getToken(user: User): string | null {
const config = user.authProvider.config;
// This should probably be extracted as auth-provider specific plugins...
let token: string = 'Bearer ' + config['access-token'];
const expiry = config.expiry;
if (expiry) {
const expiration = Date.parse(expiry);
if (expiration < Date.now()) {
if (config['cmd-path']) {
const args = config['cmd-args'];
// TODO: Cache to file?
// TODO: do this asynchronously
let result: any;
try {
let cmd = config['cmd-path'];
if (args) {
cmd = `${cmd} ${args}`;
}
result = shelljs.exec(cmd);
if (result.code !== 0) {
throw new Error(result.stderr);
}
} catch (err) {
throw new Error('Failed to refresh token: ' + err.message);
}
const output = result.stdout.toString();
const resultObj = JSON.parse(output);
let pathKey = config['token-key'];
// Format in file is {<query>}, so slice it out and add '$'
pathKey = '$' + pathKey.slice(1, -1);
config['access-token'] = jsonpath.query(resultObj, pathKey);
token = 'Bearer ' + config['access-token'];
} else {
throw new Error('Token is expired!');
}
}
}
return token;
}
}