The frontend/UI server, written in JS using the MarkoJS library
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.

51 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const proxy = require("http-proxy-middleware")
  2. const express = require("express")
  3. const jwt = require("jsonwebtoken")
  4. const config = require("../config")
  5. const router = express.Router()
  6. router.post("/", (req, res) => {
  7. const user = res.locals.user
  8. const permissions = (req.header("X-Permissions") || "").split(",").filter(t => t != "" && t != "undefined" && t != "null")
  9. let authorization = req.header("Authorization")
  10. if (authorization == "null") {
  11. authorization = ""
  12. }
  13. if (!authorization && user.loggedIn) {
  14. authorization = `Bearer ${generateToken(user.name, permissions)}`
  15. }
  16. fetch(config.graphqlEndpoint, {
  17. method: "POST",
  18. headers: {
  19. "Content-Type": req.header("Content-Type"),
  20. "Authorization": authorization,
  21. },
  22. body: req,
  23. credentials: "include",
  24. }).then(fetchRes => {
  25. res.setHeader("Content-Type", fetchRes.headers.get("Content-Type"))
  26. res.status(fetchRes.status)
  27. return fetchRes.json()
  28. }).then(json => {
  29. res.json(json)
  30. }).catch(err => {
  31. res.status(500).text(err)
  32. return null
  33. })
  34. })
  35. router.use("/", proxy(config.graphqlEndpoint, {ws: true}))
  36. /**
  37. * @param {string} user
  38. * @param {string[]} permissions
  39. */
  40. function generateToken(user) {
  41. return jwt.sign({user, exp: Math.floor((Date.now() / 1000) + 1200)}, config.backend.secret, {header: {kid: config.backend.kid}})
  42. }
  43. module.exports = {router, generateToken}