Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type CommonAccessTokenHeaderMap = Map<string, MatchMap>;
export class CommonAccessTokenHeader {
private cathMap: CommonAccessTokenHeaderMap = new Map();

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

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

/**
* Create a CATH claim from a map with string as keys
*/
public static fromMap(map: CommonAccessTokenHeaderMap) {
const cath = new CommonAccessTokenHeader();
cath.cathMap = map;
Expand Down
21 changes: 21 additions & 0 deletions src/catif.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ describe('Common Access Token If', () => {
]
});
});

test('can handle additional headers', async () => {
const basic = CommonAccessTokenIf.fromDict({
exp: [
307,
{
Location: 'https://auth.example.net/',
'x-custom-header': 'my-custom-header'
}
]
});
expect(basic.toDict()).toEqual({
exp: [
307,
{
Location: 'https://auth.example.net/',
'x-custom-header': 'my-custom-header'
}
]
});
});
});
25 changes: 17 additions & 8 deletions src/catif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export type CatIfDictValue = {
const valueToDict: { [key: string]: (value: any) => any } = {
exp: (value) => {
const [code, headers, kid] = value;
return [code, valueToDict['location'](headers.get('Location')), kid];
const dictHeaders: { [h: string]: any } = {};
headers.forEach((v: any, header: string) => {
dictHeaders[header] = valueToDict[header] ? valueToDict[header](v) : v;
});
return [code, dictHeaders, kid];
},
location: (value) => {
if (typeof value === 'string') {
Expand All @@ -35,13 +39,20 @@ const valueToDict: { [key: string]: (value: any) => any } = {
const dictToValue: { [key: string]: (value: any) => any } = {
exp: (value) => {
const [code, headers, kid] = value;
return [code, dictToValue['location'](headers['Location']), kid];
const map = new Map<string, any>();
for (const header in headers) {
map.set(
header,
dictToValue[header]
? dictToValue[header](headers[header])
: headers[header]
);
}
return [code, map, kid];
},
location: (value) => {
if (typeof value === 'string') {
const map = new Map<string, any>();
map.set('Location', value);
return map;
return value;
} else {
const [url, dict] = value;
const lmap = new Map<string, any>();
Expand All @@ -51,9 +62,7 @@ const dictToValue: { [key: string]: (value: any) => any } = {
dictToValue[key] ? dictToValue[key](dict[key]) : dict[key]
);
}
const map = new Map<string, any>();
map.set('Location', [url, lmap]);
return map;
return [url, lmap];
}
},
catu: (value) => CommonAccessTokenUri.fromDict(value).payload
Expand Down
Loading