From 6d24efd604e802f935864859476f2482fddb2a4b Mon Sep 17 00:00:00 2001 From: Tadayoshi Sato Date: Thu, 7 Mar 2024 15:34:43 +0900 Subject: [PATCH] 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. --- packages/kubernetes-api/src/index.ts | 2 +- packages/management-api/src/index.ts | 4 +- packages/oauth/src/api.ts | 103 +++++++++++++++++- packages/oauth/src/form/form-service.ts | 5 +- packages/oauth/src/globals.ts | 101 ----------------- packages/oauth/src/oauth-service.ts | 9 +- .../oauth/src/openshift/osoauth-service.ts | 15 +-- packages/oauth/src/utils/index.ts | 1 - packages/oauth/src/utils/urls.ts | 5 +- packages/oauth/src/utils/utils.ts | 3 - 10 files changed, 124 insertions(+), 124 deletions(-) delete mode 100644 packages/oauth/src/utils/utils.ts diff --git a/packages/kubernetes-api/src/index.ts b/packages/kubernetes-api/src/index.ts index fa4c404d..57b5efd1 100644 --- a/packages/kubernetes-api/src/index.ts +++ b/packages/kubernetes-api/src/index.ts @@ -6,7 +6,7 @@ const registerK8Api = async (): Promise => { log.debug('Awaiting registering of OAuth') oAuthInit() - log.debug('OAuth registered - getting active profile') + log.debug('OAuth registered - getting active profile') return await k8Init() } diff --git a/packages/management-api/src/index.ts b/packages/management-api/src/index.ts index 37a32d92..75a95eee 100644 --- a/packages/management-api/src/index.ts +++ b/packages/management-api/src/index.ts @@ -1,6 +1,6 @@ -import { ManagementService } from './management-service' -import { log } from './globals' import { isK8ApiRegistered } from '@hawtio/online-kubernetes-api' +import { log } from './globals' +import { ManagementService } from './management-service' export const mgmtService = new ManagementService() diff --git a/packages/oauth/src/api.ts b/packages/oauth/src/api.ts index 3aa4fb3d..2e672b5e 100644 --- a/packages/oauth/src/api.ts +++ b/packages/oauth/src/api.ts @@ -1,5 +1,106 @@ -import { log, UserProfile } from './globals' +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 + 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 = {} + + 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(key: string, value: T) { + this.metadata[key] = value + } + + getMetadata(): Record { + return this.metadata + } + + metadataValue(key: string) { + return this.metadata[key] as T + } +} let userProfile: UserProfile | null = null diff --git a/packages/oauth/src/form/form-service.ts b/packages/oauth/src/form/form-service.ts index 941911ce..14c36435 100644 --- a/packages/oauth/src/form/form-service.ts +++ b/packages/oauth/src/form/form-service.ts @@ -2,8 +2,8 @@ import { PUBLIC_USER, userService } from '@hawtio/react' import * as fetchIntercept from 'fetch-intercept' import $ from 'jquery' import { jwtDecode } from 'jwt-decode' -import { relToAbsUrl } from 'src/utils/utils' -import { OAuthProtoService, OPENSHIFT_MASTER_KIND, UserProfile, log } from '../globals' +import { OAuthProtoService, UserProfile } from '../api' +import { OPENSHIFT_MASTER_KIND, log } from '../globals' import { FetchOptions, fetchPath, @@ -11,6 +11,7 @@ import { joinPaths, logoutRedirect, redirect, + relToAbsUrl, secureDispose, secureRetrieve, } from '../utils' diff --git a/packages/oauth/src/globals.ts b/packages/oauth/src/globals.ts index 7353647e..ada8be10 100644 --- a/packages/oauth/src/globals.ts +++ b/packages/oauth/src/globals.ts @@ -1,6 +1,4 @@ import { Logger } from '@hawtio/react' -import { FormConfig } from './form' -import { OpenShiftOAuthConfig } from './openshift' export const log = Logger.get('hawtio-oauth') export const PATH_OSCONSOLE_CLIENT_CONFIG = 'osconsole/config.json' @@ -9,102 +7,3 @@ export const LOGOUT_ENDPOINT = '/auth/logout' // Kinds identified for the master cluster export const OPENSHIFT_MASTER_KIND = 'OPENSHIFT' export const KUBERNETES_MASTER_KIND = 'KUBERNETES' - -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 - 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 = {} - - 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(key: string, value: T) { - this.metadata[key] = value - } - - getMetadata(): Record { - return this.metadata - } - - metadataValue(key: string) { - return this.metadata[key] as T - } -} diff --git a/packages/oauth/src/oauth-service.ts b/packages/oauth/src/oauth-service.ts index c6caaeea..730a935f 100644 --- a/packages/oauth/src/oauth-service.ts +++ b/packages/oauth/src/oauth-service.ts @@ -1,10 +1,9 @@ -import { KUBERNETES_MASTER_KIND, log, OAuthProtoService, UserProfile } from './globals' -import { fetchPath } from './utils' +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 { OAuthConfig, PATH_OSCONSOLE_CLIENT_CONFIG } from './globals' import { OSOAuthService } from './openshift' -import { relToAbsUrl } from './utils/utils' -import { FormService } from './form' +import { fetchPath, relToAbsUrl } from './utils' class OAuthService { private userProfile: UserProfile = new UserProfile() diff --git a/packages/oauth/src/openshift/osoauth-service.ts b/packages/oauth/src/openshift/osoauth-service.ts index 9abb2ad0..b1b4e527 100644 --- a/packages/oauth/src/openshift/osoauth-service.ts +++ b/packages/oauth/src/openshift/osoauth-service.ts @@ -1,17 +1,19 @@ -import $ from 'jquery' +import { userService } from '@hawtio/react' import * as fetchIntercept from 'fetch-intercept' -import { log, OAuthProtoService, UserProfile } from '../globals' -import { fetchPath, isBlank, getCookie, redirect } from '../utils' +import $ from 'jquery' +import { OAuthProtoService, UserProfile } from '../api' +import { log } from '../globals' import { CLUSTER_CONSOLE_KEY } from '../metadata' +import { fetchPath, getCookie, isBlank, redirect } from '../utils' import { + CLUSTER_VERSION_KEY, DEFAULT_CLUSTER_VERSION, EXPIRES_IN_KEY, - OBTAINED_AT_KEY, - TOKEN_TYPE_KEY, - CLUSTER_VERSION_KEY, OAUTH_OS_PROTOCOL_MODULE, + OBTAINED_AT_KEY, OpenShiftOAuthConfig, ResolveUser, + TOKEN_TYPE_KEY, } from './globals' import { buildLoginUrl, @@ -21,7 +23,6 @@ import { forceRelogin, tokenHasExpired, } from './support' -import { userService } from '@hawtio/react' interface UserObject { kind: string diff --git a/packages/oauth/src/utils/index.ts b/packages/oauth/src/utils/index.ts index 4d40091d..b7c84563 100644 --- a/packages/oauth/src/utils/index.ts +++ b/packages/oauth/src/utils/index.ts @@ -3,4 +3,3 @@ export * from './fetch' export * from './https' export * from './strings' export * from './urls' -export * from './utils' diff --git a/packages/oauth/src/utils/urls.ts b/packages/oauth/src/utils/urls.ts index 9298faa9..adacdd27 100644 --- a/packages/oauth/src/utils/urls.ts +++ b/packages/oauth/src/utils/urls.ts @@ -1,6 +1,5 @@ import { LOGOUT_ENDPOINT, log } from '../globals' import { isBlank } from './strings' -import { relToAbsUrl } from './utils' /** * Join the supplied strings together using '/', stripping any leading/ending '/' @@ -77,3 +76,7 @@ export function redirect(target: URL) { // Redirect to the target URI window.location.href = target.toString() } + +export function relToAbsUrl(relativeUrl: string): string { + return new URL(relativeUrl, window.location.origin).href +} diff --git a/packages/oauth/src/utils/utils.ts b/packages/oauth/src/utils/utils.ts deleted file mode 100644 index 7f1ba516..00000000 --- a/packages/oauth/src/utils/utils.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function relToAbsUrl(relativeUrl: string): string { - return new URL(relativeUrl, window.location.origin).href -}