Skip to content

Commit

Permalink
STCOR-787 Fix tenant and clientId references (#1492)
Browse files Browse the repository at this point in the history
* Ensure okapi is being read from store after pulling from tenantOptions in AuthLogin
  • Loading branch information
ryandberger authored Jun 24, 2024
1 parent 2e162f6 commit eed1ba5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/components/AuthnLogin/AuthnLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ const AuthnLogin = ({ stripes }) => {
if (okapi.authnUrl) {
// 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/${okapi.tenant}/protocol/openid-connect/auth?client_id=${okapi.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
const authnUri = `${okapi.authnUrl}/realms/${loginTenant.name}/protocol/openid-connect/auth?client_id=${loginTenant.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
return <Redirect to={authnUri} />;
}

Expand Down
11 changes: 6 additions & 5 deletions src/components/Root/FFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/

import ms from 'ms';
import { okapi } from 'stripes-config';
import { okapi as okapiConfig } from 'stripes-config';
import {
setRtrTimeout
} from '../../okapiActions';
Expand Down Expand Up @@ -112,7 +112,8 @@ export class FFetch {
rotationP.then((rotationInterval) => {
this.logger.log('rtr', `rotation fired from rotateCallback; next callback in ${ms(rotationInterval)}`);
this.store.dispatch(setRtrTimeout(setTimeout(() => {
rtr(this.nativeFetch, this.logger, this.rotateCallback);
const { okapi } = this.store.getState();
rtr(this.nativeFetch, this.logger, this.rotateCallback, okapi);
}, rotationInterval)));
});
};
Expand Down Expand Up @@ -173,12 +174,12 @@ export class FFetch {
*/
ffetch = async (resource, options = {}) => {
// FOLIO API requests are subject to RTR
if (isFolioApiRequest(resource, okapi.url)) {
if (isFolioApiRequest(resource, okapiConfig.url)) {
this.logger.log('rtrv', 'will fetch', resource);

// on authentication, grab the response to kick of the rotation cycle,
// then return the response
if (isAuthenticationRequest(resource, okapi.url)) {
if (isAuthenticationRequest(resource, okapiConfig.url)) {
this.logger.log('rtr', 'authn request');
return this.nativeFetch.apply(global, [resource, options && { ...options, ...OKAPI_FETCH_OPTIONS }])
.then(res => {
Expand Down Expand Up @@ -207,7 +208,7 @@ export class FFetch {
// tries to logout, the logout request will fail. And that's fine, just
// fine. We will let them fail, capturing the response and swallowing it
// to avoid getting stuck in an error loop.
if (isLogoutRequest(resource, okapi.url)) {
if (isLogoutRequest(resource, okapiConfig.url)) {
this.logger.log('rtr', 'logout request');

return this.nativeFetch.apply(global, [resource, options && { ...options, ...OKAPI_FETCH_OPTIONS }])
Expand Down
4 changes: 2 additions & 2 deletions src/components/Root/token-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isEmpty } from 'lodash';
import { okapi } from 'stripes-config';

import { getTokenExpiry, setTokenExpiry } from '../../loginServices';
import { RTRError, UnexpectedResourceError } from './Errors';
Expand Down Expand Up @@ -192,9 +191,10 @@ export const isRotating = () => {
* @param {function} fetchfx native fetch function
* @param {@folio/stripes/logger} logger
* @param {function} callback
* @param {object} okapi
* @returns void
*/
export const rtr = (fetchfx, logger, callback) => {
export const rtr = (fetchfx, logger, callback, okapi) => {
logger.log('rtr', '** RTR ...');

// rotation is already in progress, maybe in this window,
Expand Down
15 changes: 10 additions & 5 deletions src/components/Root/token-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
} from './token-util';
import { RTR_SUCCESS_EVENT } from './constants';

const okapi = {
tenant: 'diku',
url: 'http://test'
};

describe('isFolioApiRequest', () => {
it('accepts requests whose origin matches okapi\'s', () => {
const oUrl = 'https://millicent-sounds-kinda-like-malificent.edu';
Expand Down Expand Up @@ -126,7 +131,7 @@ describe('rtr', () => {
let ex = null;
// const callback = () => { console.log('HOLA!!!')}; // jest.fn();
try {
await rtr(fetchfx, logger, callback);
await rtr(fetchfx, logger, callback, okapi);
expect(callback).toHaveBeenCalled();
} catch (e) {
ex = e;
Expand Down Expand Up @@ -168,7 +173,7 @@ describe('rtr', () => {

let ex = null;
try {
await rtr(nativeFetch, logger, jest.fn());
await rtr(nativeFetch, logger, jest.fn(), okapi);
} catch (e) {
ex = e;
}
Expand Down Expand Up @@ -202,7 +207,7 @@ describe('rtr', () => {

let ex = null;
try {
await rtr(nativeFetch, logger, jest.fn());
await rtr(nativeFetch, logger, jest.fn(), okapi);
} catch (e) {
ex = e;
}
Expand Down Expand Up @@ -232,7 +237,7 @@ describe('rtr', () => {

let ex = null;
try {
await rtr(nativeFetch, logger, jest.fn());
await rtr(nativeFetch, logger, jest.fn(), okapi);
} catch (e) {
ex = e;
}
Expand Down Expand Up @@ -262,7 +267,7 @@ describe('rtr', () => {

let ex = null;
try {
await rtr(nativeFetch, logger, jest.fn());
await rtr(nativeFetch, logger, jest.fn(), okapi);
} catch (e) {
ex = e;
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/SSOLanding/useSSOSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const getToken = (cookies, params) => {
return cookies?.ssoToken || params?.ssoToken;
};

const getTenant = (params, token) => {
const getTenant = (params, token, store) => {
const tenant = config.useSecureTokens
? params?.tenantId
: parseJWT(token)?.tenant;

return tenant || okapi.tenant;
return tenant || store.getState()?.okapi?.tenant;
};

const useSSOSession = () => {
Expand All @@ -42,7 +42,7 @@ const useSSOSession = () => {
const params = getParams(location);

const token = getToken(cookies, params);
const tenant = getTenant(params, token);
const tenant = getTenant(params, token, store);

useEffect(() => {
requestUserWithPerms(okapi.url, store, tenant, token)
Expand Down
9 changes: 8 additions & 1 deletion src/components/SSOLanding/useSSOSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ describe('SSOLanding', () => {
useLocation.mockReturnValue({ search: '' });
useCookies.mockReturnValue([]);

useStore.mockReturnValue({ getState: jest.fn() });
useStore.mockReturnValue({
getState: jest.fn().mockReturnValue({
okapi: {
url: 'okapiUrl',
tenant: 'okapiTenant'
}
})
});

requestUserWithPerms.mockReturnValue(Promise.resolve());
});
Expand Down
8 changes: 4 additions & 4 deletions src/discoverServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function parseApplicationDescriptor(store, descriptor) {
const APP_MAX_COUNT = 500;

function fetchApplicationDetails(store) {
const okapi = store.getState().okapi;
const { okapi } = store.getState();

return fetch(`${okapi.url}/entitlements/${okapi.tenant}/applications?limit=${APP_MAX_COUNT}`, {
credentials: 'include',
Expand Down Expand Up @@ -146,7 +146,7 @@ function fetchApplicationDetails(store) {
*/

function fetchGatewayVersion(store) {
const okapi = store.getState().okapi;
const { okapi } = store.getState();

return fetch(`${okapi.url}/version`, {
credentials: 'include',
Expand All @@ -167,7 +167,7 @@ function fetchGatewayVersion(store) {
}

function fetchOkapiVersion(store) {
const okapi = store.getState().okapi;
const { okapi } = store.getState();

return fetch(`${okapi.url}/_/version`, {
credentials: 'include',
Expand All @@ -188,7 +188,7 @@ function fetchOkapiVersion(store) {
}

function fetchModules(store) {
const okapi = store.getState().okapi;
const { okapi } = store.getState();

return fetch(`${okapi.url}/_/proxy/tenants/${okapi.tenant}/modules?full=true`, {
credentials: 'include',
Expand Down

0 comments on commit eed1ba5

Please sign in to comment.