Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STCOR-776 optionally schedule RTR based on session data #1488

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/components/Root/FFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
setRtrTimeout
} from '../../okapiActions';

import { getTokenExpiry } from '../../loginServices';
import {
getPromise,
isAuthenticationRequest,
Expand Down Expand Up @@ -107,19 +108,42 @@ export class FFetch {
rotateCallback = (res) => {
this.logger.log('rtr', 'rotation callback setup');

// set a short rotation interval by default, then inspect the response for
// token-expiration data to use instead if available. not all responses
// (e.g. those from _self) contain token-expiration values, so it is
// necessary to provide a default.
let rotationInterval = 10 * 1000;
const scheduleRotation = (rotationP) => {
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);
}, rotationInterval)));
});
};

// When starting a new session, the response from /bl-users/login-with-expiry
// will contain AT expiration info, but when restarting an existing session,
// the response from /bl-users/_self will NOT, although that information will
// have been cached in local-storage.
//
// This means there are many places we have to check to figure out when the
// AT is likely to expire, and thus when we want to rotate. First inspect
// the response, otherwise the session. Default to 10 seconds.
if (res?.accessTokenExpiration) {
rotationInterval = (new Date(res.accessTokenExpiration).getTime() - Date.now()) * RTR_AT_TTL_FRACTION;
this.logger.log('rtr', 'rotation scheduled with login response data');
const rotationPromise = Promise.resolve((new Date(res.accessTokenExpiration).getTime() - Date.now()) * RTR_AT_TTL_FRACTION);

scheduleRotation(rotationPromise);
} else {
const rotationPromise = getTokenExpiry().then((expiry) => {
if (expiry.atExpires) {
this.logger.log('rtr', 'rotation scheduled with cached session data');
return (new Date(expiry.atExpires).getTime() - Date.now()) * RTR_AT_TTL_FRACTION;
}

// default: 10 seconds
this.logger.log('rtr', 'rotation scheduled with default value');
return 10 * 1000;
});

scheduleRotation(rotationPromise);
}

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);
}, rotationInterval)));
}

/**
Expand Down Expand Up @@ -158,13 +182,16 @@ export class FFetch {
this.logger.log('rtr', 'authn request');
return this.nativeFetch.apply(global, [resource, options && { ...options, ...OKAPI_FETCH_OPTIONS }])
.then(res => {
this.logger.log('rtr', 'authn success!');
// a response can only be read once, so we clone it to grab the
// tokenExpiration in order to kick of the rtr cycle, then return
// the original
res.clone().json().then(json => {
this.rotateCallback(json.tokenExpiration);
});
const clone = res.clone();
if (clone.ok) {
this.logger.log('rtr', 'authn success!');
clone.json().then(json => {
this.rotateCallback(json.tokenExpiration);
});
}

return res;
});
Expand Down
6 changes: 4 additions & 2 deletions src/loginServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ export function validateUser(okapiUrl, store, tenant, session) {

/**
* checkOkapiSession
* 1. Pull the session from local storage; if non-empty validate it, dispatching load-resources actions.
* 1. Pull the session from local storage; if it contains a user id,
* validate it by fetching /_self to verify that it is still active,
* dispatching load-resources actions.
* 2. Check if SSO (SAML) is enabled, dispatching check-sso actions
* 3. dispatch set-okapi-ready.
*
Expand All @@ -753,7 +755,7 @@ export function validateUser(okapiUrl, store, tenant, session) {
export function checkOkapiSession(okapiUrl, store, tenant) {
getOkapiSession()
.then((sess) => {
return sess !== null ? validateUser(okapiUrl, store, tenant, sess) : null;
return sess?.user?.id ? validateUser(okapiUrl, store, tenant, sess) : null;
})
.then(() => {
return getSSOEnabled(okapiUrl, store, tenant);
Expand Down
Loading