A simple file sharing site with an easy to use API and online panel.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

36 рядки
1.7KB

  1. const ModelPath = '../../models/';
  2. const Key = require(ModelPath + 'Key.js');
  3. const User = require(ModelPath + 'User.js');
  4. // Middleware that checks for authentication by either API key or session
  5. // sets req.username, req.displayname, req.scope, and req.key if authenticated properly, otherwise throws an error.
  6. // If the user is banned, also throw an error.
  7. const authenticate = async (req, scope) => {
  8. const keyprop = req.body.key || req.query.key;
  9. let key = keyprop ? (await Key.findOne({key: keyprop})) : false;
  10. if (key) {
  11. if (!scope || key.scope.includes(scope)) {
  12. if ((await User.countDocuments({username: key.issuer, banned: true})) === 0) {
  13. req.username = key.issuer;
  14. req.displayname = key.issuer;
  15. req.scope = key.scope;
  16. req.key = key.key;
  17. return {authenticated: true, permission: true};
  18. } else return {authenticated: true, permission: false};
  19. } else return {authenticated: true, permission: false};
  20. } else if (req.isAuthenticated()) {
  21. if (!scope || req.session.passport.scope.includes(scope)) {
  22. if ((await User.countDocuments({username: req.session.passport.user, banned: true})) === 0) {
  23. req.username = req.session.passport.user;
  24. req.displayname = req.session.passport.displayname;
  25. req.scope = req.session.passport.scope;
  26. req.key = null;
  27. return {authenticated: true, permission: true};
  28. } else return {authenticated: true, permission: false};
  29. } else return {authenticated: true, permission: false};
  30. } else return {authenticated: false, permission: false};
  31. };
  32. module.exports = authenticate;