@@ -26,6 +26,9 @@ import { AccessTokenCache, Config } from './types';
26
26
27
27
const { readFile, writeFile } = promises ;
28
28
29
+ // client id, token map
30
+ type TokensCache = Record < string , AccessTokenCache > ;
31
+
29
32
export async function readConfig (
30
33
profile = 'default' ,
31
34
configPath = '~/.rai/config' ,
@@ -65,20 +68,21 @@ function readClientCredentials(configParser: ConfigIniParser, profile: string) {
65
68
}
66
69
}
67
70
71
+ const clientId = configParser . get ( profile , 'client_id' , '' ) ;
68
72
const config : Config = {
69
73
host : configParser . get ( profile , 'host' , '' ) ,
70
74
port : configParser . get ( profile , 'port' , DEFAULT_PORT ) ,
71
75
scheme : configParser . get ( profile , 'scheme' , DEFAULT_SCHEME ) ,
72
76
credentials : new ClientCredentials (
73
- configParser . get ( profile , 'client_id' , '' ) ,
77
+ clientId ,
74
78
configParser . get ( profile , 'client_secret' , '' ) ,
75
79
configParser . get (
76
80
profile ,
77
81
'client_credentials_url' ,
78
82
DEFAULT_CLIENT_CREDENTIALS_URL ,
79
83
) ,
80
- async ( ) => await readTokenCache ( profile ) ,
81
- async cache => await writeTokenCache ( cache , profile ) ,
84
+ async ( ) => await readTokenCache ( clientId ) ,
85
+ async cache => await writeTokenCache ( clientId , cache ) ,
82
86
) ,
83
87
} ;
84
88
@@ -97,16 +101,15 @@ function resolveHome(path: string) {
97
101
return path ;
98
102
}
99
103
100
- function makeTokenCachePath ( profile : string ) {
101
- return resolveHome ( `~/.rai/${ profile } _cache.json` ) ;
102
- }
104
+ const CACHE_PATH = '~/.rai/tokens.json' ;
103
105
104
- async function readTokenCache ( profile = 'default' ) {
105
- const cachePath = makeTokenCachePath ( profile ) ;
106
+ async function readTokenCache ( clientId : string ) {
107
+ const cachePath = resolveHome ( CACHE_PATH ) ;
106
108
107
109
try {
108
110
const cachedStr = await readFile ( cachePath , 'utf-8' ) ;
109
- const cache = JSON . parse ( cachedStr ) ;
111
+ const tokensCache = JSON . parse ( cachedStr ) as TokensCache ;
112
+ const cache = tokensCache [ clientId ] ;
110
113
111
114
if ( cache . access_token && cache . created_on && cache . expires_in ) {
112
115
return cache as AccessTokenCache ;
@@ -115,9 +118,20 @@ async function readTokenCache(profile = 'default') {
115
118
} catch { }
116
119
}
117
120
118
- async function writeTokenCache ( token : AccessTokenCache , profile = 'default' ) {
119
- const cachePath = makeTokenCachePath ( profile ) ;
120
- const cacheStr = JSON . stringify ( token , null , 2 ) ;
121
+ async function writeTokenCache ( clientId : string , token : AccessTokenCache ) {
122
+ const cachePath = resolveHome ( CACHE_PATH ) ;
123
+ let tokensCache : TokensCache = { } ;
124
+
125
+ try {
126
+ const cachedStr = await readFile ( cachePath , 'utf-8' ) ;
127
+
128
+ tokensCache = JSON . parse ( cachedStr ) as TokensCache ;
129
+ // eslint-disable-next-line no-empty
130
+ } catch { }
131
+
132
+ tokensCache [ clientId ] = token ;
133
+
134
+ const cacheStr = JSON . stringify ( tokensCache , null , 2 ) ;
121
135
122
136
await writeFile ( cachePath , cacheStr , 'utf-8' ) ;
123
137
}
0 commit comments