Automatically renew SplatNet 3 tokens

This commit is contained in:
Samuel Elliott
2022-09-21 16:09:13 +01:00
parent 4e6a29f74f
commit c7fbe15047
3 changed files with 94 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
import createDebug from 'debug';
import persist from 'node-persist';
import { Response } from 'node-fetch';
import { getToken, Login } from './coral.js';
import SplatNet3Api, { SplatNet3AuthData } from '../../api/splatnet3.js';
import { checkUseLimit, SHOULD_LIMIT_USE } from './util.js';
@@ -50,16 +51,64 @@ export async function getBulletToken(
await storage.setItem('BulletToken.' + token, existingToken);
return {
splatnet: SplatNet3Api.createWithSavedToken(existingToken),
data: existingToken,
};
const splatnet = SplatNet3Api.createWithSavedToken(existingToken);
splatnet.onTokenExpired = createTokenExpiredHandler(storage, token, splatnet, existingToken, proxy_url);
splatnet.onTokenShouldRenew = createTokenShouldRenewHandler(storage, token, splatnet, existingToken, proxy_url);
return {splatnet, data: existingToken};
}
debug('Using existing token');
return {
splatnet: SplatNet3Api.createWithSavedToken(existingToken),
data: existingToken,
const splatnet = SplatNet3Api.createWithSavedToken(existingToken);
if (allow_fetch_token) {
splatnet.onTokenExpired = createTokenExpiredHandler(storage, token, splatnet, existingToken, proxy_url);
splatnet.onTokenShouldRenew = createTokenShouldRenewHandler(storage, token, splatnet, existingToken, proxy_url);
}
return {splatnet, data: existingToken};
}
function createTokenExpiredHandler(
storage: persist.LocalStorage, token: string, splatnet: SplatNet3Api, existingToken: SavedBulletToken,
znc_proxy_url?: string
) {
return (response: Response) => {
debug('Token expired, renewing');
return renewToken(storage, token, splatnet, existingToken, znc_proxy_url);
};
}
function createTokenShouldRenewHandler(
storage: persist.LocalStorage, token: string, splatnet: SplatNet3Api, existingToken: SavedBulletToken,
znc_proxy_url?: string
) {
return (remaining: number, response: Response) => {
debug('Token will expire in %d seconds, renewing', remaining);
return renewToken(storage, token, splatnet, existingToken, znc_proxy_url);
};
}
async function renewToken(
storage: persist.LocalStorage, token: string, splatnet: SplatNet3Api, previousToken: SavedBulletToken,
znc_proxy_url?: string
) {
const {nso, data} = await getToken(storage, token, znc_proxy_url);
if (data[Login]) {
const announcements = await nso.getAnnouncements();
const friends = await nso.getFriendList();
const webservices = await nso.getWebServices();
const activeevent = await nso.getActiveEvent();
}
const existingToken: SavedBulletToken = await SplatNet3Api.loginWithCoral(nso, data.user);
splatnet.bullet_token = existingToken.bullet_token.bulletToken;
splatnet.version = existingToken.version;
splatnet.language = existingToken.bullet_token.lang;
splatnet.useragent = existingToken.useragent;
await storage.setItem('BulletToken.' + token, existingToken);
}