11import { claimsToLabels , CommonAccessTokenDict , labelsToClaim } from './cat' ;
2+ import { CommonAccessTokenUri } from './catu' ;
23
34type CatIfValue = Map < number , [ number , { [ key : string ] : string } ] > ;
45export type CommonAccessTokenIfMap = Map < number , CatIfValue > ;
@@ -11,9 +12,59 @@ export type CatIfDictValue = {
1112 ] ;
1213} ;
1314
15+ const valueToDict : { [ key : string ] : ( value : any ) => any } = {
16+ exp : ( value ) => {
17+ const [ code , headers , kid ] = value ;
18+ return [ code , valueToDict [ 'location' ] ( headers . get ( 'Location' ) ) , kid ] ;
19+ } ,
20+ location : ( value ) => {
21+ if ( typeof value === 'string' ) {
22+ return { Location : value } ;
23+ } else {
24+ const [ url , map ] = value ;
25+ const obj : { [ key : string ] : any } = { } ;
26+ ( map as Map < string , any > ) . forEach ( ( v , claim ) => {
27+ obj [ claim ] = valueToDict [ claim ] ? valueToDict [ claim ] ( v ) : v ;
28+ } ) ;
29+ return { Location : [ url , obj ] } ;
30+ }
31+ } ,
32+ catu : ( value ) => CommonAccessTokenUri . fromUnlabeledMap ( value ) . toDict ( )
33+ } ;
34+
35+ const dictToValue : { [ key : string ] : ( value : any ) => any } = {
36+ exp : ( value ) => {
37+ const [ code , headers , kid ] = value ;
38+ return [ code , dictToValue [ 'location' ] ( headers [ 'Location' ] ) , kid ] ;
39+ } ,
40+ location : ( value ) => {
41+ if ( typeof value === 'string' ) {
42+ const map = new Map < string , any > ( ) ;
43+ map . set ( 'Location' , value ) ;
44+ return map ;
45+ } else {
46+ const [ url , dict ] = value ;
47+ const lmap = new Map < string , any > ( ) ;
48+ for ( const key in dict ) {
49+ lmap . set (
50+ key ,
51+ dictToValue [ key ] ? dictToValue [ key ] ( dict [ key ] ) : dict [ key ]
52+ ) ;
53+ }
54+ const map = new Map < string , any > ( ) ;
55+ map . set ( 'Location' , [ url , lmap ] ) ;
56+ return map ;
57+ }
58+ } ,
59+ catu : ( value ) => CommonAccessTokenUri . fromDict ( value ) . payload
60+ } ;
61+
1462export class CommonAccessTokenIf {
1563 private catIfMap : CommonAccessTokenIfMap = new Map ( ) ;
1664
65+ /**
66+ * Create a CATIF claim from a dictionary with numbers as keys (labels)
67+ */
1768 public static fromDictTags ( dict : { [ key : number ] : any } ) {
1869 const newDict : { [ key : string ] : any } = { } ;
1970 for ( const key in dict ) {
@@ -23,14 +74,24 @@ export class CommonAccessTokenIf {
2374 return CommonAccessTokenIf . fromDict ( newDict ) ;
2475 }
2576
77+ /**
78+ * Create a CATIF claim from a dictionary with string as keys
79+ */
2680 public static fromDict ( dict : { [ key : string ] : any } ) {
2781 const catif = new CommonAccessTokenIf ( ) ;
2882 for ( const catIfClaim in dict ) {
29- catif . catIfMap . set ( claimsToLabels [ catIfClaim ] , dict [ catIfClaim ] ) ;
83+ const v = dict [ catIfClaim ] ;
84+ catif . catIfMap . set (
85+ claimsToLabels [ catIfClaim ] ,
86+ dictToValue [ catIfClaim ] ? dictToValue [ catIfClaim ] ( v ) : v
87+ ) ;
3088 }
3189 return catif ;
3290 }
3391
92+ /**
93+ * Create a CATIF claim from a map with string as keys
94+ */
3495 public static fromMap ( map : CommonAccessTokenIfMap ) {
3596 const catif = new CommonAccessTokenIf ( ) ;
3697 catif . catIfMap = map ;
@@ -40,7 +101,8 @@ export class CommonAccessTokenIf {
40101 toDict ( ) {
41102 const result : { [ key : string ] : any } = { } ;
42103 this . catIfMap . forEach ( ( catIfValue , claim ) => {
43- result [ labelsToClaim [ claim ] ] = catIfValue ;
104+ result [ labelsToClaim [ claim ] ] =
105+ valueToDict [ labelsToClaim [ claim ] ] ( catIfValue ) ;
44106 } ) ;
45107 return result ;
46108 }
0 commit comments