Skip to content

Commit

Permalink
Merge pull request #214 from NetsBlox/reenable-alexa-routes
Browse files Browse the repository at this point in the history
Update alexa routes to use new cloud
  • Loading branch information
brollb authored Feb 6, 2024
2 parents 19d0ef3 + 433d7e0 commit a007fdc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
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

0 comments on commit a007fdc

Please sign in to comment.