From 53a15bbb993f31bf96e4d1739f7024ccad70b4a0 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Fri, 8 Apr 2022 22:13:43 +0100 Subject: [PATCH] Use /v3/Account/GetToken to renew the app token --- src/api/znc-proxy.ts | 4 ++-- src/api/znc-types.ts | 3 +++ src/api/znc.ts | 24 +++++++++++++++++------- src/cli/nso/notify.ts | 3 ++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/api/znc-proxy.ts b/src/api/znc-proxy.ts index be925af..c180f85 100644 --- a/src/api/znc-proxy.ts +++ b/src/api/znc-proxy.ts @@ -1,6 +1,6 @@ import fetch from 'node-fetch'; import createDebug from 'debug'; -import { ActiveEvent, Announcements, CurrentUser, Event, Friend, PresencePermissions, User, WebService, WebServiceToken, ZncStatus, ZncSuccessResponse } from './znc-types.js'; +import { AccountToken, ActiveEvent, Announcements, CurrentUser, Event, Friend, PresencePermissions, User, WebService, WebServiceToken, ZncStatus, ZncSuccessResponse } from './znc-types.js'; import { ErrorResponse } from './util.js'; import ZncApi from './znc.js'; import { SavedToken, version } from '../util.js'; @@ -115,7 +115,7 @@ export default class ZncProxyApi implements ZncApi { return {status: ZncStatus.OK as const, result: response.token, correlationId: ''}; } - async getToken(token: string, user: NintendoAccountUser): Promise> { + async getToken(token: string, user: NintendoAccountUser): ReturnType { throw new Error('Not supported in ZncProxyApi'); } diff --git a/src/api/znc-types.ts b/src/api/znc-types.ts index e1c9ba9..eab4081 100644 --- a/src/api/znc-types.ts +++ b/src/api/znc-types.ts @@ -34,6 +34,9 @@ export interface AccountLogin { }; } +/** /v3/Account/GetToken */ +export type AccountToken = AccountLogin; + /** /v1/Announcement/List */ export type Announcements = Announcement[]; diff --git a/src/api/znc.ts b/src/api/znc.ts index 1854e2f..8d30feb 100644 --- a/src/api/znc.ts +++ b/src/api/znc.ts @@ -2,7 +2,7 @@ import fetch from 'node-fetch'; import { v4 as uuidgen } from 'uuid'; import createDebug from 'debug'; import { flapg, FlapgIid, genfc } from './f.js'; -import { AccountLogin, Announcements, CurrentUser, CurrentUserPermissions, Event, Friends, GetActiveEventResult, PresencePermissions, User, WebServices, WebServiceToken, ZncResponse, ZncStatus } from './znc-types.js'; +import { AccountLogin, AccountToken, Announcements, CurrentUser, CurrentUserPermissions, Event, Friends, GetActiveEventResult, PresencePermissions, User, WebServices, WebServiceToken, ZncResponse, ZncStatus } from './znc-types.js'; import { getNintendoAccountToken, getNintendoAccountUser, NintendoAccountUser } from './na.js'; import { ErrorResponse, JwtPayload } from './util.js'; @@ -68,7 +68,7 @@ export default class ZncApi { } async removeFavouriteFriend(nsaid: string) { - return this.fetch<{}>('/v3/Friend/Favorite/Create', 'POST', JSON.stringify({ + return this.fetch<{}>('/v3/Friend/Favorite/Delete', 'POST', JSON.stringify({ parameter: { nsaId: nsaid, }, @@ -156,21 +156,31 @@ export default class ZncApi { const id_token = nintendoAccountToken.id_token; const useragent = this.useragent ?? undefined; - const data = process.env.ZNCA_API_URL ? + const flapgdata = process.env.ZNCA_API_URL ? await genfc(process.env.ZNCA_API_URL + '/f', id_token, timestamp, uuid, FlapgIid.NSO, useragent) : await flapg(id_token, timestamp, uuid, FlapgIid.NSO, useragent); const req = { naBirthday: user.birthday, timestamp, - f: data.f, + f: flapgdata.f, requestId: uuid, naIdToken: this.token, }; - return this.fetch('/v3/Account/GetToken', 'POST', JSON.stringify({ + const data = await this.fetch('/v3/Account/GetToken', 'POST', JSON.stringify({ parameter: req, })); + + return { + uuid, + timestamp, + nintendoAccountToken, + // user, + flapg: flapgdata, + nsoAccount: data.result, + credential: data.result.webApiServerCredential, + }; } static async createWithSessionToken(token: string, useragent = ZncApi.useragent) { @@ -182,8 +192,8 @@ export default class ZncApi { }; } - async renewToken(token: string) { - const data = await ZncApi.loginWithSessionToken(token, this.useragent); + async renewToken(token: string, user: NintendoAccountUser) { + const data = await this.getToken(token, user); this.token = data.credential.accessToken; diff --git a/src/cli/nso/notify.ts b/src/cli/nso/notify.ts index b59d043..a1c0e28 100644 --- a/src/cli/nso/notify.ts +++ b/src/cli/nso/notify.ts @@ -432,9 +432,10 @@ export class ZncNotifications extends Loop { // Token expired debug('Renewing token'); - const data = await this.nso.renewToken(this.token); + const data = await this.nso.renewToken(this.token, this.data.user); const existingToken: SavedToken = { + user: this.data.user, ...data, expires_at: Date.now() + (data.credential.expiresIn * 1000), };