From eeadefbb09b2f1a8b5aa0a77d92400d0680c9054 Mon Sep 17 00:00:00 2001 From: "Jason Diaz G." Date: Thu, 4 Jul 2024 11:15:44 -0600 Subject: [PATCH] fix(cloudflare-auth-access-provider): broken tests fix Signed-off-by: Jason Diaz G. --- .../src/helpers.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts index 298fd82a89..45718ffab1 100644 --- a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts +++ b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts @@ -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 { // 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`,