|
const authenticate = (thisObject, args) => { |
|
if (!thisObject.keepAllowingAuthBlock) { |
|
if (!thisObject.disableConfirmationShown) { |
|
const areYouSure = true; |
|
if (!areYouSure) { |
|
thisObject.disableConfirmationShown = true; |
|
return "The user has declined the ability to authenticate."; |
|
} |
|
} else { |
|
return "The user has declined the ability to authenticate."; |
|
} |
|
} |
|
return new Promise(resolve => { |
|
const sanitizedName = encodeURIComponent(String(args.NAME).substring(0, 256).replace(/[^a-zA-Z0-9 _-]+/gmi, "_")); |
|
const waitingLink = `${window.location.origin}/wait.html`; |
|
const login = window.open( |
|
`https://auth.itinerary.eu.org/auth/?redirect=${btoa(waitingLink)}&name=${sanitizedName.length > 0 ? sanitizedName : "PenguinMod"}`, |
|
"Scratch Authentication", |
|
`scrollbars=yes,resizable=yes,status=no,location=yes,toolbar=no,menubar=no,width=768,height=512,left=200,top=200` |
|
); |
|
if (!login) { |
|
resolve("Authentication failed to appear."); |
|
|
|
} |
|
let cantAccessAnymore = false; |
|
let finished = false; |
|
let interval = null; |
|
interval = setInterval(() => { |
|
if (login?.closed && (!finished)) { |
|
thisObject.keepAllowingAuthBlock = false; |
|
clearInterval(interval); |
|
try { |
|
login.close(); |
|
} catch { |
|
|
|
} |
|
resolve(""); |
|
} |
|
try { |
|
const query = login.location.search; |
|
if (!cantAccessAnymore) return; |
|
const parameters = new URLSearchParams(query); |
|
const privateCode = parameters.get("privateCode"); |
|
if (!privateCode) { |
|
finished = true; |
|
clearInterval(interval); |
|
login.close(); |
|
resolve(""); |
|
} |
|
clearInterval(interval); |
|
fetch(`https://pm-bapi.vercel.app/api/verifyToken?privateCode=${privateCode}`).then(res => res.json().then(json => { |
|
finished = true; |
|
login.close(); |
|
if (json.valid != true) { |
|
resolve(""); |
|
} |
|
resolve(String(json.username)); |
|
}) |
|
.catch(() => { |
|
finished = true; |
|
login.close(); |
|
resolve(""); |
|
})) |
|
.catch(() => { |
|
finished = true; |
|
login.close(); |
|
resolve(""); |
|
}); |
|
} catch { |
|
|
|
cantAccessAnymore = true; |
|
|
|
} |
|
}, 10); |
|
}); |
|
}; |
|
|
|
module.exports = { |
|
authenticate |
|
} |