Skip to content

Commit

Permalink
Get oidc callback url in frontend functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Aug 7, 2024
1 parent 7da1369 commit d859d6d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions assets/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = {
adminUsername: 'admin',
oidcEnabled: false,
oidcLoginUrl: 'http://localhost:4000/auth/oidc_callback',
oidcCallbackUrl: '/auth/oidc_callback',
aTestVariable: 123,
},
},
Expand Down
9 changes: 9 additions & 0 deletions assets/js/lib/auth/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,3 +13,11 @@ export const getSingleSignOnLoginUrl = () => {

return '';
};

export const getSingleSignOnCallbackUrl = () => {
if (OIDC_ENABLED) {
return OIDC_CALLBACK_URL;
}

return '';
};
16 changes: 12 additions & 4 deletions assets/js/lib/auth/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
16 changes: 8 additions & 8 deletions assets/js/lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 })
Expand Down
11 changes: 10 additions & 1 deletion assets/js/trento.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -54,7 +58,12 @@ const createRouter = ({ getUser }) =>
createRoutesFromElements(
<Route element={<RoutesWrapper />} ErrorBoundary={SomethingWentWrong}>
<Route path="/session/new" element={<Login />} />
<Route path="/auth/oidc_callback" element={<OidCallback />} />
{isSingleSignOnEnabled() && (
<Route
path={getSingleSignOnCallbackUrl()}
element={<OidCallback />}
/>
)}
<Route path="/">
<Route
element={<Guard redirectPath="/session/new" getUser={getUser} />}
Expand Down

0 comments on commit d859d6d

Please sign in to comment.