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.

36 lines
978 B

11 months ago
  1. const {
  2. BrowserExtensionApiKey,
  3. } = require("../../models/browserExtensionApiKey");
  4. const { SystemSettings } = require("../../models/systemSettings");
  5. const { User } = require("../../models/user");
  6. async function validBrowserExtensionApiKey(request, response, next) {
  7. const multiUserMode = await SystemSettings.isMultiUserMode();
  8. response.locals.multiUserMode = multiUserMode;
  9. const auth = request.header("Authorization");
  10. const bearerKey = auth ? auth.split(" ")[1] : null;
  11. if (!bearerKey) {
  12. response.status(403).json({
  13. error: "No valid API key found.",
  14. });
  15. return;
  16. }
  17. const apiKey = await BrowserExtensionApiKey.validate(bearerKey);
  18. if (!apiKey) {
  19. response.status(403).json({
  20. error: "No valid API key found.",
  21. });
  22. return;
  23. }
  24. if (multiUserMode) {
  25. response.locals.user = await User.get({ id: apiKey.user_id });
  26. }
  27. response.locals.apiKey = apiKey;
  28. next();
  29. }
  30. module.exports = { validBrowserExtensionApiKey };