diff --git a/src/cath.ts b/src/cath.ts index 106b153..888d6de 100644 --- a/src/cath.ts +++ b/src/cath.ts @@ -10,6 +10,9 @@ export type CommonAccessTokenHeaderMap = Map; 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) { @@ -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) { @@ -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; diff --git a/src/catif.test.ts b/src/catif.test.ts index 81185d2..a15637a 100644 --- a/src/catif.test.ts +++ b/src/catif.test.ts @@ -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' + } + ] + }); + }); }); diff --git a/src/catif.ts b/src/catif.ts index 46cbc89..9a23ab2 100644 --- a/src/catif.ts +++ b/src/catif.ts @@ -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') { @@ -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(); + 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(); - map.set('Location', value); - return map; + return value; } else { const [url, dict] = value; const lmap = new Map(); @@ -51,9 +62,7 @@ const dictToValue: { [key: string]: (value: any) => any } = { dictToValue[key] ? dictToValue[key](dict[key]) : dict[key] ); } - const map = new Map(); - map.set('Location', [url, lmap]); - return map; + return [url, lmap]; } }, catu: (value) => CommonAccessTokenUri.fromDict(value).payload