remove mojang auth

This commit is contained in:
creeper123123321 2022-08-06 13:55:14 -03:00
parent 7fa6e6c888
commit b653256f07
3 changed files with 52 additions and 219 deletions

View File

@ -93,8 +93,10 @@ frame-src 'self' https://login.microsoftonline.com/ https://login.live.com/"
<p>WebSocket connection status: <span class="text-white bg-dark" id="connection_status">?</span></p>
<p>CORS Proxy status: <span class="text-white bg-dark" id="cors_status">?</span></p>
<hr>
<p>Listening to front-end logins from:</p>
<div id="listening"></div>
<div class="mb-3">
<p>Listening to front-end logins from:</p>
<ul class="list-group" id="listening"></ul>
</div>
<div id="actions">
<button type="button" class="btn btn-primary" id="listen_continue">Listen to <span
id="mcIdUsername"></span>
@ -156,7 +158,7 @@ frame-src 'self' https://login.microsoftonline.com/ https://login.live.com/"
</form>
</div>
<div class="mb-3">
<button id="en_notific" type="button" class="btn btn-primary">Enable browser notifications</button>
<button id="en_notifications" type="button" class="btn btn-primary">Enable browser notifications</button>
</div>
</div>
@ -174,22 +176,6 @@ frame-src 'self' https://login.microsoftonline.com/ https://login.live.com/"
</div>
</form>
</div>
<div class="mb-2">
<label class="form-label" for="form_add_mc">Add Mojang account</label>
<form class="input-group row justify-content-md-center" id="form_add_mc" action="javascript:">
<div class="col-md-auto">
<input aria-label="Email/Username" class="form-control" id="mc_email"
placeholder="Username" type="text">
</div>
<div class="col-md-auto">
<input aria-label="Password" class="form-control" id="mc_password"
placeholder="Password" type="password">
</div>
<div class="col-md-auto">
<button class="btn btn-primary" id="login_submit_mc" type="submit">Login with Mojang</button>
</div>
</form>
</div>
</div>
</div>
</div>

View File

