import { DOCUMENT } from '@angular/common';
import { inject, InjectionToken } from '@angular/core';
export const OAUTH_CONFIGURATIONS = new InjectionToken<OauthConfig>('OAUTH_CONFIGURATIONS');
export const LOCATION = new InjectionToken<Location>('LOCATION', {
providedIn: 'root',
factory: () => inject(DOCUMENT).location
});
export const STORAGE = new InjectionToken<Storage>('STORAGE', {
providedIn: 'root',
factory: () => {
const sessionStorage = inject(DOCUMENT).defaultView?.sessionStorage;
// The server DOM has a location but no sessionStorage, so only this token needs a check
if (!sessionStorage) {
throw new Error('OAuth requires sessionStorage, which is not available on the server.');
}
return sessionStorage;
}
});
export interface OauthOptions {
configurations: OauthConfig[];
}
export type OauthConfig = OauthConfigWithAppTokenUrl | OauthConfigWithAppTokenPageIdOrInternalPath;
interface OauthConfigBase {
/**
* If set to true, this configuration will be taken as the default config when OAuth actions are requested
* without a specific config name.
*/
default?: boolean;
/**
* The name of this OAuth config. Will be used for reference to identify which configuration to use when
* calling one of the OAuth actions (`authorize` or `token`).
*/
name: string;
clientId: string;
authorizeUrl: string;
tokenUrl: string;
endSessionUrl?: string;
revokeUrl?: string;
scope: string;
acrValues?: string[];
/**
* All requests to this URL (or child paths) will have the access token appended as a header
* when there is an access token present.
*/
bffBaseUrl: string;
/**
* Defines the threshold when a token is refreshed even before it has expired.
*
* Example: If the token still has 5,000 ms before it expires, it will be renewed.
* If it has 5,001ms, it won't be renewed.
*/
refreshBeforeExpiryThreshold?: number;
}
export interface OauthConfigWithAppTokenUrl extends OauthConfigBase {
appTokenUrl: string;
appTokenPageIdOrInternalPath?: never;
}
export interface OauthConfigWithAppTokenPageIdOrInternalPath extends OauthConfigBase {
appTokenPageIdOrInternalPath: string;
appTokenUrl?: never;
}