From d859d6d2134aa16ac651d47c511c88965e573365 Mon Sep 17 00:00:00 2001 From: arbulu89 Date: Wed, 7 Aug 2024 11:44:34 +0200 Subject: [PATCH] Get oidc callback url in frontend functions --- assets/jest.config.js | 1 + assets/js/lib/auth/config.js | 9 +++++++++ assets/js/lib/auth/config.test.js | 16 ++++++++++++---- assets/js/lib/auth/index.js | 16 ++++++++-------- assets/js/trento.jsx | 11 ++++++++++- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/assets/jest.config.js b/assets/jest.config.js index 35745fd120..9786828bef 100644 --- a/assets/jest.config.js +++ b/assets/jest.config.js @@ -70,6 +70,7 @@ module.exports = { adminUsername: 'admin', oidcEnabled: false, oidcLoginUrl: 'http://localhost:4000/auth/oidc_callback', + oidcCallbackUrl: '/auth/oidc_callback', aTestVariable: 123, }, }, diff --git a/assets/js/lib/auth/config.js b/assets/js/lib/auth/config.js index 4cec0ac065..e1430616c2 100644 --- a/assets/js/lib/auth/config.js +++ b/assets/js/lib/auth/config.js @@ -2,6 +2,7 @@ import { getFromConfig } from '@lib/config'; const OIDC_ENABLED = getFromConfig('oidcEnabled') || false; const OIDC_LOGIN_URL = getFromConfig('oidcLoginUrl') || ''; +const OIDC_CALLBACK_URL = getFromConfig('oidcCallbackUrl') || ''; export const isSingleSignOnEnabled = () => OIDC_ENABLED; @@ -12,3 +13,11 @@ export const getSingleSignOnLoginUrl = () => { return ''; }; + +export const getSingleSignOnCallbackUrl = () => { + if (OIDC_ENABLED) { + return OIDC_CALLBACK_URL; + } + + return ''; +}; diff --git a/assets/js/lib/auth/config.test.js b/assets/js/lib/auth/config.test.js index 49cc08c709..e71ec53c12 100644 --- a/assets/js/lib/auth/config.test.js +++ b/assets/js/lib/auth/config.test.js @@ -9,19 +9,27 @@ describe('auth config', () => { expect(isSingleSignOnEnabled()).toBeFalsy(); global.config.oidcEnabled = true; - - return import('./config').then(config => { + + return import('./config').then((config) => { expect(config.isSingleSignOnEnabled()).toBeTruthy(); }); }); it('should get OIDC login url if OIDC is enabled', async () => { global.config.oidcEnabled = true; - - return import('./config').then(config => { + + return import('./config').then((config) => { expect(config.getSingleSignOnLoginUrl()).toBe( 'http://localhost:4000/auth/oidc_callback' ); }); }); + + it('should get OIDC callback url if OIDC is enabled', async () => { + global.config.oidcEnabled = true; + + return import('./config').then((config) => { + expect(config.getSingleSignOnCallbackUrl()).toBe('/auth/oidc_callback'); + }); + }); }); diff --git a/assets/js/lib/auth/index.js b/assets/js/lib/auth/index.js index fcff55531f..549df21672 100644 --- a/assets/js/lib/auth/index.js +++ b/assets/js/lib/auth/index.js @@ -5,6 +5,14 @@ const STORAGE_REFRESH_TOKEN_IDENTIFIER = 'refresh_token'; export const authClient = axios.create(); +export const login = (credentials) => + authClient.post('/api/session', credentials).then((response) => { + if (response.status !== 200) { + throw Error('unauthorized', { cause: response.status }); + } + return response; + }); + export const oidcEnrollment = (credentials) => authClient .post('/api/session/oidc_local/callback', credentials) @@ -15,14 +23,6 @@ export const oidcEnrollment = (credentials) => return response; }); -export const login = (credentials) => - authClient.post('/api/session', credentials).then((response) => { - if (response.status !== 200) { - throw Error('unauthorized', { cause: response.status }); - } - return response; - }); - export const refreshAccessToken = (refreshToken) => authClient .post('/api/session/refresh', { refresh_token: refreshToken }) diff --git a/assets/js/trento.jsx b/assets/js/trento.jsx index 3407cbcdaf..bf003cd30d 100644 --- a/assets/js/trento.jsx +++ b/assets/js/trento.jsx @@ -44,6 +44,10 @@ import ActivityLogPage from '@pages/ActivityLogPage'; import OidCallback from '@pages/OidcCallback'; import { profile } from '@lib/auth'; +import { + isSingleSignOnEnabled, + getSingleSignOnCallbackUrl, +} from '@lib/auth/config'; import { networkClient } from '@lib/network'; import { TARGET_CLUSTER, TARGET_HOST } from '@lib/model'; @@ -54,7 +58,12 @@ const createRouter = ({ getUser }) => createRoutesFromElements( } ErrorBoundary={SomethingWentWrong}> } /> - } /> + {isSingleSignOnEnabled() && ( + } + /> + )} }