Skip to content

Commit 6d24efd

Browse files
committed
refactor(oauth): shuffling of location of api type declarations in oauth plugin
`globals.ts` should keep only global constants used within a package, so let's locate common types in `api.ts` or `types.ts`. Also, moved one orphaned util function to a more proper place.
1 parent fb8924f commit 6d24efd

File tree

10 files changed

+124
-124
lines changed

10 files changed

+124
-124
lines changed

packages/kubernetes-api/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const registerK8Api = async (): Promise<boolean> => {
66
log.debug('Awaiting registering of OAuth')
77
oAuthInit()
88

9-
log.debug('OAuth registered - getting active profile')
9+
log.debug('OAuth registered - getting active profile')
1010
return await k8Init()
1111
}
1212

packages/management-api/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ManagementService } from './management-service'
2-
import { log } from './globals'
31
import { isK8ApiRegistered } from '@hawtio/online-kubernetes-api'
2+
import { log } from './globals'
3+
import { ManagementService } from './management-service'
44

55
export const mgmtService = new ManagementService()
66

packages/oauth/src/api.ts

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,106 @@
1-
import { log, UserProfile } from './globals'
1+
import { FormConfig } from './form'
2+
import { KUBERNETES_MASTER_KIND, OPENSHIFT_MASTER_KIND, log } from './globals'
23
import { oAuthService } from './oauth-service'
4+
import { OpenShiftOAuthConfig } from './openshift'
5+
6+
export interface OAuthConfig {
7+
master_uri?: string
8+
master_kind: string
9+
hawtio?: Hawtio
10+
form?: FormConfig
11+
openshift?: OpenShiftOAuthConfig
12+
token?: string
13+
}
14+
15+
export interface Hawtio {
16+
mode: string
17+
namespace?: string
18+
}
19+
20+
export interface OAuthProtoService {
21+
isLoggedIn(): Promise<boolean>
22+
registerUserHooks(): void
23+
}
24+
25+
export class UserProfile {
26+
// Type of oauth is the profile, eg. openshift, form
27+
private oAuthType = 'unknown'
28+
private masterUri?: string
29+
private masterKind?: string
30+
private token?: string
31+
private error: Error | null = null
32+
private metadata: Record<string, unknown> = {}
33+
34+
getOAuthType() {
35+
return this.oAuthType
36+
}
37+
38+
setOAuthType(oAuthType: string) {
39+
this.oAuthType = oAuthType
40+
}
41+
42+
isActive(): boolean {
43+
return this.hasToken() || this.hasError()
44+
}
45+
46+
hasToken(): boolean {
47+
return this.getToken().length > 0
48+
}
49+
50+
getToken(): string {
51+
return this.token ?? ''
52+
}
53+
54+
setToken(token: string) {
55+
this.token = token
56+
}
57+
58+
getMasterUri(): string {
59+
return this.masterUri ?? ''
60+
}
61+
62+
setMasterUri(masterUri: string) {
63+
this.masterUri = masterUri
64+
}
65+
66+
getMasterKind(): string {
67+
return this.masterKind ?? ''
68+
}
69+
70+
setMasterKind(masterKind: string) {
71+
const ucType = masterKind.toUpperCase()
72+
if (ucType === KUBERNETES_MASTER_KIND || ucType === OPENSHIFT_MASTER_KIND) this.masterKind = ucType
73+
else {
74+
log.warn(`Unknown value set for master_kind in config (${masterKind}). Defaulting master kind to kubernetes`)
75+
this.masterKind = KUBERNETES_MASTER_KIND
76+
}
77+
}
78+
79+
hasError() {
80+
return this.error !== null
81+
}
82+
83+
getError() {
84+
return this.error
85+
}
86+
87+
setError(error: Error) {
88+
this.error = new Error('Openshift OAuth Error', { cause: error })
89+
log.error(error)
90+
}
91+
92+
addMetadata<T>(key: string, value: T) {
93+
this.metadata[key] = value
94+
}
95+
96+
getMetadata(): Record<string, unknown> {
97+
return this.metadata
98+
}
99+
100+
metadataValue<T>(key: string) {
101+
return this.metadata[key] as T
102+
}
103+
}
3104

