Skip to content

Commit

Permalink
refactor(oauth): shuffling of location of api type declarations in oa…
Browse files Browse the repository at this point in the history
…uth 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.
  • Loading branch information
tadayosi committed Mar 7, 2024
1 parent fb8924f commit 6d24efd
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 124 deletions.
2 changes: 1 addition & 1 deletion packages/kubernetes-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const registerK8Api = async (): Promise<boolean> => {
log.debug('Awaiting registering of OAuth')
oAuthInit()

log.debug('OAuth registered - getting active profile')
log.debug('OAuth registered - getting active profile')
return await k8Init()
}

Expand Down
4 changes: 2 additions & 2 deletions packages/management-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
103 changes: 102 additions & 1 deletion packages/oauth/src/api.ts
Original file line number Diff line number Diff line change
@@ -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<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

Expand Down
5 changes: 3 additions & 2 deletions packages/oauth/src/form/form-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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,
getCookie,
joinPaths,
logoutRedirect,
redirect,
relToAbsUrl,
secureDispose,
secureRetrieve,
} from '../utils'
Expand Down
101 changes: 0 additions & 101 deletions packages/oauth/src/globals.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<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
}
}
9 changes: 4 additions & 5 deletions packages/oauth/src/oauth-service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
15 changes: 8 additions & 7 deletions packages/oauth/src/openshift/osoauth-service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,7 +23,6 @@ import {
forceRelogin,
tokenHasExpired,
} from './support'
import { userService } from '@hawtio/react'

interface UserObject {
kind: string
Expand Down
1 change: 0 additions & 1 deletion packages/oauth/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './fetch'
export * from './https'
export * from './strings'
export * from './urls'
export * from './utils'
5 changes: 4 additions & 1 deletion packages/oauth/src/utils/urls.ts
Original file line number Diff line number Diff line change
@@ -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 '/'
Expand Down Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions packages/oauth/src/utils/utils.ts

This file was deleted.

0 comments on commit 6d24efd

Please sign in to comment.