UNPKG

friend-connect

Version:

**BEFORE YOU USE THIS TOOL, PLEASE READ THE FOLLOWING: WE _AS CONTRIBUTORS_ ARE NOT RESPONSIBLE FOR ANY DAMAGE OR LOSS CAUSED BY THIS APP. USE AN ALT ACCOUNT, JUST IN CASE THERE IS AN ISSUE WITH THIS METHOD.**

157 lines (156 loc) 7.18 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _LinkCodeTokenProvider_isTokenRefreshing, _LinkCodeTokenProvider_token; import { createHash } from "prismarine-auth/src/common/Util.js"; import Achievements from "./modules/achievements"; import authPkg from "prismarine-auth"; const { Authflow } = authPkg; import fs from "fs"; import EventEmitter from "events"; import SessionDirectory from "./modules/sessionDirectory"; import Social from "./modules/social"; import PeopleHub from "./modules/peopleHub"; export class LinkCodeTokenProvider extends EventEmitter { constructor(email, tokenPath, options, codeCallback) { super(); _LinkCodeTokenProvider_isTokenRefreshing.set(this, true); _LinkCodeTokenProvider_token.set(this, void 0); this.email = email; this.authOptions = options || {}; this.cachePath = tokenPath || "./auth"; this.emailHash = createHash(email); this.clientId = options.authTitle || "00000000441cc96b"; this.on("msaAuthCodePrompt", (msa) => { if (codeCallback) codeCallback(msa); console.log(msa.message, "\nPlease login with this email:", this.email); }); this.refresh(); } get userXuid() { return __classPrivateFieldGet(this, _LinkCodeTokenProvider_token, "f").userXUID; } get userHash() { return __classPrivateFieldGet(this, _LinkCodeTokenProvider_token, "f").userHash; } get xstsToken() { return __classPrivateFieldGet(this, _LinkCodeTokenProvider_token, "f").XSTSToken; } get expiresOn() { return __classPrivateFieldGet(this, _LinkCodeTokenProvider_token, "f").expiresOn; } get isRefreshing() { return __classPrivateFieldGet(this, _LinkCodeTokenProvider_isTokenRefreshing, "f"); } reset() { fs.unlinkSync(`${this.cachePath}/${this.emailHash}_xbl-cache.json`); } async refresh() { __classPrivateFieldSet(this, _LinkCodeTokenProvider_isTokenRefreshing, true, "f"); if (!fs.existsSync(this.cachePath)) fs.mkdirSync(this.cachePath, { recursive: true }); let liveCache; try { liveCache = JSON.parse(fs.readFileSync(`${this.cachePath}/${this.emailHash}_live-cache.json`, "utf8")); } catch { return this.get(); } if (!liveCache.token) { if (this.liveCache) { liveCache = this.liveCache; } else return this.get(); } const res = await fetch(LinkCodeTokenProvider.uri, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ scope: liveCache.token.scope, client_id: this.clientId, grant_type: "refresh_token", refresh_token: liveCache.token.refresh_token, }), }); let json = await res.json(); this.liveCache = { token: { ...json, obtainedOn: Date.now() }, }; //console.log(json) //console.log(this.liveCache); fs.writeFileSync(`${this.cachePath}/${this.emailHash}_live-cache.json`, JSON.stringify(this.liveCache), "utf8"); if (fs.existsSync(`${this.cachePath}/${this.emailHash}_xbl-cache.json`)) this.reset(); return this.get(); } setRefreshInterval() { let expiry = this.liveCache.token.expires_in * 1000; expiry += this.liveCache.token.obtainedOn; setInterval(() => { if (expiry - Date.now() - 3600000 < 0) this.refresh(); }, 900000); } async get() { //debug(this.email, this.authPath, this.authOptions); let token = await new Authflow(this.email, this.cachePath, this.authOptions, (res) => { //debug(`getToken msa code`); this.emit("msaAuthCodePrompt", res); }).getXboxToken(); this.liveCache = JSON.parse(fs.readFileSync(`${this.cachePath}/${this.emailHash}_live-cache.json`, "utf8")); __classPrivateFieldSet(this, _LinkCodeTokenProvider_token, token, "f"); __classPrivateFieldSet(this, _LinkCodeTokenProvider_isTokenRefreshing, false, "f"); if (!this.firstTokenAcquired) { this.setRefreshInterval(); this.emit("firstTokenAcquired"); } this.firstTokenAcquired = true; return token; } } _LinkCodeTokenProvider_isTokenRefreshing = new WeakMap(), _LinkCodeTokenProvider_token = new WeakMap(); LinkCodeTokenProvider.uri = "https://login.live.com/oauth20_token.srf"; /* readonly URIs = { gamerPic: "https://gamerpics.xboxlive.com", leaderBoard: "https://leaderboards.xboxlive.com", list: "https://eplists.xboxlive.com", entertainmentDiscovery: "https://eds.xboxlive.com", marketplace: "https://marketplace.xboxlive.com", matchmaking: "https://momatch.xboxlive.com", multiplayerActivity: "https://multiplayeractivity.xboxlive.com", presence: "https://userpresence.xboxlive.com", privacy: "https://privacy.xboxlive.com", profile: "https://profile.xboxlive.com", titleStorage: "https://titlestorage.xboxlive.com", clientStrings: "https://client-strings.xboxlive.com", message: "https://msg.xboxlive.com", userStats: "https://userstats.xboxlive.com", }; */ export class XboxClient extends EventEmitter { constructor(tokenProvider) { super(); this.token = tokenProvider; this.social = new Social(this); this.sessionDirectory = new SessionDirectory(this); this.achievements = new Achievements(this); this.peopleHub = new PeopleHub(this); } get xuid() { return this.token.userXuid; } get authorizationHeader() { return `XBL3.0 x=${this.token.userHash};${this.token.xstsToken}`; } }