Skip to content

Commit d9f25c6

Browse files
authored
fix: catif claim now handles all HTTP headers (#37)
* fix: catif can handle other headers than location * chore: explaining comments
1 parent cd4ac72 commit d9f25c6

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

src/cath.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type CommonAccessTokenHeaderMap = Map<string, MatchMap>;
1010
export class CommonAccessTokenHeader {
1111
private cathMap: CommonAccessTokenHeaderMap = new Map();
1212

13+
/**
14+
* Create a CATH claim from a dictionary with numbers as keys (labels)
15+
*/
1316
public static fromDictTags(dict: { [key: number]: any }) {
1417
const newDict: { [key: string]: any } = {};
1518
for (const headerTag in dict) {
@@ -23,6 +26,9 @@ export class CommonAccessTokenHeader {
2326
return CommonAccessTokenHeader.fromDict(newDict);
2427
}
2528

29+
/**
30+
* Create a CATH claim from a dictionary with string as keys
31+
*/
2632
public static fromDict(dict: { [key: string]: any }) {
2733
const cath = new CommonAccessTokenHeader();
2834
for (const header in dict) {
@@ -35,6 +41,9 @@ export class CommonAccessTokenHeader {
3541
return cath;
3642
}
3743

44+
/**
45+
* Create a CATH claim from a map with string as keys
46+
*/
3847
public static fromMap(map: CommonAccessTokenHeaderMap) {
3948
const cath = new CommonAccessTokenHeader();
4049
cath.cathMap = map;

src/catif.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,25 @@ describe('Common Access Token If', () => {
4545
]
4646
});
4747
});
48+
49+
test('can handle additional headers', async () => {
50+
const basic = CommonAccessTokenIf.fromDict({
51+
exp: [
52+
307,
53+
{
54+
Location: 'https://auth.example.net/',
55+
'x-custom-header': 'my-custom-header'
56+
}
57+
]
58+
});
59+
expect(basic.toDict()).toEqual({
60+
exp: [
61+
307,
62+
{
63+
Location: 'https://auth.example.net/',
64+
'x-custom-header': 'my-custom-header'
65+
}
66+
]
67+
});
68+
});
4869
});

src/catif.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export type CatIfDictValue = {
1515
const valueToDict: { [key: string]: (value: any) => any } = {
1616
exp: (value) => {
1717
const [code, headers, kid] = value;
18-
return [code, valueToDict['location'](headers.get('Location')), kid];
18+
const dictHeaders: { [h: string]: any } = {};
19+
headers.forEach((v: any, header: string) => {
20+
dictHeaders[header] = valueToDict[header] ? valueToDict[header](v) : v;
21+
});
22+
return [code, dictHeaders, kid];
1923
},
2024
location: (value) => {
2125
if (typeof value === 'string') {
@@ -35,13 +39,20 @@ const valueToDict: { [key: string]: (value: any) => any } = {
3539
const dictToValue: { [key: string]: (value: any) => any } = {
3640
exp: (value) => {
3741
const [code, headers, kid] = value;
38-
return [code, dictToValue['location'](headers['Location']), kid];
42+
const map = new Map<string, any>();
43+
for (const header in headers) {
44+
map.set(
45+
header,
46+
dictToValue[header]
47+
? dictToValue[header](headers[header])
48+
: headers[header]
49+
);
50+
}
51+
return [code, map, kid];
3952
},
4053
location: (value) => {
4154
if (typeof value === 'string') {
42-
const map = new Map<string, any>();
43-
map.set('Location', value);
44-
return map;
55+
return value;
4556
} else {
4657
const [url, dict] = value;
4758
const lmap = new Map<string, any>();
@@ -51,9 +62,7 @@ const dictToValue: { [key: string]: (value: any) => any } = {
5162
dictToValue[key] ? dictToValue[key](dict[key]) : dict[key]
5263
);
5364
}
54-
const map = new Map<string, any>();
55-
map.set('Location', [url, lmap]);
56-
return map;
65+
return [url, lmap];
5766
}
5867
},
5968
catu: (value) => CommonAccessTokenUri.fromDict(value).payload

0 commit comments

Comments
 (0)