You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
const proxy = require("http-proxy-middleware")
|
|
const express = require("express")
|
|
const jwt = require("jsonwebtoken")
|
|
|
|
const config = require("../config")
|
|
|
|
const router = express.Router()
|
|
|
|
router.post("/", (req, res) => {
|
|
const user = res.locals.user
|
|
const permissions = (req.header("X-Permissions") || "").split(",").filter(t => t != "" && t != "undefined" && t != "null")
|
|
let authorization = req.header("Authorization")
|
|
if (authorization == "null") {
|
|
authorization = ""
|
|
}
|
|
|
|
if (!authorization && user.loggedIn) {
|
|
authorization = `Bearer ${generateToken(user.name, permissions)}`
|
|
}
|
|
|
|
fetch(config.graphqlEndpoint, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": req.header("Content-Type"),
|
|
"Authorization": authorization,
|
|
},
|
|
body: req,
|
|
credentials: "include",
|
|
}).then(fetchRes => {
|
|
res.setHeader("Content-Type", fetchRes.headers.get("Content-Type"))
|
|
res.status(fetchRes.status)
|
|
|
|
return fetchRes.json()
|
|
}).then(json => {
|
|
res.json(json)
|
|
}).catch(err => {
|
|
res.status(500).text(err)
|
|
return null
|
|
})
|
|
})
|
|
|
|
router.use("/", proxy(config.graphqlEndpoint, {ws: true}))
|
|
|
|
/**
|
|
* @param {string} user
|
|
* @param {string[]} permissions
|
|
*/
|
|
function generateToken(user) {
|
|
return jwt.sign({user, exp: Math.floor((Date.now() / 1000) + 1200)}, config.backend.secret, {header: {kid: config.backend.kid}})
|
|
}
|
|
|
|
module.exports = {router, generateToken}
|