Skip to content

Commit

Permalink
fix(connect): reflect remote username for login user
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Oct 26, 2023
1 parent 8490f4f commit 8d07d9d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/hawtio/src/plugins/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { preferencesRegistry } from '@hawtiosrc/preferences/registry'
import { Connect } from './Connect'
import { ConnectPreferences } from './ConnectPreferences'
import help from './help.md'
import { isActive } from './init'
import { isActive, registerUserHooks } from './init'

export const connect: HawtioPlugin = () => {
registerUserHooks()
hawtio.addPlugin({
id: 'connect',
title: 'Connect',
Expand Down
25 changes: 24 additions & 1 deletion packages/hawtio/src/plugins/connect/init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { userService } from '@hawtiosrc/auth/user-service'
import { connectService } from '@hawtiosrc/plugins/shared/connect-service'
import { log, PATH_PROXY_ENABLED } from './globals'
import { PATH_PROXY_ENABLED, log } from './globals'

export async function isActive(): Promise<boolean> {
const proxyEnabled = await isProxyEnabled()
Expand Down Expand Up @@ -37,3 +38,25 @@ function isConnectLogin(): boolean {
const url = new URL(window.location.href)
return url.pathname === connectService.getLoginPath()
}

/**
* Register user hooks to userService if it's connecting to an authenticated
* remote Jolokia endpoint with credentials in session storage, so that the user
* can reflect the remote credentials.
*/
export function registerUserHooks() {
const credentials = connectService.getCurrentCredentials()
if (!credentials) {
return
}

userService.addFetchUserHook('connect', async resolve => {
resolve({ username: credentials.username, isLogin: true })
return true
})
userService.addLogoutHook('connect', async () => {
// Logout from remote connection should close the window
window.close()
return true
})
}
12 changes: 11 additions & 1 deletion packages/hawtio/src/plugins/shared/__mocks__/connect-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Connection, ConnectionTestResult, Connections, IConnectService } from '../connect-service'
import {
Connection,
ConnectionCredentials,
ConnectionTestResult,
Connections,
IConnectService,
} from '../connect-service'

class MockConnectService implements IConnectService {
constructor() {
Expand All @@ -14,6 +20,10 @@ class MockConnectService implements IConnectService {
return null
}

getCurrentCredentials(): ConnectionCredentials | null {
return null
}

loadConnections(): Connections {
return {}
}
Expand Down
13 changes: 9 additions & 4 deletions packages/hawtio/src/plugins/shared/connect-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type ConnectionTestResult = {
message: string
}

type ConnectionCredentials = {
export type ConnectionCredentials = {
username: string
password: string
}
Expand All @@ -44,6 +44,7 @@ const LOGIN_PATH = '/connect/login'
export interface IConnectService {
getCurrentConnectionName(): string | null
getCurrentConnection(): Connection | null
getCurrentCredentials(): ConnectionCredentials | null
loadConnections(): Connections
saveConnections(connections: Connections): void
getConnection(name: string): Connection | null
Expand Down Expand Up @@ -93,11 +94,10 @@ class ConnectService implements IConnectService {
}

// Apply credentials if it exists
const item = sessionStorage.getItem(SESSION_KEY_CREDENTIALS)
if (!item) {
const credentials = this.getCurrentCredentials()
if (!credentials) {
return conn
}
const credentials = JSON.parse(item) as ConnectionCredentials
conn.username = credentials.username
conn.password = credentials.password
this.clearCredentialsOnLogout()
Expand All @@ -109,6 +109,11 @@ class ConnectService implements IConnectService {
eventService.onLogout(() => sessionStorage.removeItem(SESSION_KEY_CREDENTIALS))
}

getCurrentCredentials(): ConnectionCredentials | null {
const item = sessionStorage.getItem(SESSION_KEY_CREDENTIALS)
return item ? JSON.parse(item) : null
}

loadConnections(): Connections {
const conns = localStorage.getItem(STORAGE_KEY_CONNECTIONS)
return conns ? JSON.parse(conns) : {}
Expand Down

0 comments on commit 8d07d9d

Please sign in to comment.