Source

services/CallbackHandler.ts

/**
 * @module CallbackHandler
 * @category Helper Functions
 */
import {storage} from './StorageManager'

//TODO: Update documentation to reflect new OIDC library flow

// Various callback paths to allow custom, pre app render operations
// NB: OIDC callbacks are currently contained in AccessManager because the OIDC configuration requires them as well
const STORAGE_LANDING_CALLBACK: string = '/landing'

/**
 * Determines whether the URI being hit is a non-app callback, invokes the appropriate callback handler.
 *
 * This function is wrapped around the `ReactDOM.render` portion of the app index file, which allows the callback to be handled
 * outside of the application and provides performance increases due to not requiring any app rendering prior to action.
 *
 * @param basePath The URI of the application
 * @returns A boolean representing whether the URL being hit is one of the "out of app" callback endpoints
 * @method
 */
export const checkForCallbacks = (basePath: string) => {
	if (window.location.pathname === basePath.concat(STORAGE_LANDING_CALLBACK)) {
		// TODO: Can be handled using OIDC 'state' in future version of oidc-client
		// Get the query string params
		const urlParams = new URLSearchParams(window.location.search)

		// Load the landing page Qstring permissions
		//@ts-ignore
		let permittedQueryStrings = getPermittedQueryStrings()
		//@ts-ignore
		let permittedQueryLength = parseInt(process.env.REACT_APP_QSTRING_LENGTH) || 80

		// Save each permitted QS to session storage (if it exists)
		//@ts-ignore
		for (let permitted of permittedQueryStrings) {
			let qVal = urlParams.get(permitted)
			if (qVal && qVal.length <= permittedQueryLength) {
				storage.saveData(permitted, qVal, {update: true})
			}
		}
		
		// Load root of application (fairly sure this negates the return true, but just in case)
		window.location.replace(basePath + '/')
		return true
	}
	return false
}

/**
 * Load the list of permitted query strings from the environment variable REACT_APP_PERMITTED_QSTRINGS. Primary use
 * is for allowing specific query strings to save data to session storage upon landing.
 *
 * @returns A list of strings that are permitted
 */
function getPermittedQueryStrings() {
	const DELIMITER = ','
	let envStrings = process.env.REACT_APP_PERMITTED_QSTRINGS
	let cleanList = []

	// Check if permitted strings have been set
	if (envStrings) {
		// Split, trim, and lowercase the options
		let stringList = envStrings.split(DELIMITER)
		//@ts-ignore
		cleanList = stringList.map((qString: string) => {
			return qString.trim().toLowerCase()
		})
	}

	return cleanList
}