From 8a7db0dd15954f0ce50e18495f46352188625d18 Mon Sep 17 00:00:00 2001 From: "Jason Diaz G." Date: Thu, 4 Jul 2024 20:03:06 -0600 Subject: [PATCH] fix(cloudflare-auth-access-provider): change default names for custom variables Signed-off-by: Jason Diaz G. --- .../config.d.ts | 4 +-- .../src/helpers.test.ts | 16 ++++++++---- .../src/helpers.ts | 26 +++++++++---------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/plugins/auth-backend-module-cloudflare-access-provider/config.d.ts b/plugins/auth-backend-module-cloudflare-access-provider/config.d.ts index 235cac8446..9715eb3a39 100644 --- a/plugins/auth-backend-module-cloudflare-access-provider/config.d.ts +++ b/plugins/auth-backend-module-cloudflare-access-provider/config.d.ts @@ -27,8 +27,8 @@ export interface Config { token: string; subject: string; }>; - customHeader?: string; - customCookieAuthName?: string; + jwtHeaderName?: string; + authorizationCookieName?: string; }; /** * The backstage token expiration. diff --git a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.test.ts b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.test.ts index be91583e16..d5a1c8be0f 100644 --- a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.test.ts +++ b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.test.ts @@ -240,13 +240,16 @@ describe('helpers', () => { }); }); - it('works for regular tokens, through custom header auth', async () => { + it('works for regular tokens, through jwtHeaderName header', async () => { jest.useFakeTimers({ now: 1600000004000, }); const helper = AuthHelper.fromConfig( - new ConfigReader({ teamName: 'mock-team', customHeader: 'X-Auth-Token' }), + new ConfigReader({ + teamName: 'mock-team', + jwtHeaderName: 'X-Auth-Token', + }), { cache }, ); const token = await tokenFactory.userToken(); @@ -282,18 +285,21 @@ describe('helpers', () => { expect(JSON.parse(cache.set.mock.calls[0][1] as string)).toEqual(expected); }); - it('works for regular tokens, through custom cookie auth name', async () => { + it('works for regular tokens, through authorizationCookieName cookie name', async () => { jest.useFakeTimers({ now: 1600000004000, }); const helper = AuthHelper.fromConfig( - new ConfigReader({ teamName: 'mock-team', customCookieAuthName: 'CF_Custom_Auth' }), + new ConfigReader({ + teamName: 'mock-team', + authorizationCookieName: 'CF_Auth', + }), { cache }, ); const token = await tokenFactory.userToken(); const request = createRequest({ - cookies: { 'CF_Custom_Auth': token }, + cookies: { CF_Custom_Auth: token }, }); const expected = { 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 45718ffab1..b3fefb8789 100644 --- a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts +++ b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts @@ -40,9 +40,9 @@ export class AuthHelper { options?: { cache?: CacheService }, ): AuthHelper { const teamName = config.getString('teamName'); - const customHeader = config.getOptionalString('customHeader'); - const customCookieAuthName = config.getOptionalString( - 'customCookieAuthName', + const jwtHeaderName = config.getOptionalString('jwtHeaderName'); + const authorizationCookieName = config.getOptionalString( + 'authorizationCookieName', ); const serviceTokens = ( config.getOptionalConfigArray('serviceTokens') ?? [] @@ -62,8 +62,8 @@ export class AuthHelper { serviceTokens, keySet, options?.cache, - customHeader, - customCookieAuthName, + jwtHeaderName, + authorizationCookieName, ); } @@ -72,24 +72,24 @@ export class AuthHelper { private readonly serviceTokens: ServiceToken[], private readonly keySet: ReturnType, private readonly cache?: CacheService, - private readonly customHeader?: string, - private readonly customCookieAuthName?: string, + private readonly jwtHeaderName?: string, + private readonly authorizationCookieName?: string, ) {} async authenticate(req: express.Request): Promise { // JWTs generated by Access are available in a request header as // Cf-Access-Jwt-Assertion and as cookies as CF_Authorization. - let jwt = req.header(this.customHeader || CF_JWT_HEADER); + let jwt = req.header(this.jwtHeaderName || CF_JWT_HEADER); if (!jwt) { - jwt = req.cookies[this.customCookieAuthName || COOKIE_AUTH_NAME]; + jwt = req.cookies[this.authorizationCookieName || COOKIE_AUTH_NAME]; } if (!jwt) { // Only throw if both are not provided by Cloudflare Access since either // can be used. throw new AuthenticationError( - `Missing ${this.customHeader || CF_JWT_HEADER} and + `Missing ${this.jwtHeaderName || CF_JWT_HEADER} and ${ - this.customCookieAuthName || COOKIE_AUTH_NAME + this.authorizationCookieName || COOKIE_AUTH_NAME } from Cloudflare Access`, ); } @@ -171,10 +171,10 @@ export class AuthHelper { ): Promise { const headers = new Headers(); // set both headers just the way inbound responses are set - headers.set(this.customHeader || CF_JWT_HEADER, jwt); + headers.set(this.jwtHeaderName || CF_JWT_HEADER, jwt); headers.set( 'cookie', - `${this.customCookieAuthName || COOKIE_AUTH_NAME}=${jwt}`, + `${this.authorizationCookieName || COOKIE_AUTH_NAME}=${jwt}`, ); try { const res = await fetch(