Adding option to pass additional headers in ProxiedSignInPage

Signed-off-by: Abhinav Rastogi <abhinav.rastogi@harness.io>
This commit is contained in:
Abhinav Rastogi
2022-12-09 17:15:50 +05:30
parent 74ebd88c23
commit 6f8716bd8c
2 changed files with 10 additions and 1 deletions
@@ -51,6 +51,7 @@ export function tokenToExpiry(jwtToken: string | undefined): Date {
type ProxiedSignInIdentityOptions = {
provider: string;
discoveryApi: typeof discoveryApiRef.T;
getHeaders?: () => Promise<HeadersInit>;
};
type State =
@@ -192,6 +193,7 @@ export class ProxiedSignInIdentity implements IdentityApi {
async fetchSession(): Promise<ProxiedSession> {
const baseUrl = await this.options.discoveryApi.getBaseUrl('auth');
const headers = await this.options.getHeaders?.();
// Note that we do not use the fetchApi here, since this all happens before
// sign-in completes so there can be no automatic token injection and
@@ -200,7 +202,7 @@ export class ProxiedSignInIdentity implements IdentityApi {
`${baseUrl}/${this.options.provider}/refresh`,
{
signal: this.abortController.signal,
headers: { 'x-requested-with': 'XMLHttpRequest' },
headers: { ...headers, 'x-requested-with': 'XMLHttpRequest' },
credentials: 'include',
},
);
@@ -36,6 +36,12 @@ export type ProxiedSignInPageProps = SignInPageProps & {
* a properly configured auth provider ID in the auth backend.
*/
provider: string;
/**
* An optional function which returns a promise resolving with any headers
* that need to be added to the call made to /refresh endpoint.
*/
getHeaders?: () => Promise<HeadersInit>;
};
/**
@@ -60,6 +66,7 @@ export const ProxiedSignInPage = (props: ProxiedSignInPageProps) => {
const identity = new ProxiedSignInIdentity({
provider: props.provider,
discoveryApi,
getHeaders: props.getHeaders,
});
await identity.start();