Skip to content

Commit

Permalink
Merge branch 'main' into remove-example-project-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb committed Feb 7, 2024
2 parents 4052bef + a007fdc commit d85d8ac
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 19 deletions.
2 changes: 2 additions & 0 deletions docs/adv/url.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ The following settings are generally available, regardless of the action:
- **noExitWarning**: Don't confirm that the user wants to leave the page on tab close.
- **lang**: Set the language immediately. For example, "lang=hu" will set NetsBlox to Hungarian on start.
- **setVariable**: Set a variable to the given value on start. The value is expected to be a URL-encoded pair so setting "hello" to "world" would be "hello%3Dworld".
- **cloud**: Override the default cloud URL. This can be used to point the public deployment to your own local cloud (e.g, `cloud=http://localhost:7777`).
- **extensions**: A list of extensions (by URL) to load on start.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# -- Project information -----------------------------------------------------

project = 'NetsBlox'
copyright = '2021, Vanderbilt University'
copyright = '2024, Vanderbilt University'
author = 'NetsBlox Team'

# -- General configuration ---------------------------------------------------
Expand Down
112 changes: 112 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"cookie-parser": "^1.4.4",
"doctrine": "^2.0.0",
"express": "^4.18.1",
"fast-csv": "^4.3.6",
"fs-extra": "^11.1.0",
"geolib": "^2.0.18",
"gnuplot": "^0.3.1",
"jimp": "^0.16.1",
"json-query": "^2.2.2",
"md5": "^2.3.0",
"moment": "^2.29.4",
"mongodb": "^3.7.3",
"mongoose": "^5.10.15",
Expand Down
18 changes: 16 additions & 2 deletions src/cloud-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ class NetsBloxCloud {
this.secret = secret;
}

async whoami(cookie) {
// TODO: look up the username using the cookie
async whoami(cookieJar) {
const cookieStr = Object.entries(cookieJar)
.map(([name, value]) => `${name}=${value}`)
.join("; ");

const opts = {
headers: {
cookie: cookieStr,
},
};
console.log("whoami using cookies:", cookieStr);
return await this.get("/users/whoami", opts);
}

async getRoomState(projectId) {
Expand Down Expand Up @@ -118,6 +128,10 @@ class NetsBloxCloud {
return clients;
}

async getOAuthToken(tokenId) {
return await this.get(`/oauth/token/${tokenId}`);
}

isConfigured() {
return this.cloudUrl && this.id && this.secret;
}
Expand Down
12 changes: 10 additions & 2 deletions src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@ class MissingFieldError extends RequestError {
}

function handleUserErrors(fn) {
return async function (req, res) {
return async function (_req, res) {
try {
await fn.call(this, ...arguments);
} catch (err) {
if (err instanceof RequestError) {
res.status(err.status).send(err.message);
} else {
res.status(500).send("Internal Error Occurred. Try again later!");
console.warn(err.stack);
res.status(500).send("Internal error occurred. Try again later!");
}
}
};
}

class LoginRequired extends RequestError {
constructor() {
super(401, "Login Required.");
}
}

module.exports = {
UserError,
NotAllowedError,
InvalidKeyProviderError,
MissingFieldError,
LoginRequired,

handleUserErrors,
};
24 changes: 10 additions & 14 deletions src/procedures/alexa/routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const AlexaSkill = require("./skill");
const express = require("express");

module.exports = express(); // FIXME: add support for OAuth on NetsBlox cloud
return;
// TODO: refactor the next imports
const OAuth = require("../../../api/core/oauth");
// FIXME: TODO: Update this
const { handleErrors } = require("../../../api/rest/utils");
const NetsBloxCloud = require("../../cloud-client");
const { handleUserErrors } = require("../../error");
const { setUsernameFromCookie } = require("../utils/router-utils");
const { LoginRequired, RequestError } = require("../../../api/core/errors");
const { LoginRequired, RequestError } = require("../../error");

const bodyParser = require("body-parser");
const axios = require("axios");
Expand All @@ -27,14 +23,14 @@ const logger = require("../utils/logger")("alexa:routes");

const router = express();
const parseCookies = cookieParser();
router.get("/ping", (req, res) => res.send("pong"));
router.get("/ping", (_req, res) => res.send("pong"));
router.get(
"/login.html",
bodyParser.json(),
parseCookies,
setUsernameFromCookie,
handleErrors((req, res) => {
const username = req.session.username;
handleUserErrors((req, res) => {
const username = req.username;

const isLoggedIn = !!username;
if (!isLoggedIn) {
Expand Down Expand Up @@ -63,8 +59,8 @@ router.put(
bodyParser.json(),
parseCookies,
setUsernameFromCookie,
handleErrors(async (req, res) => {
const { username } = req.session;
handleUserErrors(async (req, res) => {
const { username } = req;
const isLoggedIn = !!username;

if (!isLoggedIn) {
Expand Down Expand Up @@ -126,7 +122,7 @@ router.post(
handleErrorsInAlexa(async (req, res) => {
const reqData = req.body;
const { accessToken } = reqData.session.user;
const token = await OAuth.getToken(accessToken);
const token = await NetsBloxCloud.getToken(accessToken);
const { username } = token;

const skillId = reqData.session.application.applicationId;
Expand Down Expand Up @@ -194,7 +190,7 @@ function speak(text) {
}

function handleErrorsInAlexa(fn) {
return async function (req, res) {
return async function (_req, res) {
try {
await fn(...arguments);
} catch (err) {
Expand Down
Loading

0 comments on commit d85d8ac

Please sign in to comment.