-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapi.ts
131 lines (105 loc) · 2.96 KB
/
api.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { FormConfig } from './form'
import { KUBERNETES_MASTER_KIND, OPENSHIFT_MASTER_KIND, log } from './globals'
import { oAuthService } from './oauth-service'
import { OpenShiftOAuthConfig } from './openshift'
export interface OAuthConfig {
master_uri?: string
master_kind: string
hawtio?: Hawtio
form?: FormConfig
openshift?: OpenShiftOAuthConfig
token?: string
}
export interface Hawtio {
mode: string
namespace?: string
}
export interface OAuthProtoService {
isLoggedIn(): Promise<boolean>
registerUserHooks(): void
}
export class UserProfile {
// Type of oauth is the profile, eg. openshift, form
private oAuthType = 'unknown'
private masterUri?: string
private masterKind?: string
private token?: string
private error: Error | null = null
private metadata: Record<string, unknown> = {}
getOAuthType() {
return this.oAuthType
}
setOAuthType(oAuthType: string) {
this.oAuthType = oAuthType
}
isActive(): boolean {
return this.hasToken() || this.hasError()
}
hasToken(): boolean {
return this.getToken().length > 0
}
getToken(): string {
return this.token ?? ''
}
setToken(token: string) {
this.token = token
}
getMasterUri(): string {
return this.masterUri ?? ''
}
setMasterUri(masterUri: string) {
this.masterUri = masterUri
}
getMasterKind(): string {
return this.masterKind ?? ''
}
setMasterKind(masterKind: string) {
const ucType = masterKind.toUpperCase()
if (ucType === KUBERNETES_MASTER_KIND || ucType === OPENSHIFT_MASTER_KIND) this.masterKind = ucType
else {
log.warn(`Unknown value set for master_kind in config (${masterKind}). Defaulting master kind to kubernetes`)
this.masterKind = KUBERNETES_MASTER_KIND
}
}
hasError() {
return this.error !== null
}
getError() {
return this.error
}
setError(error: Error) {
this.error = new Error('Openshift OAuth Error', { cause: error })
log.error(error)
}
addMetadata<T>(key: string, value: T) {
this.metadata[key] = value
}
getMetadata(): Record<string, unknown> {
return this.metadata
}
metadataValue<T>(key: string) {
return this.metadata[key] as T
}
}
let userProfile: UserProfile | null = null
async function findUserProfile(): Promise<UserProfile> {
const loggedIn = await oAuthService.isLoggedIn()
if (loggedIn) {
log.debug('Active Auth plugin:', oAuthService.getUserProfile().getOAuthType())
return oAuthService.getUserProfile()
} else {
return Promise.reject('No user profile is yet available')
}
}
export async function getActiveProfile(): Promise<UserProfile> {
if (!userProfile) {
log.debug("Finding 'userProfile' from the active OAuth plugins")
userProfile = await findUserProfile()
}
return userProfile
}
export function getOAuthType(): string | null {
const profile: UserProfile = !userProfile ? oAuthService.getUserProfile() : userProfile
if (!profile) return null
return profile.getOAuthType()
}