Skip to content

Commit 853317b

Browse files
authored
feat: remove padding from base64 encoded token (#26)
* feat: remove padding from base64 encoded token * fix: no padding on catif renew
1 parent c822c97 commit 853317b

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/index.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,53 @@ describe('CAT', () => {
183183
});
184184
});
185185

186+
test('can generate a token with no padding', async () => {
187+
const generator = new CAT({
188+
keys: {
189+
Symmetric256: Buffer.from(
190+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
191+
'hex'
192+
)
193+
}
194+
});
195+
const base64encoded = await generator.generateFromJson(
196+
{
197+
iss: 'eyevinn',
198+
exp: 1742984408,
199+
iat: 1742980808,
200+
cti: '66400ca63ab2c267cc0d874cc5f9a378',
201+
catv: 1
202+
},
203+
{
204+
type: 'mac',
205+
alg: 'HS256',
206+
kid: 'Symmetric256'
207+
}
208+
);
209+
console.log(base64encoded);
210+
expect(base64encoded).not.toContain('=');
211+
const validator = new CAT({
212+
keys: {
213+
Symmetric256: Buffer.from(
214+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
215+
'hex'
216+
)
217+
}
218+
});
219+
const result = await validator.validate(base64encoded!, 'mac', {
220+
issuer: 'eyevinn'
221+
});
222+
expect(result.error).not.toBeDefined();
223+
expect(result.cat).toBeDefined();
224+
expect(result.cat!.claims).toEqual({
225+
iss: 'eyevinn',
226+
exp: 1742984408,
227+
iat: 1742980808,
228+
cti: '66400ca63ab2c267cc0d874cc5f9a378',
229+
catv: 1
230+
});
231+
});
232+
186233
test('can validate a MAC:ed token with standard claims', async () => {
187234
const base64encoded =
188235
'0YRDoQEFoQRMU3ltbWV0cmljMjU2eDZkOTAxMDNhMTAxNzU2MzZmNjE3MDNhMmYyZjYxNzMyZTY1Nzg2MTZkNzA2YzY1MmU2MzZmNmRYIDL8dIteq8pMXXX9oL4eo2NX1kQUaselV6p/JHSEVXWX';

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
CommonAccessTokenFactory
55
} from './cat';
66
import { KeyNotFoundError } from './errors';
7-
import { generateRandomHex, toBase64 } from './util';
7+
import { generateRandomHex, toBase64NoPadding } from './util';
88

99
export { CommonAccessToken } from './cat';
1010
export { CommonAccessTokenRenewal } from './catr';
@@ -239,7 +239,7 @@ export class CAT {
239239
if (!cat.raw) {
240240
throw new Error('Failed to MAC token');
241241
}
242-
return toBase64(cat.raw);
242+
return toBase64NoPadding(cat.raw);
243243
}
244244
}
245245

@@ -295,7 +295,7 @@ export class CAT {
295295
if (!cat.raw) {
296296
throw new Error('Failed to MAC token');
297297
}
298-
return toBase64(cat.raw);
298+
return toBase64NoPadding(cat.raw);
299299
}
300300
}
301301

@@ -329,6 +329,6 @@ export class CAT {
329329
if (!newCat.raw) {
330330
throw new Error('Failed to MAC token');
331331
}
332-
return toBase64(newCat.raw);
332+
return toBase64NoPadding(newCat.raw);
333333
}
334334
}

src/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ export function toHex(input: Buffer): string {
2424
.map((byte) => byte.toString(16).padStart(2, '0'))
2525
.join('');
2626
}
27+
28+
/**
29+
* Convert a buffer to base64 string without padding
30+
* @param input Buffer to convert
31+
* @returns Base64 string without padding
32+
*/
33+
export function toBase64NoPadding(input: Buffer): string {
34+
const base64 = toBase64(input);
35+
return base64.replace(/=+$/, '');
36+
}

src/validators/http.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { CommonAccessTokenDict, CommonAccessTokenFactory } from '../cat';
2020
import { ICTIStore } from '../stores/interface';
2121
import { ITokenLogger } from '../loggers/interface';
2222
import { CatIfDictValue } from '../catif';
23+
import { toBase64NoPadding } from '../util';
2324

2425
interface HttpValidatorKey {
2526
kid: string;
@@ -388,7 +389,7 @@ export class HttpValidator {
388389
this.opts.alg || 'HS256',
389390
{ addCwtTag: true }
390391
);
391-
const newToken = newCat.raw?.toString('base64');
392+
const newToken = toBase64NoPadding(newCat.raw!);
392393
const encodedToken = encodeURIComponent(newToken!);
393394
const newUrl = new URL(value[header][0] + encodedToken);
394395
response.setHeader(header, newUrl.toString());

0 commit comments

Comments
 (0)