4105
let userProfile: UserProfile | null = null
5106

packages/oauth/src/form/form-service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { PUBLIC_USER, userService } from '@hawtio/react'
22
import * as fetchIntercept from 'fetch-intercept'
33
import $ from 'jquery'
44
import { jwtDecode } from 'jwt-decode'
5-
import { relToAbsUrl } from 'src/utils/utils'
6-
import { OAuthProtoService, OPENSHIFT_MASTER_KIND, UserProfile, log } from '../globals'
5+
import { OAuthProtoService, UserProfile } from '../api'
6+
import { OPENSHIFT_MASTER_KIND, log } from '../globals'
77
import {
88
FetchOptions,
99
fetchPath,
1010
getCookie,
1111
joinPaths,
1212
logoutRedirect,
1313
redirect,
14+
relToAbsUrl,
1415
secureDispose,
1516
secureRetrieve,
1617
} from '../utils'

packages/oauth/src/globals.ts

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { Logger } from '@hawtio/react'
2-
import { FormConfig } from './form'
3-
import { OpenShiftOAuthConfig } from './openshift'
42

53
export const log = Logger.get('hawtio-oauth')
64
export const PATH_OSCONSOLE_CLIENT_CONFIG = 'osconsole/config.json'
@@ -9,102 +7,3 @@ export const LOGOUT_ENDPOINT = '/auth/logout'
97
// Kinds identified for the master cluster
108
export const OPENSHIFT_MASTER_KIND = 'OPENSHIFT'
119
export const KUBERNETES_MASTER_KIND = 'KUBERNETES'
12-
13-
export interface OAuthConfig {
14-
master_uri?: string
15-
master_kind: string
16-
hawtio?: Hawtio
17-
form?: FormConfig
18-
openshift?: OpenShiftOAuthConfig
19-
token?: string
20-
}
21-
22-
export interface Hawtio {
23-
mode: string
24-
namespace?: string
25-
}
26-
27-
export interface OAuthProtoService {
28-
isLoggedIn(): Promise<boolean>
29-
registerUserHooks(): void
30-
}
31-
32-
export class UserProfile {
33-
// Type of oauth is the profile, eg. openshift, form
34-
private oAuthType = 'unknown'
35-
private masterUri?: string
36-
private masterKind?: string
37-
private token?: string
38-
private error: Error | null = null
39-
private metadata: Record<string, unknown> = {}
40-
41-
getOAuthType() {
42-
return this.oAuthType
43-
}
44-
45-
setOAuthType(oAuthType: string) {
46-
this.oAuthType = oAuthType
47-
}
48-
49-
isActive(): boolean {
50-
return this.hasToken() || this.hasError()
51-
}
52-
53-
hasToken(): boolean {
54-
return this.getToken().length > 0
55-
}
56-
57-
getToken(): string {
58-
return this.token ?? ''
59-
}
60-
61-
setToken(token: string) {
62-
this.token = token
63-
}
64-
65-
getMasterUri(): string {
66-
return this.masterUri ?? ''
67-
}
68-
69-
setMasterUri(masterUri: string) {
70-
this.masterUri = masterUri
71-
}
72-
73-
getMasterKind(): string {
74-
return this.masterKind ?? ''
75-
}
76-
77-
setMasterKind(masterKind: string) {
78-
const ucType = masterKind.toUpperCase()
79-
if (ucType === KUBERNETES_MASTER_KIND || ucType === OPENSHIFT_MASTER_KIND) this.masterKind = ucType
80-
else {
81-
log.warn(`Unknown value set for master_kind in config (${masterKind}). Defaulting master kind to kubernetes`)
82-
this.masterKind = KUBERNETES_MASTER_KIND
83-
}
84-
}
85-
86-
hasError() {
87-
return this.error !== null
88-
}
89-
90-
getError() {
91-
return this.error
92-
}
93-
94-
setError(error: Error) {
95-
this.error = new Error('Openshift OAuth Error', { cause: error })
96-
log.error(error)
97-
}
98-
99-
addMetadata<T>(key: string, value: T) {
100-
this.metadata[key] = value
101-
}
102-
103-
getMetadata(): Record<string, unknown> {
104-
return this.metadata
105-
}
106-
107-
metadataValue<T>(key: string) {
108-
return this.metadata[key] as T
109-
}
110-
}

