mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-10-13 21:09:31 -05:00
Move authentication functions
app no longer imports from cli
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import * as util from 'util';
|
||||
import * as crypto from 'crypto';
|
||||
import createDebug from 'debug';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
import { getNintendoAccountSessionToken } from '../../api/na.js';
|
||||
import { ZNMA_CLIENT_ID } from '../../api/moon.js';
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import createDebug from 'debug';
|
||||
import Table from '../util/table.js';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, hrduration, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:daily-summaries');
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import createDebug from 'debug';
|
||||
import Table from '../util/table.js';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:devices');
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ import * as path from 'path';
|
||||
import * as fs from 'fs/promises';
|
||||
import createDebug from 'debug';
|
||||
import mkdirp from 'mkdirp';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
import { DailySummaryResult } from '../../api/moon-types.js';
|
||||
import MoonApi from '../../api/moon.js';
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import createDebug from 'debug';
|
||||
import Table from '../util/table.js';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:monthly-summaries');
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import createDebug from 'debug';
|
||||
import Table from '../util/table.js';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:monthly-summary');
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import createDebug from 'debug';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:settings');
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import * as util from 'util';
|
||||
import createDebug from 'debug';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:token');
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import createDebug from 'debug';
|
||||
import type { Arguments as ParentArguments } from '../../cli.js';
|
||||
import type { Arguments as ParentArguments } from '../pctl.js';
|
||||
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
|
||||
import { getPctlToken } from './util.js';
|
||||
import { getPctlToken } from '../../common/auth/moon.js';
|
||||
|
||||
const debug = createDebug('cli:pctl:user');
|
||||
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
import createDebug from 'debug';
|
||||
import * as persist from 'node-persist';
|
||||
import { ZNMA_CLIENT_ID } from '../../api/moon.js';
|
||||
import { NintendoAccountSessionTokenJwtPayload, NintendoAccountToken, NintendoAccountUser } from '../../api/na.js';
|
||||
import { Jwt } from '../../util.js';
|
||||
import MoonApi from '../../api/moon.js';
|
||||
|
||||
const debug = createDebug('cli:nso');
|
||||
|
||||
export interface SavedMoonToken {
|
||||
nintendoAccountToken: NintendoAccountToken;
|
||||
user: NintendoAccountUser;
|
||||
|
||||
expires_at: number;
|
||||
}
|
||||
|
||||
export async function getPctlToken(storage: persist.LocalStorage, token: string) {
|
||||
if (!token) {
|
||||
console.error('No token set. Set a Nintendo Account session token using the `--token` option or by running `nxapi pctl auth`.');
|
||||
throw new Error('Invalid token');
|
||||
}
|
||||
|
||||
const [jwt, sig] = Jwt.decode<NintendoAccountSessionTokenJwtPayload>(token);
|
||||
|
||||
if (jwt.payload.iss !== 'https://accounts.nintendo.com') {
|
||||
throw new Error('Invalid Nintendo Account session token issuer');
|
||||
}
|
||||
if (jwt.payload.typ !== 'session_token') {
|
||||
throw new Error('Invalid Nintendo Account session token type');
|
||||
}
|
||||
if (jwt.payload.aud !== ZNMA_CLIENT_ID) {
|
||||
throw new Error('Invalid Nintendo Account session token audience');
|
||||
}
|
||||
if (jwt.payload.exp <= (Date.now() / 1000)) {
|
||||
throw new Error('Nintendo Account session token expired');
|
||||
}
|
||||
|
||||
// Nintendo Account session tokens use a HMAC SHA256 signature, so we can't verify this is valid
|
||||
|
||||
const existingToken: SavedMoonToken | undefined = await storage.getItem('MoonToken.' + token);
|
||||
|
||||
if (!existingToken || existingToken.expires_at <= Date.now()) {
|
||||
console.warn('Authenticating to Nintendo Switch Parental Controls app');
|
||||
debug('Authenticating to pctl with session token');
|
||||
|
||||
const {moon, data} = await MoonApi.createWithSessionToken(token);
|
||||
|
||||
const existingToken: SavedMoonToken = {
|
||||
...data,
|
||||
expires_at: Date.now() + (data.nintendoAccountToken.expires_in * 1000),
|
||||
};
|
||||
|
||||
await storage.setItem('MoonToken.' + token, existingToken);
|
||||
await storage.setItem('NintendoAccountToken-pctl.' + data.user.id, token);
|
||||
|
||||
return {moon, data: existingToken};
|
||||
}
|
||||
|
||||
debug('Using existing token');
|
||||
await storage.setItem('NintendoAccountToken-pctl.' + existingToken.user.id, token);
|
||||
|
||||
return {
|
||||
moon: new MoonApi(existingToken.nintendoAccountToken.access_token!, existingToken.user.id),
|
||||
data: existingToken,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user