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.

29 lines
706 B

11 months ago
  1. const { ApiKey } = require("../../models/apiKeys");
  2. const { SystemSettings } = require("../../models/systemSettings");
  3. async function validApiKey(request, response, next) {
  4. const multiUserMode = await SystemSettings.isMultiUserMode();
  5. response.locals.multiUserMode = multiUserMode;
  6. const auth = request.header("Authorization");
  7. const bearerKey = auth ? auth.split(" ")[1] : null;
  8. if (!bearerKey) {
  9. response.status(403).json({
  10. error: "No valid api key found.",
  11. });
  12. return;
  13. }
  14. if (!(await ApiKey.get({ secret: bearerKey }))) {
  15. response.status(403).json({
  16. error: "No valid api key found.",
  17. });
  18. return;
  19. }
  20. next();
  21. }
  22. module.exports = {
  23. validApiKey,
  24. };