fix(cloudflare-auth-access-provider): broken tests fix

Signed-off-by: Jason Diaz G. <jason.rene.diaz.gonzalez@ericsson.com>
This commit is contained in:
Jason Diaz G.
2024-07-04 11:15:44 -06:00
parent 75d026ab1f
commit eeadefbb09
@@ -40,8 +40,10 @@ export class AuthHelper {
options?: { cache?: CacheService },
): AuthHelper {
const teamName = config.getString('teamName');
const customHeader = config.getString('customHeader');
const customCookieAuthName = config.getString('customCookieAuthName');
const customHeader = config.getOptionalString('customHeader');
const customCookieAuthName = config.getOptionalString(
'customCookieAuthName',
);
const serviceTokens = (
config.getOptionalConfigArray('serviceTokens') ?? []
)?.map(cfg => {
@@ -55,7 +57,14 @@ export class AuthHelper {
new URL(`https://${teamName}.cloudflareaccess.com/cdn-cgi/access/certs`),
);
return new AuthHelper(teamName, serviceTokens, keySet, options?.cache, customHeader, customCookieAuthName);
return new AuthHelper(
teamName,
serviceTokens,
keySet,
options?.cache,
customHeader,
customCookieAuthName,
);
}
private constructor(
@@ -65,7 +74,7 @@ export class AuthHelper {
private readonly cache?: CacheService,
private readonly customHeader?: string,
private readonly customCookieAuthName?: string,
) { }
) {}
async authenticate(req: express.Request): Promise<CloudflareAccessResult> {
// JWTs generated by Access are available in a request header as
@@ -79,7 +88,9 @@ export class AuthHelper {
// can be used.
throw new AuthenticationError(
`Missing ${this.customHeader || CF_JWT_HEADER} and
${this.customCookieAuthName || COOKIE_AUTH_NAME} from Cloudflare Access`,
${
this.customCookieAuthName || COOKIE_AUTH_NAME
} from Cloudflare Access`,
);
}
@@ -161,7 +172,10 @@ export class AuthHelper {
const headers = new Headers();
// set both headers just the way inbound responses are set
headers.set(this.customHeader || CF_JWT_HEADER, jwt);
headers.set('cookie', `${this.customCookieAuthName || COOKIE_AUTH_NAME}=${jwt}`);
headers.set(
'cookie',
`${this.customCookieAuthName || COOKIE_AUTH_NAME}=${jwt}`,
);
try {
const res = await fetch(
`https://${this.teamName}.cloudflareaccess.com/cdn-cgi/access/get-identity`,