Skip to content

Commit

Permalink
Rework flow to immediately set config to okapi for compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandberger committed Jun 7, 2024
1 parent e5c73eb commit 1b1a71b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
28 changes: 18 additions & 10 deletions src/components/AuthnLogin/AuthnLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,43 @@ import { setUnauthorizedPathToSession } from '../../loginServices';

const AuthnLogin = ({ stripes }) => {
const { config, okapi } = stripes;
const { tenantOptions = {} } = config;
// If config.tenantOptions is not defined, default to classic okapi.tenant and okapi.clientId
const { tenantOptions = [{ name: okapi.tenant, clientId: okapi.clientId }] } = config;
const tenants = Object.values(tenantOptions);

const setTenant = (tenant, clientId) => {
localStorage.setItem('tenant', JSON.stringify({ tenantName: tenant, clientId }));
stripes.store.dispatch(setOkapiTenant({ tenant, clientId }));
};

useEffect(() => {
if (okapi.authnUrl) {
/** Store unauthorized pathname to session storage. Refs STCOR-789
* @see OIDCRedirect
*/
setUnauthorizedPathToSession(window.location.pathname);
}

// If only 1 tenant is defined in config (in either okapi or config.tenantOptions) set to okapi to be accessed there
// in the rest of the application for compatibity across existing modules.
if (tenants.length === 1) {
const loginTenant = tenants[0];
setTenant(loginTenant.name, loginTenant.clientId);
}
// we only want to run this effect once, on load.
// okapi.authnUrl are defined in stripes.config.js
// okapi.authnUrl tenant values are defined in stripes.config.js
}, []); // eslint-disable-line react-hooks/exhaustive-deps

if (okapi.authnUrl) {

Check failure on line 39 in src/components/AuthnLogin/AuthnLogin.js

View workflow job for this annotation

GitHub Actions / build-npm

Block must not be padded by blank lines

Check failure on line 39 in src/components/AuthnLogin/AuthnLogin.js

View workflow job for this annotation

GitHub Actions / build-npm

Block must not be padded by blank lines

// If only 1 tenant is defined in config, skip the tenant selection screen.
if (tenants.length === 1) {
const loginTenant = tenants[0];
const redirectUri = `${window.location.protocol}//${window.location.host}/oidc-landing`;
const authnUri = `${okapi.authnUrl}/realms/${loginTenant.name}/protocol/openid-connect/auth?client_id=${loginTenant.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
const authnUri = `${okapi.authnUrl}/realms/${okapi.tenant}/protocol/openid-connect/auth?client_id=${okapi.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
return <Redirect to={authnUri} />;
}

const handleSelectTenant = (tenant, clientId) => {
localStorage.setItem('tenant', JSON.stringify({ tenantName: tenant, clientId }));
stripes.store.dispatch(setOkapiTenant({ tenant, clientId }));
};

return <PreLoginLanding onSelectTenant={handleSelectTenant} />;
return <PreLoginLanding onSelectTenant={setTenant} />;
}

return <Login
Expand Down
12 changes: 4 additions & 8 deletions src/components/OIDCLanding.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const OIDCLanding = () => {
const location = useLocation();
const store = useStore();
// const samlError = useRef();
const { okapi, config } = useStripes();
const { tenantOptions = {} } = config;

const { okapi } = useStripes();
const [potp, setPotp] = useState();
const [samlError, setSamlError] = useState();

Expand Down Expand Up @@ -58,12 +56,10 @@ const OIDCLanding = () => {
const otp = getOtp();

if (otp) {
const loginTenant = Object.values(tenantOptions)[0];

setPotp(otp);
fetch(`${okapi.url}/authn/token?code=${otp}&redirect-uri=${window.location.protocol}//${window.location.host}/oidc-landing`, {
credentials: 'include',
headers: { 'X-Okapi-tenant': loginTenant.name, 'Content-Type': 'application/json' },
headers: { 'X-Okapi-tenant': okapi.tenant, 'Content-Type': 'application/json' },
mode: 'cors',
})
.then((resp) => {
Expand All @@ -76,7 +72,7 @@ const OIDCLanding = () => {
});
})
.then(() => {
return requestUserWithPerms(okapi.url, store, loginTenant.name);
return requestUserWithPerms(okapi.url, store, okapi.tenant);
});
} else {
return resp.json().then((error) => {
Expand All @@ -93,7 +89,7 @@ const OIDCLanding = () => {
// we only want to run this effect once, on load.
// keycloak authentication will redirect here and the other deps will be constant:
// location.search: the query string; this will never change
// config.tenantOptions, okapi.url: these are defined in stripes.config.js
// okapi.tenant, okapi.url: these are defined in stripes.config.js
// store: the redux store
}, []); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down
4 changes: 2 additions & 2 deletions src/components/OIDCRedirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe('OIDCRedirect', () => {
afterAll(() => sessionStorage.removeItem('unauthorized_path'));

it('redirects to value from session storage under unauthorized_path key', () => {
useStripes.mockReturnValue({ okapi: { authnUrl: 'http://example.com/authn' }, config: {} });
useStripes.mockReturnValue({ okapi: { authnUrl: 'http://example.com/authn' } });
render(<OIDCRedirect />);

expect(screen.getByText(/internalredirect/)).toBeInTheDocument();
});

it('redirects fwd if no authn provided to stripes okapi config', () => {
useStripes.mockReturnValue({ okapi: { }, config: {} });
useStripes.mockReturnValue({ okapi: { } });
render(<OIDCRedirect />);

expect(screen.getByText(/internalredirect/)).toBeInTheDocument();
Expand Down

0 comments on commit 1b1a71b

Please sign in to comment.