Skip to content
Open
Changes from all 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
53 changes: 25 additions & 28 deletions src/settings/RefreshTokenRotation.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
import { merge } from 'lodash';
import PropTypes from 'prop-types';
import { useCallback, useEffect, useState } from 'react';
import { Field, Form } from 'react-final-form';
import { FormattedMessage } from 'react-intl';

import { getTokenExpiry } from '@folio/stripes/core';
import { Button, LoadingPane, Pane, PaneHeader, TextField } from '@folio/stripes/components';
import { getTokenExpiry, RTR_CONSTANTS } from '@folio/stripes/core';
import { Button, LoadingPane, Pane, PaneHeader } from '@folio/stripes/components';

/**
* manipulate AT/RT expiration dates in storage in order to test RTR.
* @returns
*/
const RefreshTokenRotation = ({ stripes }) => {
// why WHY copy this string here instead of importing it from stripes-core?
//
// RTR_FORCE_REFRESH_EVENT will be present in stripes-core 10.2.0 (stripes
// 9.2.0). Importing it would force the stripes peer depedency to bump from
// ^9.1.0 to ^9.2.0.If we copy the string instead of importing it, we can
// remain compatible with 9.1.0.
//
// OK, compatibility is nice. But it's still gross, right? Yep, super gross.
// Aren't you nauseated? Yes, yes I am. 🤢🧼🛁
const RTR_FORCE_REFRESH_EVENT = '@folio/stripes/core::RTRForceRefresh';

Comment on lines -15 to -25
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that ui-developer 9.0.0 is out, we should be safe to do 9.2.0 for R release.

const RefreshTokenRotation = () => {
const [isLoading, setIsLoading] = useState(true);
const [tokenExpiration, setTokenExpiration] = useState({});
const [tokenExpiration, setTokenExpiration] = useState({ atExpires: -1, rtExpires: -1 });

useEffect(() => {
setIsLoading(true);
getTokenExpiry().then((te) => {
setTokenExpiration(te ?? { atExpires: -1, rtExpires: -1 });
setIsLoading(false);
});
const callback = () => {
setIsLoading(true);
getTokenExpiry().then((te) => {
setTokenExpiration({
atExpires: te.atExpires ?? te.accessTokenExpiration ?? -1,
rtExpires: te.rtExpires ?? te.refreshTokenExpiration ?? -1,
Comment on lines +20 to +21
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safely handles old and new style

});
setIsLoading(false);
});
};

callback();

window.addEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback);

return () => window.removeEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback);
}, []);

/**
* forceRefresh
* dispatch an event to force a token rotation
*/
const forceRefresh = useCallback(
() => window.dispatchEvent(new Event(RTR_FORCE_REFRESH_EVENT)),
() => window.dispatchEvent(new Event(RTR_CONSTANTS.RTR_FORCE_REFRESH_EVENT)),
[],
);

/**
* saveRtrConfig
* update stripes.config.rtr from form
*/
const saveRtrConfig = useCallback(
/* const saveRtrConfig = useCallback(
(values) => {
merge(stripes.config.rtr, {
idleSessionTTL: values.idleSessionTTL,
Expand All @@ -60,7 +57,7 @@ const RefreshTokenRotation = ({ stripes }) => {
forceRefresh();
},
[stripes, forceRefresh],
);
); */

if (!isLoading) {
return (
Expand Down Expand Up @@ -88,7 +85,7 @@ const RefreshTokenRotation = ({ stripes }) => {
<FormattedMessage id="ui-developer.rtr.forceRefresh" />
</Button>

<Form
{/* <Form
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this form currently doesn't configure anything, due to that being pulled from STCOR-888, so let's hide it.

onSubmit={saveRtrConfig}
initialValues={{
...stripes.config.rtr,
Expand Down Expand Up @@ -130,7 +127,7 @@ const RefreshTokenRotation = ({ stripes }) => {
</Button>
</form>
)}
</Form>
</Form> */}
</div>
</Pane>
);
Expand Down
Loading