|
| 1 | +import * as proc from 'child_process'; |
| 2 | +import https = require('https'); |
| 3 | +import * as jsonpath from 'jsonpath-plus'; |
| 4 | +import request = require('request'); |
| 5 | + |
| 6 | +import { Authenticator } from './auth'; |
| 7 | +import { User } from './config_types'; |
| 8 | + |
| 9 | +/* FIXME: maybe we can extend the User and User.authProvider type to have a proper type. |
| 10 | +Currently user.authProvider has `any` type and so we don't have a type for user.authProvider.config. |
| 11 | +We therefore define its type here |
| 12 | +*/ |
| 13 | +interface Config { |
| 14 | + expiry: string; |
| 15 | + ['cmd-args']?: string; |
| 16 | + ['cmd-path']?: string; |
| 17 | + ['token-key']: string; |
| 18 | + ['expiry-key']: string; |
| 19 | + ['access-token']?: string; |
| 20 | +} |
| 21 | +export class GoogleCloudPlatformAuth implements Authenticator { |
| 22 | + public isAuthProvider(user: User): boolean { |
| 23 | + if (!user || !user.authProvider) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + return user.authProvider.name === 'gcp'; |
| 27 | + } |
| 28 | + |
| 29 | + public async applyAuthentication( |
| 30 | + user: User, |
| 31 | + opts: request.Options | https.RequestOptions, |
| 32 | + ): Promise<void> { |
| 33 | + const token = this.getToken(user); |
| 34 | + if (token) { |
| 35 | + opts.headers!.Authorization = `Bearer ${token}`; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private getToken(user: User): string | null { |
| 40 | + const config = user.authProvider.config; |
| 41 | + if (this.isExpired(config)) { |
| 42 | + this.updateAccessToken(config); |
| 43 | + } |
| 44 | + return config['access-token']; |
| 45 | + } |
| 46 | + |
| 47 | + private isExpired(config: Config): boolean { |
| 48 | + const token = config['access-token']; |
| 49 | + const expiry = config.expiry; |
| 50 | + if (!token) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + if (!expiry) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + const expiration = Date.parse(expiry); |
| 58 | + if (expiration < Date.now()) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + private updateAccessToken(config: Config): void { |
| 65 | + let cmd = config['cmd-path']; |
| 66 | + if (!cmd) { |
| 67 | + throw new Error('Token is expired!'); |
| 68 | + } |
| 69 | + // Wrap cmd in quotes to make it cope with spaces in path |
| 70 | + cmd = `"${cmd}"`; |
| 71 | + const args = config['cmd-args']; |
| 72 | + if (args) { |
| 73 | + cmd = cmd + ' ' + args; |
| 74 | + } |
| 75 | + // TODO: Cache to file? |
| 76 | + // TODO: do this asynchronously |
| 77 | + let output: any; |
| 78 | + try { |
| 79 | + output = proc.execSync(cmd); |
| 80 | + } catch (err) { |
| 81 | + throw new Error('Failed to refresh token: ' + err.message); |
| 82 | + } |
| 83 | + |
| 84 | + const resultObj = JSON.parse(output); |
| 85 | + |
| 86 | + const tokenPathKeyInConfig = config['token-key']; |
| 87 | + const expiryPathKeyInConfig = config['expiry-key']; |
| 88 | + |
| 89 | + // Format in file is {<query>}, so slice it out and add '$' |
| 90 | + const tokenPathKey = '$' + tokenPathKeyInConfig.slice(1, -1); |
| 91 | + const expiryPathKey = '$' + expiryPathKeyInConfig.slice(1, -1); |
| 92 | + |
| 93 | + config['access-token'] = jsonpath.JSONPath(tokenPathKey, resultObj); |
| 94 | + config.expiry = jsonpath.JSONPath(expiryPathKey, resultObj); |
| 95 | + } |
| 96 | +} |
0 commit comments