Skip to content

Commit 6bd25d4

Browse files
authored
feat: support for cta in cookies (#24)
* feat: support for cta in cookies * fix: token needs to be url encoded when in query param
1 parent 05d1d5f commit 6bd25d4

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/validators/http.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,49 @@ describe('HTTP Request CAT Validator with auto renew', () => {
626626
expect(result.cfResponse.headers['cta-common-access-token']).toBeDefined();
627627
});
628628

629+
test('cloudfront request with token as cookie', async () => {
630+
const httpValidator = new HttpValidator({
631+
keys: [
632+
{
633+
kid: 'Symmetric256',
634+
key: Buffer.from(
635+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
636+
'hex'
637+
)
638+
}
639+
],
640+
issuer: 'eyevinn'
641+
});
642+
const result = await httpValidator.validateCloudFrontRequest({
643+
clientIp: 'dummy',
644+
method: 'GET',
645+
uri: '/content/path/file.m3u8',
646+
querystring: '',
647+
headers: {
648+
cookie: [
649+
{
650+
value: `CTA-Common-Access-Token=${base64encoded!}; Path=/; Secure; HttpOnly`
651+
}
652+
],
653+
host: [
654+
{
655+
key: 'Host',
656+
value: 'example.com'
657+
}
658+
]
659+
}
660+
});
661+
expect(result.status).toBe(200);
662+
expect(
663+
result.cfResponse.headers['cta-common-access-token']
664+
).not.toBeDefined();
665+
expect(
666+
result.cfResponse.headers['set-cookie'][0].value.includes(
667+
'cta-common-access-token'
668+
)
669+
).toBeTruthy();
670+
});
671+
629672
test('cloudfront request with autorenew where token has not expired', async () => {
630673
base64encoded = await generator.generate(
631674
{

src/validators/http.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ export class HttpValidator {
314314
? request.headers[headerName]![0]
315315
: (request.headers[headerName] as string);
316316
catrType = 'header';
317+
} else if (request.headers['cookie']) {
318+
const cookies = !Array.isArray(request.headers['cookie'])
319+
? [request.headers['cookie'] as string]
320+
: (request.headers['cookie'] as string[]);
321+
for (const cookie of cookies) {
322+
const parts = cookie.split(';')[0].match(/(.*?)=(.*)$/);
323+
if (parts) {
324+
const [name, value] = parts.slice(1);
325+
if (name.toLowerCase() === 'cta-common-access-token') {
326+
token = value;
327+
catrType = 'cookie';
328+
break;
329+
}
330+
}
331+
}
317332
} else if (url && url.searchParams.has(this.tokenUriParam)) {
318333
token = url.searchParams.get(this.tokenUriParam) || undefined;
319334
catrType = 'query';
@@ -374,7 +389,8 @@ export class HttpValidator {
374389
{ addCwtTag: true }
375390
);
376391
const newToken = newCat.raw?.toString('base64');
377-
const newUrl = new URL(value[header][0] + newToken);
392+
const encodedToken = encodeURIComponent(newToken!);
393+
const newUrl = new URL(value[header][0] + encodedToken);
378394
response.setHeader(header, newUrl.toString());
379395
}
380396
}

0 commit comments

Comments
 (0)