SplatNet 3 authentication

This commit is contained in:
Samuel Elliott
2022-09-08 19:44:15 +01:00
parent 80afc62268
commit 1aa4aad85a
16 changed files with 481 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ export * as users from './users.js';
export * as nso from './nso.js';
export * as splatnet2 from './splatnet2.js';
export * as nooklink from './nooklink.js';
export * as splatnet3 from './splatnet3.js';
export * as pctl from './pctl.js';
export * as androidZncaApiServerFrida from './android-znca-api-server-frida.js';
export * as util from './util.js';

View File

@@ -59,7 +59,7 @@ export async function handler(argv: ArgumentsCamelCase<Arguments>) {
// This means the other user had already sent this user a friend request,
// so sending them a friend request just accepted theirs
const friends = await nso.getFriendList();
const friend = friends.result.friends.find(f => f.nsaId === nsa_id);
const friend = friends.friends.find(f => f.nsaId === nsa_id);
if (friend) {
console.log('You are now friends with %s.', friend.name);

29
src/cli/splatnet3.ts Normal file
View File

@@ -0,0 +1,29 @@
import process from 'node:process';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import * as commands from './splatnet3/index.js';
const debug = createDebug('cli:splatnet3');
export const command = 'splatnet3 <command>';
export const desc = 'SplatNet 3';
export function builder(yargs: Argv<ParentArguments>) {
for (const command of Object.values(commands)) {
// @ts-expect-error
yargs.command(command);
}
return yargs.option('znc-proxy-url', {
describe: 'URL of Nintendo Switch Online app API proxy server to use',
type: 'string',
default: process.env.ZNC_PROXY_URL,
}).option('auto-update-session', {
describe: 'Automatically obtain and refresh the SplatNet 3 access token',
type: 'boolean',
default: true,
});
}
export type Arguments = YargsArguments<ReturnType<typeof builder>>;

View File

@@ -0,0 +1,2 @@
export * as user from './user.js';
export * as token from './token.js';

View File

@@ -0,0 +1,51 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../splatnet3.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getBulletToken } from '../../common/auth/splatnet3.js';
import { SplatNet3CliTokenData } from '../../api/splatnet3.js';
const debug = createDebug('cli:splatnet3:token');
export const command = 'token';
export const desc = 'Get the authenticated Nintendo Account\'s SplatNet 3 user data and access token';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.option('user', {
describe: 'Nintendo Account ID',
type: 'string',
}).option('token', {
describe: 'Nintendo Account session token',
type: 'string',
}).option('json', {
describe: 'Output raw JSON',
type: 'boolean',
}).option('json-pretty-print', {
describe: 'Output pretty-printed JSON',
type: 'boolean',
});
}
type Arguments = YargsArguments<ReturnType<typeof builder>>;
export async function handler(argv: ArgumentsCamelCase<Arguments>) {
const storage = await initStorage(argv.dataPath);
const usernsid = argv.user ?? await storage.getItem('SelectedUser');
const token: string = argv.token || await storage.getItem('NintendoAccountToken.' + usernsid);
const {splatnet, data} = await getBulletToken(storage, token, argv.zncProxyUrl, argv.autoUpdateSession);
if (argv.json || argv.jsonPrettyPrint) {
const result: SplatNet3CliTokenData = {
bullet_token: data.bullet_token.bulletToken,
expires_at: data.expires_at,
language: data.bullet_token.lang,
version: data.version,
};
console.log(JSON.stringify(result, null, argv.jsonPrettyPrint ? 4 : 0));
return;
}
console.log(data.bullet_token.bulletToken);
}

33
src/cli/splatnet3/user.ts Normal file
View File

@@ -0,0 +1,33 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../splatnet3.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getBulletToken } from '../../common/auth/splatnet3.js';
const debug = createDebug('cli:splatnet3:user');
export const command = 'user';
export const desc = 'Get the authenticated Nintendo Account\'s player record';
export function builder(yargs: Argv<ParentArguments>) {
return yargs.option('user', {
describe: 'Nintendo Account ID',
type: 'string',
}).option('token', {
describe: 'Nintendo Account session token',
type: 'string',
});
}
type Arguments = YargsArguments<ReturnType<typeof builder>>;
export async function handler(argv: ArgumentsCamelCase<Arguments>) {
const storage = await initStorage(argv.dataPath);
const usernsid = argv.user ?? await storage.getItem('SelectedUser');
const token: string = argv.token ||
await storage.getItem('NintendoAccountToken.' + usernsid);
const {splatnet, data} = await getBulletToken(storage, token, argv.zncProxyUrl, argv.autoUpdateSession);
throw new Error('Not implemented');
}