mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-09-07 19:05:09 -05:00
SplatNet 3 authentication
This commit is contained in:
@@ -81,6 +81,9 @@ export default class NooklinkApi {
|
||||
}
|
||||
|
||||
static async loginWithCoral(nso: CoralApi, user: NintendoAccountUser) {
|
||||
const { default: { coral_gws_nooklink: config } } = await import('../common/remote-config.js');
|
||||
if (!config) throw new Error('Remote configuration prevents NookLink authentication');
|
||||
|
||||
const webserviceToken = await nso.getWebServiceToken(NOOKLINK_WEBSERVICE_ID);
|
||||
|
||||
return this.loginWithWebServiceToken(webserviceToken, user);
|
||||
|
||||
27
src/api/splatnet3-types.ts
Normal file
27
src/api/splatnet3-types.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/** /bullet_tokens */
|
||||
export interface BulletToken {
|
||||
bulletToken: string;
|
||||
lang: string;
|
||||
is_noe_country: 'true' | unknown;
|
||||
// ...
|
||||
}
|
||||
|
||||
/** /graphql */
|
||||
export interface GraphQLRequest<Variables extends unknown> {
|
||||
variables: Variables;
|
||||
extensions: {
|
||||
persistedQuery: {
|
||||
version: 1;
|
||||
sha256Hash: RequestParameters['id'];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export interface RequestParameters {
|
||||
id: string;
|
||||
// ...
|
||||
}
|
||||
|
||||
export interface GraphQLResponse<T = unknown> {
|
||||
// ...
|
||||
}
|
||||
223
src/api/splatnet3.ts
Normal file
223
src/api/splatnet3.ts
Normal file
@@ -0,0 +1,223 @@
|
||||
import fetch from 'node-fetch';
|
||||
import createDebug from 'debug';
|
||||
import { WebServiceToken } from './coral-types.js';
|
||||
import { NintendoAccountUser } from './na.js';
|
||||
import { defineResponse, ErrorResponse } from './util.js';
|
||||
import CoralApi from './coral.js';
|
||||
import { timeoutSignal } from '../util/misc.js';
|
||||
import { BulletToken, GraphQLRequest, GraphQLResponse, RequestParameters } from './splatnet3-types.js';
|
||||
|
||||
const debug = createDebug('nxapi:api:splatnet3');
|
||||
|
||||
export const SPLATNET3_WEBSERVICE_ID = '4834290508791808';
|
||||
export const SPLATNET3_WEBSERVICE_URL = 'https://api.lp1.av5ja.srv.nintendo.net';
|
||||
export const SPLATNET3_WEBSERVICE_USERAGENT = 'Mozilla/5.0 (Linux; Android 8.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.125 Mobile Safari/537.36';
|
||||
|
||||
const languages = [
|
||||
'de-DE', 'en-GB', 'en-US', 'es-ES', 'es-MX', 'fr-CA',
|
||||
'fr-FR', 'it-IT', 'ja-JP', 'ko-KR', 'nl-NL', 'ru-RU',
|
||||
'zh-CN', 'zh-TW',
|
||||
];
|
||||
|
||||
const SPLATNET3_URL = SPLATNET3_WEBSERVICE_URL + '/api';
|
||||
|
||||
export default class SplatNet3Api {
|
||||
protected constructor(
|
||||
public bullet_token: string,
|
||||
public version: string,
|
||||
public language: string,
|
||||
public useragent: string,
|
||||
) {}
|
||||
|
||||
async fetch<T = unknown>(url: string, method = 'GET', body?: string | FormData, headers?: object) {
|
||||
const [signal, cancel] = timeoutSignal();
|
||||
const response = await fetch(SPLATNET3_URL + url, {
|
||||
method,
|
||||
headers: Object.assign({
|
||||
'User-Agent': this.useragent,
|
||||
'Accept': '*/*',
|
||||
'Referrer': 'https://api.lp1.av5ja.srv.nintendo.net/',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'authorization': 'Bearer ' + this.bullet_token,
|
||||
'content-type': 'application/json',
|
||||
'X-Web-View-Ver': this.version,
|
||||
'Accept-Language': this.language,
|
||||
}, headers),
|
||||
body,
|
||||
signal,
|
||||
}).finally(cancel);
|
||||
|
||||
debug('fetch %s %s, response %s', method, url, response.status);
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new ErrorResponse('[splatnet3] Non-200 status code', response, await response.text());
|
||||
}
|
||||
|
||||
const data = await response.json() as T;
|
||||
|
||||
return defineResponse(data, response);
|
||||
}
|
||||
|
||||
async graphql<T = unknown, V = unknown>(request_parameters: RequestParameters, variables: V) {
|
||||
const req: GraphQLRequest<V> = {
|
||||
variables,
|
||||
extensions: {
|
||||
persistedQuery: {
|
||||
version: 1,
|
||||
sha256Hash: request_parameters.id,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const data = await this.fetch<GraphQLResponse<T>>('/graphql', 'POST', JSON.stringify(req));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static async createWithCoral(nso: CoralApi, user: NintendoAccountUser) {
|
||||
const data = await this.loginWithCoral(nso, user);
|
||||
return {splatnet: this.createWithSavedToken(data), data};
|
||||
}
|
||||
|
||||
static createWithSavedToken(data: SplatNet3AuthData) {
|
||||
return new this(
|
||||
data.bullet_token.bulletToken,
|
||||
data.version,
|
||||
data.bullet_token.lang,
|
||||
data.useragent,
|
||||
);
|
||||
}
|
||||
|
||||
static createWithCliTokenData(data: SplatNet3CliTokenData) {
|
||||
return new this(
|
||||
data.bullet_token,
|
||||
data.version,
|
||||
data.language,
|
||||
SPLATNET3_WEBSERVICE_USERAGENT,
|
||||
);
|
||||
}
|
||||
|
||||
static async loginWithCoral(nso: CoralApi, user: NintendoAccountUser) {
|
||||
const { default: { coral_gws_splatnet3: config } } = await import('../common/remote-config.js');
|
||||
if (!config) throw new Error('Remote configuration prevents SplatNet 3 authentication');
|
||||
|
||||
const webserviceToken = await nso.getWebServiceToken(SPLATNET3_WEBSERVICE_ID);
|
||||
|
||||
return this.loginWithWebServiceToken(webserviceToken, user);
|
||||
}
|
||||
|
||||
static async loginWithWebServiceToken(
|
||||
webserviceToken: WebServiceToken, user: NintendoAccountUser
|
||||
): Promise<SplatNet3AuthData> {
|
||||
const { default: { coral_gws_splatnet3: config } } = await import('../common/remote-config.js');
|
||||
if (!config) throw new Error('Remote configuration prevents SplatNet 3 authentication');
|
||||
|
||||
const language = languages.includes(user.language) ? user.language : 'en-GB';
|
||||
const version = config.app_ver ?? config.version + '-' + config.revision.substr(0, 8);
|
||||
|
||||
const url = new URL(SPLATNET3_WEBSERVICE_URL);
|
||||
url.search = new URLSearchParams({
|
||||
lang: user.language,
|
||||
na_country: user.country,
|
||||
na_lang: user.language,
|
||||
}).toString();
|
||||
|
||||
const [signal, cancel] = timeoutSignal();
|
||||
const response = await fetch(url.toString(), {
|
||||
headers: {
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
'User-Agent': SPLATNET3_WEBSERVICE_USERAGENT,
|
||||
'x-appcolorscheme': 'DARK',
|
||||
'x-gamewebtoken': webserviceToken.accessToken,
|
||||
'dnt': '1',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-GB,en-US;q=0.8',
|
||||
'X-Requested-With': 'com.nintendo.znca',
|
||||
},
|
||||
signal,
|
||||
}).finally(cancel);
|
||||
|
||||
debug('fetch %s %s, response %s', 'GET', url, response.status);
|
||||
|
||||
const body = await response.text();
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new ErrorResponse('[splatnet3] Non-200 status code', response, body);
|
||||
}
|
||||
|
||||
const cookies = response.headers.get('Set-Cookie');
|
||||
|
||||
const [signal2, cancel2] = timeoutSignal();
|
||||
const token_response = await fetch(SPLATNET3_URL + '/bullet_tokens', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'User-Agent': SPLATNET3_WEBSERVICE_USERAGENT,
|
||||
'Accept': '*/*',
|
||||
'Referrer': 'https://api.lp1.av5ja.srv.nintendo.net/',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/json',
|
||||
'X-Web-View-Ver': version,
|
||||
'X-NACOUNTRY': user.country,
|
||||
'Accept-Language': language,
|
||||
'X-GameWebToken': webserviceToken.accessToken,
|
||||
},
|
||||
body: '',
|
||||
signal: signal2,
|
||||
}).finally(cancel2);
|
||||
|
||||
debug('fetch %s %s, response %s', 'POST', '/bullet_tokens', response.status);
|
||||
|
||||
if (token_response.status === 401) {
|
||||
throw new ErrorResponse('[splatnet3] ERROR_INVALID_GAME_WEB_TOKEN', token_response, await token_response.text());
|
||||
}
|
||||
if (token_response.status === 403) {
|
||||
throw new ErrorResponse('[splatnet3] ERROR_OBSOLETE_VERSION', token_response, await token_response.text());
|
||||
}
|
||||
if (token_response.status === 204) {
|
||||
throw new ErrorResponse('[splatnet3] USER_NOT_REGISTERED', token_response, await token_response.text());
|
||||
}
|
||||
if (token_response.status !== 200) {
|
||||
throw new ErrorResponse('[splatnet3] Non-200 status code', token_response, await token_response.text());
|
||||
}
|
||||
|
||||
const bullet_token = await token_response.json() as BulletToken;
|
||||
const expires_at = Date.now() + (2 * 60 * 60 * 1000); // ??
|
||||
|
||||
return {
|
||||
webserviceToken,
|
||||
url: url.toString(),
|
||||
cookies,
|
||||
body,
|
||||
|
||||
language,
|
||||
country: user.country,
|
||||
version,
|
||||
|
||||
bullet_token,
|
||||
expires_at,
|
||||
useragent: SPLATNET3_WEBSERVICE_USERAGENT,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export interface SplatNet3AuthData {
|
||||
webserviceToken: WebServiceToken;
|
||||
url: string;
|
||||
cookies: string | null;
|
||||
body: string;
|
||||
|
||||
language: string;
|
||||
country: string;
|
||||
version: string;
|
||||
|
||||
bullet_token: BulletToken;
|
||||
expires_at: number;
|
||||
useragent: string;
|
||||
}
|
||||
|
||||
export interface SplatNet3CliTokenData {
|
||||
bullet_token: string;
|
||||
expires_at: number;
|
||||
language: string;
|
||||
version: string;
|
||||
}
|
||||
Reference in New Issue
Block a user