import { OktaAuthOptions } from '@okta/okta-auth-js' ; import { LOGIN_CALLBACK_PATH , STORAGE_KEY } from './constants' ; const HOST = window . location . host ; const PROTO = window . location . protocol ; const REDIRECT_URI = `${ PROTO }//${ HOST }${ LOGIN_CALLBACK_PATH }` ; const POST_LOGOUT_REDIRECT_URI = `${ PROTO }//${ HOST }/` ; const DEFAULT_SIW_VERSION = '' ; export interface Config extends OktaAuthOptions { defaultScopes : boolean ; siwVersion : string ; siwAuthClient : boolean ; idps : string ; clientSecret : string ; forceRedirect : boolean ; useInteractionCodeFlow : boolean ; enableSharedStorage : boolean ; } export function getDefaultConfig ( ) { const ISSUER = process . env . ISSUER ; const CLIENT_ID = process . env . CLIENT_ID ; const CLIENT_SECRET = process . env . CLIENT_SECRET || '' ; return { issuer : ISSUER , clientId : CLIENT_ID , redirectUri : REDIRECT_URI , useInteractionCodeFlow : false , responseType : [ 'token' , 'id_token' ] , scopes : [ 'openid' , 'email' , 'offline_access' ] , pkce : true , forceRedirect : false , siwVersion : DEFAULT_SIW_VERSION , siwAuthClient : false , idps : '' , postLogoutRedirectUri : POST_LOGOUT_REDIRECT_URI , clientSecret : CLIENT_SECRET , defaultScopes : false , cookies : { secure : true } , enableSharedStorage : true } ; } export function getConfigFromUrl ( ) { const url = new URL ( window . location . href ) ; const issuer = url . searchParams . get ( 'issuer' ) ; const redirectUri = url . searchParams . get ( 'redirectUri' ) || REDIRECT_URI ; const postLogoutRedirectUri = url . searchParams . get ( 'postLogoutRedirectUri' ) || POST_LOGOUT_REDIRECT_URI ; const clientId = url . searchParams . get ( 'clientId' ) ; const clientSecret = url . searchParams . get ( 'clientSecret' ) ; const pkce = url . searchParams . get ( 'pkce' ) !== 'false' ; const defaultScopes = url . searchParams . get ( 'defaultScopes' ) === 'true' ; const scopes = ( url . searchParams . get ( 'scopes' ) || 'openid,email,offline_access' ) . split ( ',' ) ; const responseType = ( url . searchParams . get ( 'responseType' ) || 'id_token,token' ) . split ( ',' ) ; const responseMode = url . searchParams . get ( 'responseMode' ) || undefined ; const storage = url . searchParams . get ( 'storage' ) || undefined ; const expireEarlySeconds = + url . searchParams . get ( 'expireEarlySeconds' ) || undefined ; const secureCookies = url . searchParams . get ( 'secure' ) !== 'false' ; const sameSite = url . searchParams . get ( 'sameSite' ) || undefined ; const siwVersion = url . searchParams . get ( 'siwVersion' ) || DEFAULT_SIW_VERSION ; const siwAuthClient = url . searchParams . get ( 'siwAuthClient' ) === 'true' ; const idps = url . searchParams . get ( 'idps' ) || '' ; const useInteractionCodeFlow = url . searchParams . get ( 'useInteractionCodeFlow' ) === 'true' ; const forceRedirect = url . searchParams . get ( 'forceRedirect' ) === 'true' ; const enableSharedStorage = url . searchParams . get ( 'enableSharedStorage' ) !== 'false' ; return { issuer , clientId , redirectUri , useInteractionCodeFlow , pkce , defaultScopes , scopes , responseType , responseMode , postLogoutRedirectUri , forceRedirect , siwVersion , siwAuthClient , idps , enableSharedStorage , clientSecret , cookies : { secure : secureCookies , sameSite } , tokenManager : { storage , expireEarlySeconds } , transactionManager : { enableSharedStorage } , } ; } export function saveConfigToStorage ( config ) { const configCopy = { } ; Object . keys ( config ) . forEach ( key => { if ( typeof ( config as any ) [ key ] !== 'function' ) { configCopy [ key ] = ( config as any ) [ key ] ; } } ) ; localStorage . setItem ( STORAGE_KEY , JSON . stringify ( configCopy ) ) ; } export function getConfigFromStorage ( ) { const storedValue = localStorage . getItem ( STORAGE_KEY ) ; if ( ! storedValue ) { return getDefaultConfig ( ) ; } const config = JSON . parse ( storedValue ) ; return config ; } export function clearStorage ( ) { localStorage . removeItem ( STORAGE_KEY ) ; } export function flattenConfig ( config ) { const flat = { } ; Object . assign ( flat , config . tokenManager ) ; Object . assign ( flat , config . cookies ) ; Object . assign ( flat , config . transactionManager ) ; Object . keys ( config ) . forEach ( key => { if ( key !== 'tokenManager' && key !== 'cookies' && key !== 'transactionManager' ) { ( flat as any ) [ key ] = ( config as any ) [ key ] ; } } ) ; return flat ; }