Skip to content

Commit

Permalink
fix(oauth): fix runtime typeerror with new UserProfile() constructor
Browse files Browse the repository at this point in the history
For some unknown reason, if UserProfile is moved to `api.ts` the console starts
to throw TypeError at runtime. As a workaround, moving UserProfile back to
`globals.ts`.
  • Loading branch information
tadayosi committed Mar 7, 2024
1 parent 3870ad4 commit 701bdc9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 89 deletions.
82 changes: 1 addition & 81 deletions packages/oauth/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormConfig } from './form'
import { KUBERNETES_MASTER_KIND, OPENSHIFT_MASTER_KIND, log } from './globals'
import { UserProfile, log } from './globals'
import { oAuthService } from './oauth-service'
import { OpenShiftOAuthConfig } from './openshift'

Expand All @@ -22,86 +22,6 @@ export interface OAuthProtoService {
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> {
Expand Down
4 changes: 2 additions & 2 deletions packages/oauth/src/form/form-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { OAuthProtoService, UserProfile } from '../api'
import { OPENSHIFT_MASTER_KIND, log } from '../globals'
import { OAuthProtoService } from '../api'
import { OPENSHIFT_MASTER_KIND, log, UserProfile } from '../globals'
import {
FetchOptions,
fetchPath,
Expand Down
80 changes: 80 additions & 0 deletions packages/oauth/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,83 @@ 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 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
}
}
6 changes: 3 additions & 3 deletions packages/oauth/src/oauth-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OAuthConfig, OAuthProtoService, UserProfile } from './api'
import { OAuthConfig, OAuthProtoService } from './api'
import { FormService } from './form'
import { KUBERNETES_MASTER_KIND, PATH_OSCONSOLE_CLIENT_CONFIG, log } from './globals'
import { KUBERNETES_MASTER_KIND, PATH_OSCONSOLE_CLIENT_CONFIG, UserProfile, 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 userProfile: UserProfile = new UserProfile()

private readonly config: Promise<OAuthConfig | null>
private readonly protoService: Promise<OAuthProtoService | null>
Expand Down
4 changes: 2 additions & 2 deletions packages/oauth/src/openshift/osoauth-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { userService } from '@hawtio/react'
import * as fetchIntercept from 'fetch-intercept'
import $ from 'jquery'
import { OAuthProtoService, UserProfile } from '../api'
import { log } from '../globals'
import { OAuthProtoService } from '../api'
import { UserProfile, log } from '../globals'
import { CLUSTER_CONSOLE_KEY } from '../metadata'
import { fetchPath, getCookie, isBlank, redirect } from '../utils'
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/oauth/src/openshift/support.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relToAbsUrl, redirect, logoutRedirect, secureStore, secureDispose, secureRetrieve } from '../utils'
import { log, UserProfile } from '../globals'
import { logoutRedirect, redirect, relToAbsUrl, secureDispose, secureRetrieve, secureStore } from '../utils'
import { EXPIRES_IN_KEY, OBTAINED_AT_KEY, OpenShiftOAuthConfig, TokenMetadata } from './globals'

const OS_TOKEN_STORAGE_KEY = 'osAuthCreds'
Expand Down

0 comments on commit 701bdc9

Please sign in to comment.