11import { LRUCache } from 'lru-cache' ;
22import type { Adapter , AdapterPayload } from "oidc-provider" ;
33
4-
54const options = {
65 max : 500 ,
7- sizeCalculation : ( item :any , key :any ) => {
8- return 1
9- } ,
10- // for use with tracking overall storage size
6+ sizeCalculation : ( item : any , key : any ) => 1 ,
117 maxSize : 5000 ,
12-
13- // how long to live in ms
148 ttl : 1000 * 60 * 5 ,
15-
16- // return stale items before removing from cache?
179 allowStale : false ,
18-
1910 updateAgeOnGet : false ,
2011 updateAgeOnHas : false ,
2112}
2213
2314const epochTime = ( date = Date . now ( ) ) => Math . floor ( date / 1000 ) ;
24-
25- const storage = new LRUCache < string , AdapterPayload | string [ ] | string > ( options ) ;
15+ const storage = new LRUCache < string , AdapterPayload | string [ ] | string > ( options ) ;
2616
2717function grantKeyFor ( id : string ) {
2818 return `grant:${ id } ` ;
2919}
30-
31- function userCodeKeyFor ( userCode :string ) {
20+ function userCodeKeyFor ( userCode : string ) {
3221 return `userCode:${ userCode } ` ;
3322}
3423
35- class MemoryAdapter implements Adapter {
24+ class MemoryAdapter implements Adapter {
3625 private readonly name : string ;
37- constructor ( name :string ) {
26+
27+ constructor ( name : string ) {
3828 this . name = name ;
3929 }
4030
41- key ( id :string ) {
31+ key ( id : string ) {
4232 return `${ this . name } :${ id } ` ;
4333 }
4434
45- destroy ( id :string ) {
35+ destroy ( id : string ) {
4636 const key = this . key ( id ) ;
37+ const found = storage . get ( key ) as AdapterPayload | undefined ;
38+ const grantId = found ?. grantId ;
4739
48- const found = storage . get ( key ) as AdapterPayload ;
49- const grantId = found && found . grantId ;
40+ if ( found ?. userCode ) {
41+ storage . delete ( userCodeKeyFor ( found . userCode ) ) ;
42+ }
5043
5144 storage . delete ( key ) ;
5245
5346 if ( grantId ) {
5447 const grantKey = grantKeyFor ( grantId ) ;
55- ( storage . get ( grantKey ) as string [ ] ) ! . forEach ( token => storage . delete ( token ) ) ;
48+ const tokens = storage . get ( grantKey ) as string [ ] | undefined ;
49+ tokens ?. forEach ( token => storage . delete ( token ) ) ;
5650 storage . delete ( grantKey ) ;
5751 }
5852
5953 return Promise . resolve ( ) ;
6054 }
6155
6256 consume ( id : string ) {
63- ( storage . get ( this . key ( id ) ) as AdapterPayload ) ! . consumed = epochTime ( ) ;
57+ const key = this . key ( id ) ;
58+ const payload = storage . get ( key ) as AdapterPayload | undefined ;
59+ if ( payload ) {
60+ payload . consumed = epochTime ( ) ;
61+ storage . set ( key , payload ) ;
62+ }
6463 return Promise . resolve ( ) ;
6564 }
6665
6766 find ( id : string ) : Promise < AdapterPayload | void | undefined > {
68- if ( storage . has ( this . key ( id ) ) ) {
69- return Promise . resolve < AdapterPayload > ( storage . get ( this . key ( id ) ) as AdapterPayload ) ;
67+ if ( storage . has ( this . key ( id ) ) ) {
68+ return Promise . resolve ( storage . get ( this . key ( id ) ) as AdapterPayload ) ;
7069 }
71- return Promise . resolve < undefined > ( undefined )
70+ return Promise . resolve ( undefined ) ;
7271 }
7372
7473 findByUserCode ( userCode : string ) {
7574 const id = storage . get ( userCodeKeyFor ( userCode ) ) as string ;
7675 return this . find ( id ) ;
7776 }
7877
79- upsert ( id : string , payload : {
80- iat : number ;
81- exp : number ;
82- uid : string ;
83- kind : string ;
84- jti : string ;
85- accountId : string ;
86- loginTs : number ;
87- } , expiresIn : number ) {
78+ upsert ( id : string , payload : AdapterPayload , expiresIn : number ) {
8879 const key = this . key ( id ) ;
8980
90- storage . set ( key , payload , { ttl : expiresIn * 1000 } ) ;
81+ if ( payload . grantId ) {
82+ const grantKey = grantKeyFor ( payload . grantId ) ;
83+ const grant = ( storage . get ( grantKey ) as string [ ] ) || [ ] ;
84+ if ( ! grant . includes ( key ) ) grant . push ( key ) ;
85+ storage . set ( grantKey , grant ) ;
86+ }
87+
88+ if ( payload . userCode ) {
89+ storage . set ( userCodeKeyFor ( payload . userCode ) , id ) ;
90+ }
9191
92+ storage . set ( key , payload , { ttl : expiresIn * 1000 } ) ;
9293 return Promise . resolve ( ) ;
9394 }
9495
9596 findByUid ( uid : string ) : Promise < AdapterPayload | void | undefined > {
96- for ( const [ _ , value ] of storage . entries ( ) ) {
97- if ( typeof value === "object" && "uid" in value && value . uid === uid ) {
98- return Promise . resolve ( value ) ;
97+ for ( const [ _ , value ] of storage . entries ( ) ) {
98+ if ( typeof value === "object" && "uid" in value && value . uid === uid ) {
99+ return Promise . resolve ( value as AdapterPayload ) ;
99100 }
100101 }
101102 return Promise . resolve ( undefined ) ;
102103 }
103104
104105 revokeByGrantId ( grantId : string ) : Promise < void | undefined > {
105106 const grantKey = grantKeyFor ( grantId ) ;
106- const grant = storage . get ( grantKey ) as string [ ] ;
107+ const grant = storage . get ( grantKey ) as string [ ] | undefined ;
107108 if ( grant ) {
108109 grant . forEach ( ( token ) => storage . delete ( token ) ) ;
109110 storage . delete ( grantKey ) ;
@@ -112,4 +113,4 @@ class MemoryAdapter implements Adapter{
112113 }
113114}
114115
115- export default MemoryAdapter
116+ export default MemoryAdapter ;
0 commit comments