forked from samuelthomas2774/nxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoral.ts
119 lines (95 loc) · 4.2 KB
/
coral.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import createDebug from 'debug';
import * as persist from 'node-persist';
import { Response } from 'node-fetch';
import { NintendoAccountSessionTokenJwtPayload } from '../../api/na.js';
import { Jwt } from '../../util/jwt.js';
import { CoralErrorResponse } from '../../api/coral-types.js';
import CoralApi, { CoralAuthData, ZNCA_CLIENT_ID } from '../../api/coral.js';
import ZncProxyApi from '../../api/znc-proxy.js';
import { checkUseLimit, SHOULD_LIMIT_USE } from './util.js';
const debug = createDebug('nxapi:auth:coral');
export const Login = Symbol('Login');
export interface SavedToken extends CoralAuthData {
expires_at: number;
proxy_url?: string;
/** Indicates we just logged in and didn't use a cached token */
[Login]?: boolean;
}
export async function getToken(
storage: persist.LocalStorage, token: string, proxy_url: string, ratelimit?: boolean
): Promise<{
nso: ZncProxyApi;
data: SavedToken;
}>
export async function getToken(
storage: persist.LocalStorage, token: string, proxy_url?: string, ratelimit?: boolean
): Promise<{
nso: CoralApi;
data: SavedToken;
}>
export async function getToken(
storage: persist.LocalStorage, token: string, proxy_url?: string, ratelimit = SHOULD_LIMIT_USE
) {
if (!token) {
console.error('No token set. Set a Nintendo Account session token using the `--token` option or by running `nxapi nso token`.');
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 !== ZNCA_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: SavedToken | undefined = await storage.getItem('NsoToken.' + token);
if (!existingToken || existingToken.expires_at <= Date.now()) {
await checkUseLimit(storage, 'coral', jwt.payload.sub, ratelimit);
console.warn('Authenticating to Nintendo Switch Online app');
debug('Authenticating to znc with session token');
const {nso, data} = proxy_url ?
await ZncProxyApi.createWithSessionToken(proxy_url, token) :
await CoralApi.createWithSessionToken(token);
const existingToken: SavedToken = {
...data,
expires_at: Date.now() + (data.credential.expiresIn * 1000),
};
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, existingToken);
await storage.setItem('NsoToken.' + token, existingToken);
await storage.setItem('NintendoAccountToken.' + data.user.id, token);
existingToken[Login] = true;
return {nso, data: existingToken};
}
debug('Using existing token');
await storage.setItem('NintendoAccountToken.' + existingToken.user.id, token);
const nso = proxy_url ?
new ZncProxyApi(proxy_url, token) :
CoralApi.createWithSavedToken(existingToken);
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, existingToken);
return {nso, data: existingToken};
}
function createTokenExpiredHandler(
storage: persist.LocalStorage, token: string, nso: CoralApi, existingToken: SavedToken
) {
return (data: CoralErrorResponse, response: Response) => {
debug('Token expired', existingToken.user.id, data);
return renewToken(storage, token, nso, existingToken);
};
}
async function renewToken(
storage: persist.LocalStorage, token: string, nso: CoralApi, previousToken: SavedToken
) {
const data = await nso.renewToken(token, previousToken.user);
const existingToken: SavedToken = {
...previousToken,
...data,
expires_at: Date.now() + (data.credential.expiresIn * 1000),
};
await storage.setItem('NsoToken.' + token, existingToken);
}