-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathfile_auth.ts
49 lines (43 loc) · 1.6 KB
/
file_auth.ts
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
import fs = require('fs');
import https = require('https');
import request = require('request');
import { Authenticator } from './auth';
import { User } from './config_types';
export class FileAuth implements Authenticator {
private token: string | null = null;
private lastRead: Date | null = null;
public isAuthProvider(user: User): boolean {
return user.authProvider && user.authProvider.config && user.authProvider.config.tokenFile;
}
public async applyAuthentication(
user: User,
opts: request.Options | https.RequestOptions,
): Promise<void> {
if (this.token == null) {
this.refreshToken(user.authProvider.config.tokenFile);
}
if (this.isTokenExpired()) {
this.refreshToken(user.authProvider.config.tokenFile);
}
if (this.token) {
opts.headers!.Authorization = `Bearer ${this.token}`;
}
}
private refreshToken(filePath: string): void {
// TODO make this async?
this.token = fs.readFileSync(filePath).toString('utf8');
this.lastRead = new Date();
}
private isTokenExpired(): boolean {
if (this.lastRead === null) {
return true;
}
const now = new Date();
const delta = (now.getTime() - this.lastRead.getTime()) / 1000;
// For now just refresh every 60 seconds. This is imperfect since the token
// could be out of date for this time, but it is unlikely and it's also what
// the client-go library does.
// TODO: Use file notifications instead?
return delta > 60;
}
}