UNPKG

dmclc

Version:

Dolphin Minecraft Launcher Core

98 lines (97 loc) 3.27 kB
import got from "got"; export class YggdrasilAccount { data; launcher; root; constructor(data, launcher) { this.data = data; this.launcher = launcher; this.root = launcher.rootPath; } getTokens() { const ret = []; if (this.data.accessToken) ret.push(this.data.accessToken); if (this.data.clientToken) ret.push(this.data.clientToken); return ret; } async getLaunchGameArgs() { const map = new Map(); const at = await this.getAccessToken(); map.set("auth_access_token", at); map.set("auth_session", at); map.set("auth_player_name", this.data.name); map.set("user_type", "mojang"); map.set("user_properties", "{}"); return map; } async login() { const content = await this.launcher.askUser({ username: this.launcher.i18n("accounts.yggdrasil.username"), password: this.launcher.i18n("accounts.yggdrasil.password"), profileID: this.launcher.i18n("accounts.yggdrasil.profileID") }); const profileID = Number.parseInt(content.profileID); const res = await got.post(this.data.apiurl + "/authserver/authenticate", { json: { username: content.username, password: content.password, requestUser: true, agent: { name: "Minecraft", version: 1 } }, throwHttpErrors: false, responseType: "json" }); if (res.statusCode === 403) { await this.launcher.error("accounts.yggdrasil.wrong_email_or_password"); return false; } const obj = res.body; this.data.accessToken = obj.accessToken; this.data.clientToken = obj.clientToken; this.data.uuid = obj.availableProfiles[profileID].id; this.data.name = obj.availableProfiles[profileID].name; const meta = await got(this.data.apiurl).json(); this.data.serverName = meta.meta.serverName; return true; } async check() { const resp = await got.post(this.data.apiurl + "/authserver/validate", { json: { accessToken: this.data.accessToken, clientToken: this.data.clientToken }, throwHttpErrors: false }); if (resp.statusCode === 204) return true; return await this.refresh(); } async refresh() { const req = await got.post(this.data.apiurl + "/authserver/refresh", { json: { accessToken: this.data.accessToken, clientToken: this.data.clientToken }, responseType: "json", followRedirect: false, throwHttpErrors: false }); if (req.statusCode >= 300 || req.statusCode < 200) return false; const res = req.body; this.data.accessToken = res.accessToken; this.data.clientToken = res.clientToken; return true; } getUUID() { return this.data.uuid; } async getAccessToken() { return this.data.accessToken; } }