Skip to content

Commit b284d5c

Browse files
committed
Move token fetching to its own function
1 parent cdd1c9d commit b284d5c

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

scripts/background.js

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let checkoutSessionId;
55
let userDataCache = null;
66
let userFamilyDataCache = null;
77
let userFamilySemaphore = null;
8+
let tokenSemaphore = null;
89
let nextAllowedRequest = 0;
910

1011
/** @type {browser} ExtensionApi */
@@ -196,24 +197,9 @@ async function FetchSteamUserFamilyData( callback )
196197

197198
try
198199
{
199-
const tokenResponseFetch = await fetch(
200-
`https://store.steampowered.com/pointssummary/ajaxgetasyncconfig`,
201-
{
202-
credentials: 'include',
203-
headers: {
204-
Accept: 'application/json',
205-
},
206-
}
207-
);
208-
const token = await tokenResponseFetch.json();
209-
210-
if( !token || !token.success || !token.data || !token.data.webapi_token )
211-
{
212-
throw new Error( 'Are you logged on the Steam Store in this browser?' );
213-
}
214-
200+
const token = await GetStoreToken();
215201
const paramsSharedLibrary = new URLSearchParams();
216-
paramsSharedLibrary.set( 'access_token', token.data.webapi_token );
202+
paramsSharedLibrary.set( 'access_token', token );
217203
paramsSharedLibrary.set( 'family_groupid', '0' ); // family_groupid is ignored
218204
paramsSharedLibrary.set( 'include_excluded', 'true' );
219205
paramsSharedLibrary.set( 'include_free', 'true' );
@@ -402,6 +388,62 @@ function GetAchievementsGroups( appid, callback )
402388
.catch( ( error ) => callback( { success: false, error: error.message } ) );
403389
}
404390

391+
/**
392+
* @return {Promise<String>}
393+
*/
394+
async function GetStoreToken()
395+
{
396+
if( tokenSemaphore !== null )
397+
{
398+
return await tokenSemaphore ;
399+
}
400+
let token = null;
401+
let semaphoreResolve = null;
402+
tokenSemaphore = new Promise( resolve =>
403+
{
404+
semaphoreResolve = resolve;
405+
} );
406+
407+
try
408+
{
409+
token = await GetLocalOption( { storetoken: false } ).then( data => data.storetoken );
410+
if( token )
411+
{
412+
const jwt = token.split( '.' );
413+
const payload = JSON.parse( atob( jwt[ 1 ] ) );
414+
const expiration = payload.exp * 1000;
415+
if( Date.now() < expiration )
416+
{
417+
return token;
418+
}
419+
}
420+
421+
token = await fetch(
422+
`https://store.steampowered.com/pointssummary/ajaxgetasyncconfig`,
423+
{
424+
credentials: 'include',
425+
headers: {
426+
Accept: 'application/json',
427+
},
428+
}
429+
).then( response =>response.json() );
430+
431+
if( !token || !token.success || !token.data || !token.data.webapi_token )
432+
{
433+
throw new Error( 'Are you logged on the Steam Store in this browser?' );
434+
}
435+
436+
await SetLocalOption( 'storetoken', token.data.webapi_token );
437+
438+
return token.data.webapi_token;
439+
}
440+
finally
441+
{
442+
semaphoreResolve( token );
443+
tokenSemaphore = null;
444+
};
445+
}
446+
405447
/**
406448
* @param {Function} callback
407449
*/

0 commit comments

Comments
 (0)