@@ -26,6 +26,9 @@ import { AccessTokenCache, Config } from './types';
2626
2727const { readFile, writeFile } = promises ;
2828
29+ // client id, token map
30+ type TokensCache = Record < string , AccessTokenCache > ;
31+
2932export async function readConfig (
3033 profile = 'default' ,
3134 configPath = '~/.rai/config' ,
@@ -65,20 +68,21 @@ function readClientCredentials(configParser: ConfigIniParser, profile: string) {
6568 }
6669 }
6770
71+ const clientId = configParser . get ( profile , 'client_id' , '' ) ;
6872 const config : Config = {
6973 host : configParser . get ( profile , 'host' , '' ) ,
7074 port : configParser . get ( profile , 'port' , DEFAULT_PORT ) ,
7175 scheme : configParser . get ( profile , 'scheme' , DEFAULT_SCHEME ) ,
7276 credentials : new ClientCredentials (
73- configParser . get ( profile , 'client_id' , '' ) ,
77+ clientId ,
7478 configParser . get ( profile , 'client_secret' , '' ) ,
7579 configParser . get (
7680 profile ,
7781 'client_credentials_url' ,
7882 DEFAULT_CLIENT_CREDENTIALS_URL ,
7983 ) ,
80- async ( ) => await readTokenCache ( profile ) ,
81- async cache => await writeTokenCache ( cache , profile ) ,
84+ async ( ) => await readTokenCache ( clientId ) ,
85+ async cache => await writeTokenCache ( clientId , cache ) ,
8286 ) ,
8387 } ;
8488
@@ -97,16 +101,15 @@ function resolveHome(path: string) {
97101 return path ;
98102}
99103
100- function makeTokenCachePath ( profile : string ) {
101- return resolveHome ( `~/.rai/${ profile } _cache.json` ) ;
102- }
104+ const CACHE_PATH = '~/.rai/tokens.json' ;
103105
104- async function readTokenCache ( profile = 'default' ) {
105- const cachePath = makeTokenCachePath ( profile ) ;
106+ async function readTokenCache ( clientId : string ) {
107+ const cachePath = resolveHome ( CACHE_PATH ) ;
106108
107109 try {
108110 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 ] ;
110113
111114 if ( cache . access_token && cache . created_on && cache . expires_in ) {
112115 return cache as AccessTokenCache ;
@@ -115,9 +118,20 @@ async function readTokenCache(profile = 'default') {
115118 } catch { }
116119}
117120
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 ) ;
121135
122136 await writeFile ( cachePath , cacheStr , 'utf-8' ) ;
123137}
0 commit comments