Spaces:
Building
Building
File size: 11,460 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
const formatMessage = require('format-message');
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');
const Legacy = require('./legacy');
const Icon = require("./icon.svg");
/**
* Class for Scratch Authentication blocks
* @constructor
*/
let currentPrivateCode = '';
class JgScratchAuthenticateBlocks {
constructor(runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
this.promptStatus = {
inProgress: false,
blocked: false,
completed: false,
userClosed: false,
};
this.loginInfo = {};
// legacy
this.keepAllowingAuthBlock = true;
this.disableConfirmationShown = false;
}
/**
* dummy function for reseting user provided permisions when a save is loaded
*/
deserialize() {
this.disableConfirmationShown = false;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
return {
id: 'jgScratchAuthenticate',
name: 'Scratch Auth',
color1: '#FFA01C',
color2: '#ff8C00',
blockIconURI: Icon,
// TODO: docs doesnt exist, make some docs
// docsURI: 'https://docs.penguinmod.com/extensions/scratch-auth',
blocks: [
// LEGACY BLOCK
{
opcode: 'authenticate',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.authenticate',
default: 'get scratch username and set sign in location name to [NAME]',
description: "Block that returns the user's name on Scratch."
}),
disableMonitor: true,
hideFromPalette: true,
arguments: {
NAME: { type: ArgumentType.STRING, defaultValue: "PenguinMod" }
},
blockType: BlockType.REPORTER
},
// NEW BLOCKS
{
opcode: 'showPrompt',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.showPrompt',
default: 'show login message as [NAME]',
description: "Block that shows the Log in menu from Scratch Authentication."
}),
arguments: {
NAME: {
type: ArgumentType.STRING,
menu: 'loginLocation'
}
},
blockType: BlockType.COMMAND
},
{
opcode: 'getPromptStatus',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.promptStatus',
default: 'login prompt [STATUS]?',
description: "The status of the login prompt for Scratch Authentication."
}),
arguments: {
STATUS: {
type: ArgumentType.STRING,
menu: "promptStatus"
}
},
disableMonitor: true,
blockType: BlockType.BOOLEAN
},
{
opcode: 'privateCode',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.privateCode',
default: 'authentication code',
description: "The login code when Scratch Authentication closes the login prompt."
}),
disableMonitor: true,
blockType: BlockType.REPORTER
},
{
opcode: 'serverRedirectLocation',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.serverRedirectLocation',
default: 'redirect location',
description: "The redirect location when Scratch Authentication closes the login prompt."
}),
disableMonitor: true,
blockType: BlockType.REPORTER
},
'---',
{
text: formatMessage({
id: 'jgScratchAuthenticate.labels.loginInfo1',
default: 'The blocks below invalidate',
description: "Label to denote that blocks invalidate the Scratch Auth private code below this label"
}),
blockType: BlockType.LABEL
},
{
text: formatMessage({
id: 'jgScratchAuthenticate.labels.loginInfo2',
default: 'the authentication code from above.',
description: "Label to denote that blocks invalidate the Scratch Auth private code below this label"
}),
blockType: BlockType.LABEL
},
{
opcode: 'validLogin',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.validLogin',
default: 'login is valid?',
description: "Whether or not the authentication was valid."
}),
disableMonitor: true,
// this doesnt seem to be important,
// login should always be valid when checking on client-side
hideFromPalette: true,
blockType: BlockType.BOOLEAN
},
{
opcode: 'scratchUsername',
text: formatMessage({
id: 'jgScratchAuthenticate.blocks.scratchUsername',
default: 'scratch username',
description: "The username that was logged in."
}),
disableMonitor: true,
blockType: BlockType.REPORTER
},
],
menus: {
loginLocation: {
items: '_getLoginLocations',
isTypeable: true,
},
promptStatus: [
{ text: 'in progress', value: 'inProgress' },
{ text: 'blocked', value: 'blocked' },
{ text: 'complete', value: 'completed' },
{ text: 'closed by the user', value: 'userClosed' },
]
}
};
}
// menus
_getLoginLocations() {
const nameSplit = document.title.split(" - ");
nameSplit.pop();
const projectName = Cast.toString(nameSplit.join(" - "));
return [
projectName === 'PenguinMod' ? 'Project' : projectName,
'PenguinMod',
'Game',
];
}
// util
async parseLoginCode_() {
if (!currentPrivateCode) throw new Error('Private code not present');
const req = await fetch(`https://pm-bapi.vercel.app/api/verifyToken?privateCode=${currentPrivateCode}`);
const json = await req.json();
this.loginInfo = {
valid: json.valid,
username: json.username
};
return this.loginInfo;
}
// blocks
showPrompt(args) {
// reset
this.promptStatus = {
inProgress: true,
blocked: false,
completed: false,
userClosed: false,
};
this.loginInfo = {};
const loginLocation = Cast.toString(args.NAME);
const sanitizedName = encodeURIComponent(loginLocation.substring(0, 256).replace(/[^a-zA-Z0-9 _\-\.\[\]\(\)]+/gmi, ""));
const waitingLink = `https://studio.penguinmod.com/scratchAuthExt.html?openLocation=${encodeURIComponent(window.origin)}`;
// listen for events before opening
let login;
let finished = false;
const listener = (event) => {
if (event.origin !== (new URL(waitingLink)).origin) {
return;
}
if (!(event.data && event.data.scratchauthd1)) {
return;
}
const data = event.data.scratchauthd1;
const privateCode = data.pv;
currentPrivateCode = privateCode;
// update status
this.promptStatus.inProgress = false;
this.promptStatus.completed = true;
finished = true;
window.removeEventListener("message", listener);
login.close();
};
window.addEventListener("message", listener);
// open prompt
login = window.open(
`https://auth.itinerary.eu.org/auth/?redirect=${btoa(waitingLink)}${sanitizedName.length > 0 ? `&name=${sanitizedName}` : ""}`,
"Scratch Authentication",
`scrollbars=yes,resizable=yes,status=no,location=yes,toolbar=no,menubar=no,width=768,height=512,left=200,top=200`
);
if (!login) {
// popup was blocked most likely
this.promptStatus.inProgress = false;
this.promptStatus.blocked = true;
return;
}
// .onclose doesnt work on most platforms it seems
// so just set interval
const closedInterval = setInterval(() => {
if (!login.closed) return;
this.promptStatus.inProgress = false;
if (!finished) {
this.promptStatus.userClosed = true;
}
window.removeEventListener("message", listener);
clearInterval(closedInterval);
}, 500);
}
privateCode() {
const code = currentPrivateCode;
currentPrivateCode = '';
return code;
}
serverRedirectLocation() {
const waitingLink = `https://studio.penguinmod.com/scratchAuthExt.html?openLocation=${window.origin}`;
return waitingLink;
}
getPromptStatus(args) {
const option = Cast.toString(args.STATUS);
if (!(option in this.promptStatus)) return false;
return this.promptStatus[option];
}
// parsing privat4e code blocks
async validLogin() {
if (Object.keys(this.loginInfo).length <= 0) {
try {
await this.parseLoginCode_();
} catch {
// just say invalid if we cant parse
return false;
}
}
return !!this.loginInfo.valid;
}
async scratchUsername() {
if (Object.keys(this.loginInfo).length <= 0) {
try {
await this.parseLoginCode_();
} catch {
// just say no username if we cant parse
return '';
}
}
return Cast.toString(this.loginInfo.username);
}
// legacy block
authenticate(...args) {
return Legacy.authenticate(this, ...args);
}
}
module.exports = JgScratchAuthenticateBlocks;
|