whodis-react-storage-browser
Version:
React hooks and components for secure, best practices authentication in seconds
59 lines • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenFromStorage = void 0;
const simple_cookie_client_1 = require("simple-cookie-client");
const isServerSideRendering_1 = require("../../logic/env/isServerSideRendering");
const deleteSynchronizationCookie_1 = require("../../logic/synchronization/deleteSynchronizationCookie");
const isTokenSynchronized_1 = require("../../logic/synchronization/isTokenSynchronized");
const loadAntiCsrfTokenFromQueryParamsIfNeeded_1 = require("../queryparams/loadAntiCsrfTokenFromQueryParamsIfNeeded");
const key_1 = require("./key");
const setTokenToStorage_1 = require("./setTokenToStorage");
/**
* internal use only
*
* gets the token from storage
* - on client side rendering, gets the anti-csrf-token from local storage and checks it is still in sync w/ the token in the cookie
* - on server side rendering, gets the token from exposed cookies
*
* WARNING:
* - on server side this _does_ expose the raw JWT - and care must be taken not to log it
*/
const getTokenFromStorage = () => {
// handle getting the token on client-side
if (!(0, isServerSideRendering_1.isServerSideRendering)()) {
// try and load the anti-csrf-token into local storage from query params if present (e.g., from oidc auth redirect)
(0, loadAntiCsrfTokenFromQueryParamsIfNeeded_1.loadAntiCsrfTokenFromQueryParamsIfNeeded)();
// try and find the anti-csrf-token from local storage
const antiCsrfToken = localStorage.getItem(key_1.TOKEN_STORAGE_KEY);
if (!antiCsrfToken || antiCsrfToken === 'null') {
(0, deleteSynchronizationCookie_1.deleteSynchronizationCookie)(); // ensure that serverside is aware that there's no token
return null; // and return that there's no token
}
// if one exists, check that it is still in sync with the serverside
const synchronized = (0, isTokenSynchronized_1.isTokenSynchronized)({ token: antiCsrfToken });
if (!synchronized) {
(0, setTokenToStorage_1.setTokenToStorage)({ token: null });
return null; // and report to the caller that there is no token available
}
// if we reached here, that means we have a synchronized anti-csrf-token that we can use for consistent authenticated requests
return antiCsrfToken;
}
// handle getting the token on server-side
if ((0, isServerSideRendering_1.isServerSideRendering)()) {
// try and find the token from cookie storage
const tokenCookie = (0, simple_cookie_client_1.getCookie)({ name: 'authorization' });
if (!tokenCookie)
return null; // if no cookie, then no token in this env
// if one exists, check that it is still in sync w/ the clientside
const token = tokenCookie.value;
const synchronized = (0, isTokenSynchronized_1.isTokenSynchronized)({ token });
if (!synchronized)
return null; // and report to the caller that there is no token available
// if we reached here, that means we have a synchronized token that we can use for consistent authenticated request
return token;
}
// otherwise, unsupported environment
throw new Error('unexpected environment to get token from storage');
};
exports.getTokenFromStorage = getTokenFromStorage;
//# sourceMappingURL=getTokenFromStorage.js.map