Online mode fix & npm package updates

This commit is contained in:
q13x 2024-03-06 16:41:57 -08:00
parent 67c3e0bbda
commit 375a275442
3 changed files with 641 additions and 524 deletions

908
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,22 +9,23 @@
"author": "WorldEditAxe, q13x", "author": "WorldEditAxe, q13x",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@thi.ng/leb128": "^3.0.5", "@thi.ng/leb128": "3.0.5",
"@types/node": "^18.11.18", "@types/node": "18.11.18",
"@types/semver": "^7.3.13", "@types/semver": "7.3.13",
"@types/sharp": "^0.31.1", "@types/sharp": "0.31.1",
"@types/ws": "^8.5.4", "@types/ws": "8.5.4",
"chalk": "^5.2.0", "chalk": "5.2.0",
"dotenv": "^16.0.3", "dotenv": "16.0.3",
"minecraft-protocol": "^1.40.2", "minecraft-protocol": "^1.26.5",
"node-fetch": "^3.3.1", "node-fetch": "3.3.1",
"parse-domain": "^7.0.1", "parse-domain": "7.0.1",
"prismarine-block": "^1.16.3", "prismarine-auth": "^2.4.1",
"prismarine-chunk": "^1.33.0", "prismarine-block": "1.16.3",
"prismarine-registry": "^1.6.0", "prismarine-chunk": "1.33.0",
"semver": "^7.3.8", "prismarine-registry": "1.6.0",
"sharp": "^0.31.3", "semver": "^7.6.0",
"ws": "^8.12.0" "sharp": "^0.33.2",
"ws": "8.12.0"
}, },
"type": "module" "type": "module"
} }

View File