@ -4,13 +4,13 @@ window.location.hash.substring(1).split("?")
.map(it => new URLSearchParams(it)
.forEach((a, b) => urlParams.append(b, a)));
let mcIdUsername = urlParams.get("username");
let mcauth_code = urlParams.get("mcauth_code");
let mcauth_success = urlParams.get("mcauth_success");
let mcIdCode = urlParams.get("mcauth_code");
let mcIdSuccess = urlParams.get("mcauth_success");
$(() => {
if (mcauth_success === "false") {
if (mcIdSuccess === "false") {
addToast("Couldn't authenticate with Minecraft.ID", urlParams.get("mcauth_msg"));
}
if (mcauth_code != null) {
if (mcIdCode != null) {
history.replaceState(null, null, "#");
}
});
@ -41,13 +41,12 @@ $(() => {
$("a[href='javascript:']").on("click", e => e.preventDefault());
cors_proxy_txt.value = getCorsProxy();
ws_url_txt.value = getWsUrl();
$("#form_add_mc").on("submit", () => loginMc($("#mc_email").val(), $("#mc_password").val()));
$("#form_add_ms").on("submit", () => loginMs());
$("#form_ws_url").on("submit", () => setWsUrl($("#ws-url").val()));
$("#form_cors_proxy").on("submit", () => setCorsProxy($("#cors-proxy").val()));
$("#form_listen").on("submit", () => submittedListen());
$("#form_send_token").on("submit", () => submittedSendToken());
$("#en_notific").on("click", () => Notification.requestPermission().then(renderActions));
$("#en_notifications").on("click", () => Notification.requestPermission().then(renderActions));
$("#listen_continue").on("click", () => clickedListenContinue());
window.addEventListener('beforeinstallprompt', e => e.preventDefault());
ohNo();
@ -81,8 +80,6 @@ function addMcAccountToList(account) {
<button type="button" class='btn btn-danger mc-remove'>Logout</button>
</li>`);
let txt = account.name;
if (account instanceof MicrosoftAccount)
txt += " (" + account.msUser + ")";
line.find(".mc-user").text(txt);
line.find(".mc-remove").on("click", () => account.logout());
let head = line.find(".mc-head");
@ -140,21 +137,21 @@ function clickedListenContinue() {
sendSocket(JSON.stringify({
"action": "minecraft_id_login",
"username": mcIdUsername,
"code": mcauth_code
"code": mcIdCode
}));
mcauth_code = null;
mcIdCode = null;
renderActions();
}
function renderActions() {
$("#en_notific").hide();
$("#en_notifications").hide();
$("#listen_continue").hide();
$("#listen_open").hide();
$("#send_token_open").hide();
if (Notification.permission === "default") {
$("#en_notific").show();
$("#en_notifications").show();
}
if (listenVisible) {
if (mcIdUsername != null && mcauth_code != null) {
if (mcIdUsername != null && mcIdCode != null) {
$("#listen_continue").show();
}
$("#listen_open").show();
@ -171,15 +168,20 @@ function onCompletedPoW(e) {
sendSocket(e.data.msg);
}
function addListeningList(userId, username, token) {
let line = $("<p><img alt='?' src='?' loading='lazy' width=24 class='head'/> <span class='username'></span> <button class='btn btn-danger' type='button'>Unlisten</button></p>");
line.find(".username").text(username || userId);
let line = $(`<li class='list-group-item d-flex justify-content-between align-items-center p-1'>
<span class='ms-2'><img alt='?' src='?' loading='lazy' width=24 class='mc-head'/></span>
<span class='username'></span>
<button class='btn btn-danger' type='button'>Unlisten</button>
</li>`);
let displayName = username || userId;
line.find(".username").text(displayName);
line.find(".btn").on("click", () => {
removeToken(token);
line.remove();
unlisten(userId);
});
let head = line.find(".head");
head.attr("alt", userId + "'s head");
let head = line.find(".mc-head");
head.attr("alt", displayName + "'s head");
head.attr("src", "https://crafthead.net/helm/" + userId);
$(listening).append(line);
}
@ -224,7 +226,7 @@ function resetHtml() {
}
function ohNo() {
try {
icanhazepoch().then(sec => {
getNetworkTimestamp().then(sec => {
const calcDelta = Date.now() - sec * 1000;
if (Math.abs(calcDelta) > 10000) {
addToast("Time isn't synchronized", "Please synchronize your computer time to NTP servers");
@ -261,7 +263,7 @@ async function getIpAddress(cors) {
.then(r => r.text())
.then(it => it.trim());
}
function icanhazepoch() {
function getNetworkTimestamp() {
return fetch("https://icanhazepoch.com")
.then(checkFetchSuccess("code"))
.then(r => r.text())
@ -295,7 +297,7 @@ function authNotification(msg, yes, no) {
{ action: "reject", title: "Reject" },
{ action: "confirm", title: "Confirm" }
]
});
}).then(() => { });
notificationCallbacks.set(tag, action => {
if (action === "reject") {
no();
@ -323,7 +325,6 @@ let activeAccounts = [];
function loadAccounts() {
(JSON.parse(localStorage.getItem("viaaas_mc_accounts")) || []).forEach((it) => {
if (it.clientToken) {
addActiveAccount(new MojangAccount(it.id, it.name, it.accessToken, it.clientToken));
}
else if (it.msUser && myMSALObj.getAccountByUsername(it.msUser)) {
addActiveAccount(new MicrosoftAccount(it.id, it.name, it.accessToken, it.msUser));
@ -351,14 +352,7 @@ class McAccount {
this.loggedOut = true;
}
async checkActive() {
return fetch(getCorsProxy() + "https://authserver.mojang.com/validate", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: this.clientToken || undefined
}),
headers: { "content-type": "application/json" }
}).then(data => data.ok);
return true;
}
async joinGame(hash) {
await this.acquireActiveToken()
@ -387,54 +381,6 @@ class McAccount {
.catch(e => addToast("Failed to refresh token!", e));
}
}
class MojangAccount extends McAccount {
constructor(id, username, accessToken, clientToken) {
super(id, username, accessToken);
this.clientToken = clientToken;
}
async logout() {
await super.logout();
await fetch(getCorsProxy() + "https://authserver.mojang.com/invalidate", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: this.clientToken
}),
headers: { "content-type": "application/json" }
}).then(checkFetchSuccess("not success logout"));
}
async refresh() {
console.log("refreshing " + this.id);
let jsonResp = await fetch(getCorsProxy() + "https://authserver.mojang.com/refresh", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: this.clientToken
}),
headers: { "content-type": "application/json" },
})
.then(async (r) => {
if (r.status === 403) {
try {
await this.logout();
}
catch (e) {
console.error(e);
}
throw "403, token expired?";
}
return r;
})
.then(checkFetchSuccess("code"))
.then(r => r.json());
console.log("refreshed " + jsonResp.selectedProfile.id);
this.accessToken = jsonResp.accessToken;
this.clientToken = jsonResp.clientToken;
this.name = jsonResp.selectedProfile.name;
this.id = jsonResp.selectedProfile.id;
saveRefreshAccounts();
}
}
class MicrosoftAccount extends McAccount {
constructor(id, username, accessToken, msUser) {
super(id, username, accessToken);
@ -528,26 +474,6 @@ function addActiveAccount(acc) {
activeAccounts.push(acc);
saveRefreshAccounts();
}
function loginMc(user, pass) {
const clientToken = uuid.v4();
fetch(getCorsProxy() + "https://authserver.mojang.com/authenticate", {
method: "post",
body: JSON.stringify({
agent: { name: "Minecraft", version: 1 },
username: user,
password: pass,
clientToken: clientToken,
}),
headers: { "content-type": "application/json" }
}).then(checkFetchSuccess("code"))
.then(r => r.json())
.then(data => {
let acc = new MojangAccount(data.selectedProfile.id, data.selectedProfile.name, data.accessToken, data.clientToken);
addActiveAccount(acc);
return acc;
}).catch(e => addToast("Failed to login", e));
$("#form_add_mc input").val("");
}
function getLoginRequest() {
return { scopes: ["XboxLive.signin"] };
}

View File

@ -7,14 +7,14 @@ window.location.hash.substring(1).split("?")
.map(it => new URLSearchParams(it)
.forEach((a, b) => urlParams.append(b, a)));
let mcIdUsername = urlParams.get("username");
let mcauth_code = urlParams.get("mcauth_code");
let mcauth_success = urlParams.get("mcauth_success");
let mcIdCode = urlParams.get("mcauth_code");
let mcIdSuccess = urlParams.get("mcauth_success");
$(() => {
if (mcauth_success === "false") {
if (mcIdSuccess === "false") {
addToast("Couldn't authenticate with Minecraft.ID", urlParams.get("mcauth_msg"));
}
if (mcauth_code != null) {
if (mcIdCode != null) {
history.replaceState(null, null, "#");
}
});
@ -51,13 +51,12 @@ $(() => {
cors_proxy_txt.value = getCorsProxy();
ws_url_txt.value = getWsUrl();
$("#form_add_mc").on("submit", () => loginMc($("#mc_email").val() as string, $("#mc_password").val() as string));
$("#form_add_ms").on("submit", () => loginMs());
$("#form_ws_url").on("submit", () => setWsUrl($("#ws-url").val() as string));
$("#form_cors_proxy").on("submit", () => setCorsProxy($("#cors-proxy").val() as string));
$("#form_listen").on("submit", () => submittedListen());
$("#form_send_token").on("submit", () => submittedSendToken());
$("#en_notific").on("click", () => Notification.requestPermission().then(renderActions));
$("#en_notifications").on("click", () => Notification.requestPermission().then(renderActions));
$("#listen_continue").on("click", () => clickedListenContinue());
window.addEventListener('beforeinstallprompt', e => e.preventDefault());
@ -99,7 +98,6 @@ function addMcAccountToList(account: McAccount) {
<button type="button" class='btn btn-danger mc-remove'>Logout</button>
</li>`);
let txt = account.name;
if (account instanceof MicrosoftAccount) txt += " (" + account.msUser + ")";
line.find(".mc-user").text(txt);
line.find(".mc-remove").on("click", () => account.logout());
let head = line.find(".mc-head");
@ -161,23 +159,23 @@ function clickedListenContinue() {
sendSocket(JSON.stringify({
"action": "minecraft_id_login",
"username": mcIdUsername,
"code": mcauth_code
"code": mcIdCode
}));
mcauth_code = null;
mcIdCode = null;
renderActions();
}
function renderActions() {
$("#en_notific").hide();
$("#en_notifications").hide();
$("#listen_continue").hide();
$("#listen_open").hide();
$("#send_token_open").hide();
if (Notification.permission === "default") {
$("#en_notific").show();
$("#en_notifications").show();
}
if (listenVisible) {
if (mcIdUsername != null && mcauth_code != null) {
if (mcIdUsername != null && mcIdCode != null) {
$("#listen_continue").show();
}
$("#listen_open").show();
@ -196,15 +194,20 @@ function onCompletedPoW(e: MessageEvent) {
}
function addListeningList(userId: string, username: string, token: string) {
let line = $("<p><img alt='?' src='?' loading='lazy' width=24 class='head'/> <span class='username'></span> <button class='btn btn-danger' type='button'>Unlisten</button></p>");
line.find(".username").text(username || userId);
let line = $(`<li class='list-group-item d-flex justify-content-between align-items-center p-1'>
<span class='ms-2'><img alt='?' src='?' loading='lazy' width=24 class='mc-head'/></span>
<span class='username'></span>
<button class='btn btn-danger' type='button'>Unlisten</button>
</li>`);
let displayName = username || userId;
line.find(".username").text(displayName);
line.find(".btn").on("click", () => {
removeToken(token);
line.remove();
unlisten(userId);
});
let head = line.find(".head");
head.attr("alt", userId + "'s head");
let head = line.find(".mc-head");
head.attr("alt", displayName + "'s head");
head.attr("src", "https://crafthead.net/helm/" + userId);
$(listening).append(line);
}
@ -256,7 +259,7 @@ function resetHtml() {
function ohNo() {
try {
icanhazepoch().then(sec => {
getNetworkTimestamp().then(sec => {
const calcDelta = Date.now() - sec * 1000;
if (Math.abs(calcDelta) > 10000) {
addToast("Time isn't synchronized", "Please synchronize your computer time to NTP servers");
@ -293,7 +296,7 @@ async function getIpAddress(cors: boolean): Promise<String> {
.then(it => it.trim());
}
function icanhazepoch() {
function getNetworkTimestamp() {
return fetch("https://icanhazepoch.com")
.then(checkFetchSuccess("code"))
.then(r => r.text())
@ -331,7 +334,7 @@ function authNotification(msg: string, yes: () => void, no: () => void) {
{action: "reject", title: "Reject"},
{action: "confirm", title: "Confirm"}
]
});
}).then(() => {});
notificationCallbacks.set(tag, action => {
if (action === "reject") {
no();
@ -365,7 +368,7 @@ let activeAccounts: Array<McAccount> = [];
function loadAccounts() {
(JSON.parse(localStorage.getItem("viaaas_mc_accounts")) || []).forEach((it: any) => {
if (it.clientToken) {
addActiveAccount(new MojangAccount(it.id, it.name, it.accessToken, it.clientToken))
// Mojang auth doesn't work on multiplayer anymore
} else if (it.msUser && myMSALObj.getAccountByUsername(it.msUser)) {
addActiveAccount(new MicrosoftAccount(it.id, it.name, it.accessToken, it.msUser))
}
@ -403,14 +406,7 @@ class McAccount {
}
async checkActive(): Promise<boolean> {
return fetch(getCorsProxy() + "https://authserver.mojang.com/validate", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: (this as any).clientToken || undefined
}),
headers: {"content-type": "application/json"}
}).then(data => data.ok);
return true;
}
async joinGame(hash: string): Promise<void> {
@ -443,59 +439,6 @@ class McAccount {
}
}
class MojangAccount extends McAccount {
public clientToken: string;
constructor(id: string, username: string, accessToken: string, clientToken: string) {
super(id, username, accessToken);
this.clientToken = clientToken;
}
override async logout() {
await super.logout();
await fetch(getCorsProxy() + "https://authserver.mojang.com/invalidate", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: this.clientToken
}),
headers: {"content-type": "application/json"}
}).then(checkFetchSuccess("not success logout"));
}
override async refresh() {
console.log("refreshing " + this.id);
let jsonResp = await fetch(getCorsProxy() + "https://authserver.mojang.com/refresh", {
method: "post",
body: JSON.stringify({
accessToken: this.accessToken,
clientToken: this.clientToken
}),
headers: {"content-type": "application/json"},
})
.then(async r => {
if (r.status === 403) {
try {
await this.logout();
} catch (e) {
console.error(e);
}
throw "403, token expired?";
}
return r;
})
.then(checkFetchSuccess("code"))
.then(r => r.json());
console.log("refreshed " + jsonResp.selectedProfile.id);
this.accessToken = jsonResp.accessToken;
this.clientToken = jsonResp.clientToken;
this.name = jsonResp.selectedProfile.name;
this.id = jsonResp.selectedProfile.id;
saveRefreshAccounts();
}
}
class MicrosoftAccount extends McAccount {
public msUser: string;
@ -598,28 +541,6 @@ function addActiveAccount(acc: McAccount) {
saveRefreshAccounts()
}
function loginMc(user: string, pass: string) {
// @ts-ignore
const clientToken = uuid.v4();
fetch(getCorsProxy() + "https://authserver.mojang.com/authenticate", {
method: "post",
body: JSON.stringify({
agent: {name: "Minecraft", version: 1},
username: user,
password: pass,
clientToken: clientToken,
}),
headers: {"content-type": "application/json"}
}).then(checkFetchSuccess("code"))
.then(r => r.json())
.then(data => {
let acc = new MojangAccount(data.selectedProfile.id, data.selectedProfile.name, data.accessToken, data.clientToken);
addActiveAccount(acc);
return acc;
}).catch(e => addToast("Failed to login", e));
$("#form_add_mc input").val("");
}
function getLoginRequest() {
return {scopes: ["XboxLive.signin"]};
}