-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathoauth-service.ts
115 lines (95 loc) · 3.67 KB
/
oauth-service.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
import { OAuthConfig, OAuthProtoService, UserProfile } from './api'
import { FormService } from './form'
import { KUBERNETES_MASTER_KIND, PATH_OSCONSOLE_CLIENT_CONFIG, log } from './globals'
import { DEFAULT_HAWTIO_MODE, DEFAULT_HAWTIO_NAMESPACE, HAWTIO_MODE_KEY, HAWTIO_NAMESPACE_KEY } from './metadata'
import { OSOAuthService } from './openshift'
import { fetchPath, relToAbsUrl } from './utils'
class OAuthService {
private userProfile: UserProfile = new UserProfile()
private readonly config: Promise<OAuthConfig | null>
private readonly protoService: Promise<OAuthProtoService | null>
constructor() {
log.debug('Initialising OAuth Service')
this.config = this.loadOAuthConfig()
this.protoService = this.processConfig()
}
private async loadOAuthConfig(): Promise<OAuthConfig | null> {
return fetchPath<OAuthConfig | null>(PATH_OSCONSOLE_CLIENT_CONFIG, {
success: (data: string) => {
log.debug('Loaded', PATH_OSCONSOLE_CLIENT_CONFIG, ':', data)
return JSON.parse(data)
},
error: err => {
this.userProfile.setError(err)
return null
},
})
}
private async processConfig(): Promise<OAuthProtoService | null> {
const config = await this.config
if (!config) {
this.userProfile.setError(new Error('Cannot find the osconsole configuration'))
return null
}
log.debug('OAuth config to be processed: ', config)
log.debug('Adding master uri to profile')
this.userProfile.setMasterUri(relToAbsUrl(config.master_uri || '/master'))
this.userProfile.setMasterKind(config.master_kind || KUBERNETES_MASTER_KIND)
log.debug('Adding hawtio-mode to profile metadata')
const hawtioMode = config.hawtio?.mode || DEFAULT_HAWTIO_MODE
this.userProfile.addMetadata(HAWTIO_MODE_KEY, hawtioMode)
if (hawtioMode !== DEFAULT_HAWTIO_MODE)
this.userProfile.addMetadata(HAWTIO_NAMESPACE_KEY, config.hawtio?.namespace || DEFAULT_HAWTIO_NAMESPACE)
let protoService: OAuthProtoService | null = null
if (config.form) {
protoService = new FormService(config.form, this.userProfile)
} else if (config.openshift) {
protoService = new OSOAuthService(config.openshift, this.userProfile)
}
if (!protoService) {
this.userProfile.setError(new Error('Cannot initialise service as no protocol service can be initialised'))
}
return protoService
}
private async isProtoServiceLoggedIn(): Promise<boolean> {
const protoService = await this.protoService
if (!protoService) {
return false
}
if (this.userProfile.hasError()) {
log.debug('Cannot login as user profile has error: ', this.userProfile.getError())
return false
}
return await protoService.isLoggedIn()
}
/**
* Service has a working protocol service delegate
* but not necessarily logged in yet
*/
async isActive(): Promise<boolean> {
const protoService = await this.protoService
return protoService !== null
}
/**
* Service has a working protocol service and
* fully logged-in
*/
async isLoggedIn(): Promise<boolean> {
const protoServiceActive = await this.isProtoServiceLoggedIn()
return protoServiceActive && this.userProfile.isActive()
}
getUserProfile(): UserProfile {
return this.userProfile
}
async registerUserHooks(): Promise<void> {
log.debug('Registering oAuth user hooks')
const protoService = await this.protoService
const loggedIn = (await protoService?.isLoggedIn()) ?? false
if (!loggedIn) {
log.debug('Cannot register user hooks as OAuth Protocol Service is not logged-in')
return
}
protoService?.registerUserHooks()
}
}
export const oAuthService = new OAuthService()