Skip to content

Commit df2eeee

Browse files
committed
fix: public key b64 encoding
1 parent 513397b commit df2eeee

File tree

2 files changed

+11
-6
lines changed
  • packages/spacecat-shared-http-utils

2 files changed

+11
-6
lines changed

packages/spacecat-shared-http-utils/src/auth/handlers/jwt.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export default class JwtHandler extends AbstractHandler {
2626
}
2727

2828
async #setup(context) {
29-
const authPublicKey = context.env?.AUTH_PUBLIC_KEY;
29+
const authPublicKeyB64 = context.env?.AUTH_PUBLIC_KEY_B64;
3030

31-
if (!hasText(authPublicKey)) {
31+
if (!hasText(authPublicKeyB64)) {
3232
throw new Error('No public key provided');
3333
}
3434

35+
const authPublicKey = Buffer.from(authPublicKeyB64, 'base64').toString('utf-8');
36+
3537
this.authPublicKey = await importSPKI(authPublicKey, ALGORITHM_ES256);
3638
}
3739

@@ -48,6 +50,8 @@ export default class JwtHandler extends AbstractHandler {
4850
},
4951
);
5052

53+
verifiedToken.payload.tenants = verifiedToken.payload.tenants || [];
54+
5155
return verifiedToken.payload;
5256
}
5357

packages/spacecat-shared-http-utils/test/auth/handlers/jwt.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import AuthInfo from '../../../src/auth/auth-info.js';
2626
use(chaiAsPromised);
2727

2828
const publicKey = fs.readFileSync('test/fixtures/auth/jwt/public_key.pem', 'utf8');
29+
const publicKeyB64 = Buffer.from(publicKey, 'utf-8').toString('base64');
2930

3031
const privateKeyEncrypted = fs.readFileSync('test/fixtures/auth/jwt/private_key.pem', 'utf8');
3132
const decryptedPrivateKey = crypto.createPrivateKey({
@@ -85,7 +86,7 @@ describe('SpacecatJWTHandler', () => {
8586

8687
it('returns null when there is no authorization header', async () => {
8788
const context = {
88-
env: { AUTH_PUBLIC_KEY: publicKey },
89+
env: { AUTH_PUBLIC_KEY_B64: publicKeyB64 },
8990
};
9091
const result = await handler.checkAuth({}, context);
9192

@@ -96,7 +97,7 @@ describe('SpacecatJWTHandler', () => {
9697

9798
it('returns null when "Bearer " is missing from the authorization header', async () => {
9899
const context = {
99-
env: { AUTH_PUBLIC_KEY: publicKey },
100+
env: { AUTH_PUBLIC_KEY_B64: publicKeyB64 },
100101
pathInfo: { headers: { authorization: 'some-token' } },
101102
};
102103
const result = await handler.checkAuth({}, context);
@@ -108,7 +109,7 @@ describe('SpacecatJWTHandler', () => {
108109

109110
it('returns null when the token is empty', async () => {
110111
const context = {
111-
env: { AUTH_PUBLIC_KEY: publicKey },
112+
env: { AUTH_PUBLIC_KEY_B64: publicKeyB64 },
112113
pathInfo: { headers: { authorization: 'Bearer ' } },
113114
};
114115
const result = await handler.checkAuth({}, context);
@@ -123,7 +124,7 @@ describe('SpacecatJWTHandler', () => {
123124

124125
beforeEach(() => {
125126
context = {
126-
env: { AUTH_PUBLIC_KEY: publicKey },
127+
env: { AUTH_PUBLIC_KEY_B64: publicKeyB64 },
127128
func: { version: 'ci' },
128129
log: logStub,
129130
};

0 commit comments

Comments
 (0)