diff --git a/src/catif.test.ts b/src/catif.test.ts index a15637a..8810ca4 100644 --- a/src/catif.test.ts +++ b/src/catif.test.ts @@ -66,4 +66,53 @@ describe('Common Access Token If', () => { ] }); }); + + test('can handle catu in catif', async () => { + const withCatu = CommonAccessTokenIf.fromDict({ + exp: [ + 307, + { + Location: [ + 'https://auth.example.net/?CAT=', + { + iss: null, + iat: null, + catu: { + scheme: { + 'exact-match': 'https' + }, + path: { + 'prefix-match': '/' + } + } + } + ] + }, + 'abc123' + ] + }); + expect(withCatu.toDict()).toEqual({ + exp: [ + 307, + { + Location: [ + 'https://auth.example.net/?CAT=', + { + iss: null, + iat: null, + catu: { + scheme: { + 'exact-match': 'https' + }, + path: { + 'prefix-match': '/' + } + } + } + ] + }, + 'abc123' + ] + }); + }); }); diff --git a/src/catif.ts b/src/catif.ts index 9a23ab2..11fc7c0 100644 --- a/src/catif.ts +++ b/src/catif.ts @@ -17,23 +17,28 @@ const valueToDict: { [key: string]: (value: any) => any } = { const [code, headers, kid] = value; const dictHeaders: { [h: string]: any } = {}; headers.forEach((v: any, header: string) => { - dictHeaders[header] = valueToDict[header] ? valueToDict[header](v) : v; + dictHeaders[header] = valueToDict[header.toLowerCase()] + ? valueToDict[header.toLowerCase()](v) + : v; }); return [code, dictHeaders, kid]; }, location: (value) => { if (typeof value === 'string') { - return { Location: value }; + return value; } else { const [url, map] = value; const obj: { [key: string]: any } = {}; (map as Map).forEach((v, claim) => { - obj[claim] = valueToDict[claim] ? valueToDict[claim](v) : v; + const label = parseInt(claim); + obj[labelsToClaim[label]] = valueToDict[labelsToClaim[label]] + ? valueToDict[labelsToClaim[label]](v) + : v; }); - return { Location: [url, obj] }; + return [url, obj]; } }, - catu: (value) => CommonAccessTokenUri.fromUnlabeledMap(value).toDict() + catu: (value) => CommonAccessTokenUri.fromMap(value).toDict() }; const dictToValue: { [key: string]: (value: any) => any } = { @@ -43,8 +48,8 @@ const dictToValue: { [key: string]: (value: any) => any } = { for (const header in headers) { map.set( header, - dictToValue[header] - ? dictToValue[header](headers[header]) + dictToValue[header.toLowerCase()] + ? dictToValue[header.toLowerCase()](headers[header]) : headers[header] ); } @@ -55,10 +60,10 @@ const dictToValue: { [key: string]: (value: any) => any } = { return value; } else { const [url, dict] = value; - const lmap = new Map(); + const lmap = new Map(); for (const key in dict) { lmap.set( - key, + claimsToLabels[key], dictToValue[key] ? dictToValue[key](dict[key]) : dict[key] ); } diff --git a/src/index.test.ts b/src/index.test.ts index 7068c44..fa1abf1 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -92,7 +92,21 @@ describe('CAT', () => { exp: [ 307, { - Location: 'https://auth.example.net' + Location: [ + 'https://auth.example.net?CAT=', + { + iss: null, + iat: null, + catu: { + scheme: { + 'exact-match': 'https' + }, + path: { + 'prefix-match': '/' + } + } + } + ] } ] },