@ -12,41 +12,23 @@ import XboxTokenManager from "prismarine-auth/src/TokenManagers/XboxTokenManager
import MsaTokenManager from "prismarine-auth/src/TokenManagers/MsaTokenManager.js"; import MsaTokenManager from "prismarine-auth/src/TokenManagers/MsaTokenManager.js";
import BedrockTokenManager from "prismarine-auth/src/TokenManagers/MinecraftBedrockTokenManager.js"; import BedrockTokenManager from "prismarine-auth/src/TokenManagers/MinecraftBedrockTokenManager.js";
/*
MIT License
Copyright (c) 2020 PrismarineJS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
async function retry(methodFn, beforeRetry, times) { async function retry(methodFn, beforeRetry, times) {
for (let attempts = 0; attempts < times; attempts++) { while (times--) {
if (times !== 0) {
try { try {
return await methodFn(); return await methodFn();
} catch (err) { } catch (e) {
if (err instanceof URIError) { if (e instanceof URIError) {
throw err; throw e;
} else {
// debug(e);
} }
} }
await new Promise((resolve) => setTimeout(resolve, 2000)); await new Promise((resolve) => setTimeout(resolve, 2000));
await beforeRetry(); await beforeRetry();
} else {
return await methodFn();
}
} }
} }
@ -63,9 +45,7 @@ export class CustomAuthflow {
constructor(username = "", cache, options, codeCallback) { constructor(username = "", cache, options, codeCallback) {
this.username = username; this.username = username;
if (options && !options.flow) { if (options && !options.flow) {
throw new Error( throw new Error("Missing 'flow' argument in options. See docs for more information.");
"Missing 'flow' argument in options. See docs for more information."
);
} }
this.options = options || { flow: "msal" }; this.options = options || { flow: "msal" };
this.initTokenManagers(username, cache); this.initTokenManagers(username, cache);
@ -74,49 +54,33 @@ export class CustomAuthflow {
initTokenManagers(username, cache) { initTokenManagers(username, cache) {
if (this.options.flow === "live" || this.options.flow === "sisu") { if (this.options.flow === "live" || this.options.flow === "sisu") {
if (!this.options.authTitle) if (!this.options.authTitle) throw new Error(`Please specify an "authTitle" in Authflow constructor when using ${this.options.flow} flow`);
throw new Error( this.msa = new LiveTokenManager(this.options.authTitle, ["service::user.auth.xboxlive.com::MBI_SSL"], cache({ cacheName: this.options.flow, username }));
`Please specify an "authTitle" in Authflow constructor when using ${this.options.flow} flow`
);
this.msa = new LiveTokenManager(
this.options.authTitle,
["service::user.auth.xboxlive.com::MBI_SSL"],
cache({ cacheName: this.options.flow, username })
);
this.doTitleAuth = true; this.doTitleAuth = true;
} else if (this.options.flow === "msal") { } else if (this.options.flow === "msal") {
const config = Object.assign( const config = Object.assign({ ...msalConfig }, this.options.authTitle ? { auth: { ...msalConfig.auth, clientId: this.options.authTitle } } : {});
{ ...msalConfig }, this.msa = new MsaTokenManager(config, ["XboxLive.signin", "offline_access"], cache({ cacheName: "msal", username }));
this.options.authTitle
? { auth: { ...msalConfig.auth, clientId: this.options.authTitle } }
: {}
);
this.msa = new MsaTokenManager(
config,
["XboxLive.signin", "offline_access"],
cache({ cacheName: "msal", username })
);
} else { } else {
throw new Error( throw new Error(`Unknown flow: ${this.options.flow} (expected "live", "sisu", or "msal")`);
`Unknown flow: ${this.options.flow} (expected "live", "sisu", or "msal")`
);
} }
const keyPair = crypto.generateKeyPairSync("ec", { namedCurve: "P-256" }); const keyPair = crypto.generateKeyPairSync("ec", { namedCurve: "P-256" });
this.xbl = new XboxTokenManager( this.xbl = new XboxTokenManager(keyPair, cache({ cacheName: "xbl", username }));
keyPair,
cache({ cacheName: "xbl", username })
);
this.mba = new BedrockTokenManager(cache({ cacheName: "bed", username })); this.mba = new BedrockTokenManager(cache({ cacheName: "bed", username }));
this.mca = new JavaTokenManager(cache({ cacheName: "mca", username })); this.mca = new JavaTokenManager(cache({ cacheName: "mca", username }));
} }
static resetTokenCaches(cache) { static resetTokenCaches(cache) {
if (!cache) throw new Error("You must provide a cache directory to reset."); if (!cache) throw new Error("You must provide a cache directory to reset.");
try {
if (fs.existsSync(cache)) { if (fs.existsSync(cache)) {
fs.rmSync(cache, { recursive: true }); fs.rmSync(cache, { recursive: true });
return true; return true;
} }
} catch (e) {
console.log("Failed to clear cache dir", e);
return false;
}
} }
async getMsaToken() { async getMsaToken() {
@ -130,59 +94,48 @@ export class CustomAuthflow {
console.info(response.message); console.info(response.message);
}); });
if (ret.account) {
console.info(`[msa] Signed in as ${ret.account.username}`);
} else {
// We don't get extra account data here per scope
console.info("[msa] Signed in with Microsoft");
}
return ret.accessToken; return ret.accessToken;
} }
} }
async getXboxToken( async getXboxToken(relyingParty = this.options.relyingParty || Endpoints.XboxRelyingParty, forceRefresh = false) {
relyingParty = this.options.relyingParty || Endpoints.XboxRelyingParty
) {
const options = { ...this.options, relyingParty }; const options = { ...this.options, relyingParty };
if (await this.xbl.verifyTokens(relyingParty)) {
const { data } = await this.xbl.getCachedXstsToken(relyingParty); const { xstsToken, userToken, deviceToken, titleToken } = await this.xbl.getCachedTokens(relyingParty);
return data;
} else if (options.password) { if (xstsToken.valid && !forceRefresh) {
const xsts = await this.xbl.doReplayAuth( return xstsToken.data;
this.username, }
options.password,
options if (options.password) {
); const xsts = await this.xbl.doReplayAuth(this.username, options.password, options);
return xsts; return xsts;
} else { }
return await retry( return await retry(
async () => { async () => {
const msaToken = await this.getMsaToken(); const msaToken = await this.getMsaToken();
if (options.flow === "sisu") { // sisu flow generates user and title tokens differently to other flows and should also be used to refresh them if they are invalid
const deviceToken = await this.xbl.getDeviceToken(options); if (options.flow === "sisu" && (!userToken.valid || !deviceToken.valid || !titleToken.valid)) {
const sisu = await this.xbl.doSisuAuth( const dt = await this.xbl.getDeviceToken(options);
msaToken, const sisu = await this.xbl.doSisuAuth(msaToken, dt, options);
deviceToken,
options
);
return sisu; return sisu;
} }
const userToken = await this.xbl.getUserToken( const ut = userToken.token ?? (await this.xbl.getUserToken(msaToken, options.flow === "msal"));
msaToken, const dt = deviceToken.token ?? (await this.xbl.getDeviceToken(options));
options.flow === "msal" const tt = titleToken.token ?? (this.doTitleAuth ? await this.xbl.getTitleToken(msaToken, dt) : undefined);
);
if (this.doTitleAuth) { const xsts = await this.xbl.getXSTSToken({ userToken: ut, deviceToken: dt, titleToken: tt }, options);
const deviceToken = await this.xbl.getDeviceToken(options);
const titleToken = await this.xbl.getTitleToken(
msaToken,
deviceToken
);
const xsts = await this.xbl.getXSTSToken(
{ userToken, deviceToken, titleToken },
options
);
return xsts; return xsts;
} else {
const xsts = await this.xbl.getXSTSToken({ userToken }, options);
return xsts;
}
}, },
() => { () => {
this.msa.forceRefresh = true; this.msa.forceRefresh = true;
@ -190,10 +143,9 @@ export class CustomAuthflow {
2 2
); );
} }
}
async getMinecraftJavaToken(options: any = {}) { async getMinecraftJavaToken(options: any = {}) {
const response: any = { token: "", entitlements: {}, profile: {} }; const response: any = { token: "", entitlements: {} as any, profile: {} as any };
if (await this.mca.verifyTokens()) { if (await this.mca.verifyTokens()) {
const { token } = await this.mca.getCachedAccessToken(); const { token } = await this.mca.getCachedAccessToken();
response.token = token; response.token = token;
@ -211,15 +163,43 @@ export class CustomAuthflow {
} }
if (options.fetchEntitlements) { if (options.fetchEntitlements) {
response.entitlements = await this.mca.fetchEntitlements(response.token); response.entitlements = await this.mca.fetchEntitlements(response.token).catch((e) => {});
} }
if (options.fetchProfile) { if (options.fetchProfile) {
response.profile = await this.mca.fetchProfile(response.token); response.profile = await this.mca.fetchProfile(response.token).catch((e) => {});
} }
if (options.fetchCertificates) { if (options.fetchCertificates) {
response.certificates = await this.mca.fetchCertificates(response.token); response.certificates = await this.mca.fetchCertificates(response.token).catch((e) => []);
} }
return response; return response;
} }
async getMinecraftBedrockToken(publicKey) {
// TODO: Fix cache, in order to do cache we also need to cache the ECDH keys so disable it
// is this even a good idea to cache?
if ((await this.mba.verifyTokens()) && false) {
// eslint-disable-line
const { chain } = this.mba.getCachedAccessToken();
return chain;
} else {
if (!publicKey) throw new Error("Need to specifiy a ECDH x509 URL encoded public key");
return await retry(
async () => {
const xsts = await this.getXboxToken(Endpoints.BedrockXSTSRelyingParty);
const token = await this.mba.getAccessToken(publicKey, xsts);
// If we want to auth with a title ID, make sure there's a TitleID in the response
const body = JSON.parse(Buffer.from(token.chain[1].split(".")[1], "base64").toString());
if (!body.extraData.titleId && this.doTitleAuth) {
throw Error("missing titleId in response");
}
return token.chain;
},
() => {
this.xbl.forceRefresh = true;
},
2
);
}
}
} }