This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
auth.ts
86 lines (71 loc) · 2.03 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import ifm = require('./interfaces')
export class BasicCredentialHandler implements ifm.IRequestHandler {
username: string
password: string
constructor(username: string, password: string) {
this.username = username
this.password = password
}
prepareRequest(options: any): void {
options.headers['Authorization'] =
'Basic ' +
Buffer.from(this.username + ':' + this.password).toString('base64')
}
// This handler cannot handle 401
canHandleAuthentication(response: ifm.IHttpClientResponse): boolean {
return false
}
handleAuthentication(
httpClient: ifm.IHttpClient,
requestInfo: ifm.IRequestInfo,
objs
): Promise<ifm.IHttpClientResponse> {
return null
}
}
export class BearerCredentialHandler implements ifm.IRequestHandler {
token: string
constructor(token: string) {
this.token = token
}
// currently implements pre-authorization
// TODO: support preAuth = false where it hooks on 401
prepareRequest(options: any): void {
options.headers['Authorization'] = 'Bearer ' + this.token
}
// This handler cannot handle 401
canHandleAuthentication(response: ifm.IHttpClientResponse): boolean {
return false
}
handleAuthentication(
httpClient: ifm.IHttpClient,
requestInfo: ifm.IRequestInfo,
objs
): Promise<ifm.IHttpClientResponse> {
return null
}
}
export class PersonalAccessTokenCredentialHandler
implements ifm.IRequestHandler {
token: string
constructor(token: string) {
this.token = token
}
// currently implements pre-authorization
// TODO: support preAuth = false where it hooks on 401
prepareRequest(options: any): void {
options.headers['Authorization'] =
'Basic ' + Buffer.from('PAT:' + this.token).toString('base64')
}
// This handler cannot handle 401
canHandleAuthentication(response: ifm.IHttpClientResponse): boolean {
return false
}
handleAuthentication(
httpClient: ifm.IHttpClient,
requestInfo: ifm.IRequestInfo,
objs
): Promise<ifm.IHttpClientResponse> {
return null
}
}