[FEATURE] Initial Cairo Auth Integration
This commit is contained in:
parent
edbfc2348a
commit
184f1fa631
10 changed files with 234 additions and 14 deletions
32
lib/routes/middlewares/auth-middleware.js
Normal file
32
lib/routes/middlewares/auth-middleware.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Imports
|
||||
import { Router } from "express";
|
||||
import bearerTokenMiddleware from "express-bearer-token";
|
||||
import { ERR, VERB } from "../../util/logging.js";
|
||||
|
||||
// Constants
|
||||
const { MCL_CAIRO_URL } = process.env;
|
||||
const cairoAuthMiddleware = Router();
|
||||
|
||||
const cairoAuthenticate = async (token) => {
|
||||
const config = { headers: { Authorization: `Bearer ${token}` } };
|
||||
return fetch(`${MCL_CAIRO_URL}/api/user/info`, config).then((res) =>
|
||||
res.json(),
|
||||
);
|
||||
};
|
||||
|
||||
// Middleware
|
||||
const cairoAuthHandler = (req, res, next) => {
|
||||
if (!req.token) return res.status(401).send("Cairo auth required!");
|
||||
VERB("AUTH", `${MCL_CAIRO_URL}/api/user/info`);
|
||||
cairoAuthenticate(req.token)
|
||||
.then(() => next())
|
||||
.catch((err) => {
|
||||
ERR("AUTH", err.response ? err.response.data : err.message);
|
||||
if (!err.response) return res.status(500).send(`Auth failure ${err}`);
|
||||
return res.status(err.response.status).send(err.response.data);
|
||||
});
|
||||
};
|
||||
|
||||
cairoAuthMiddleware.use([bearerTokenMiddleware(), cairoAuthHandler]);
|
||||
|
||||
export default cairoAuthMiddleware;
|
Loading…
Add table
Add a link
Reference in a new issue