packages/oauth/src/oauth-service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { KUBERNETES_MASTER_KIND, log, OAuthProtoService, UserProfile } from './globals'
2-
import { fetchPath } from './utils'
1+
import { OAuthConfig, OAuthProtoService, UserProfile } from './api'
2+
import { FormService } from './form'
3+
import { KUBERNETES_MASTER_KIND, PATH_OSCONSOLE_CLIENT_CONFIG, log } from './globals'
34
import { DEFAULT_HAWTIO_MODE, DEFAULT_HAWTIO_NAMESPACE, HAWTIO_MODE_KEY, HAWTIO_NAMESPACE_KEY } from './metadata'
4-
import { OAuthConfig, PATH_OSCONSOLE_CLIENT_CONFIG } from './globals'
55
import { OSOAuthService } from './openshift'
6-
import { relToAbsUrl } from './utils/utils'
7-
import { FormService } from './form'
6+
import { fetchPath, relToAbsUrl } from './utils'
87

98
class OAuthService {
109
private userProfile: UserProfile = new UserProfile()

packages/oauth/src/openshift/osoauth-service.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import $ from 'jquery'
1+
import { userService } from '@hawtio/react'
22
import * as fetchIntercept from 'fetch-intercept'
3-
import { log, OAuthProtoService, UserProfile } from '../globals'
4-
import { fetchPath, isBlank, getCookie, redirect } from '../utils'
3+
import $ from 'jquery'
4+
import { OAuthProtoService, UserProfile } from '../api'
5+
import { log } from '../globals'
56
import { CLUSTER_CONSOLE_KEY } from '../metadata'
7+
import { fetchPath, getCookie, isBlank, redirect } from '../utils'
68
import {
9+
CLUSTER_VERSION_KEY,
710
DEFAULT_CLUSTER_VERSION,
811
EXPIRES_IN_KEY,
9-
OBTAINED_AT_KEY,
10-
TOKEN_TYPE_KEY,
11-
CLUSTER_VERSION_KEY,
1212
OAUTH_OS_PROTOCOL_MODULE,
13+
OBTAINED_AT_KEY,
1314
OpenShiftOAuthConfig,
1415
ResolveUser,
16+
TOKEN_TYPE_KEY,
1517
} from './globals'
1618
import {
1719
buildLoginUrl,
@@ -21,7 +23,6 @@ import {
2123
forceRelogin,
2224
tokenHasExpired,
2325
} from './support'
24-
import { userService } from '@hawtio/react'
2526

2627
interface UserObject {
2728
kind: string

packages/oauth/src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ export * from './fetch'
33
export * from './https'
44
export * from './strings'
55
export * from './urls'
6-
export * from './utils'

packages/oauth/src/utils/urls.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { LOGOUT_ENDPOINT, log } from '../globals'
22
import { isBlank } from './strings'
3-
import { relToAbsUrl } from './utils'
43

54
/**
65
* Join the supplied strings together using '/', stripping any leading/ending '/'
@@ -77,3 +76,7 @@ export function redirect(target: URL) {
7776
// Redirect to the target URI
7877
window.location.href = target.toString()
7978
}
79+
80+
export function relToAbsUrl(relativeUrl: string): string {
81+
return new URL(relativeUrl, window.location.origin).href
82+
}

packages/oauth/src/utils/utils.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)