auth.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. "use strict"
  20. const APIError = require('../api/APIError');
  21. const {jwt_auth} = require('../helpers');
  22. const { UserActorType } = require('../services/auth/Actor');
  23. const { DB_WRITE } = require('../services/database/consts');
  24. const { Context } = require('../util/context');
  25. const auth2 = require('./auth2');
  26. const auth = async (req, res, next)=>{
  27. let auth2_ok = false;
  28. try{
  29. // Delegate to new middleware
  30. await auth2(req, res, () => { auth2_ok = true; });
  31. if ( ! auth2_ok ) return;
  32. // Everything using the old reference to the auth middleware
  33. // should only allow session tokens
  34. if ( ! (req.actor.type instanceof UserActorType) ) {
  35. throw APIError.create('forbidden');
  36. }
  37. next();
  38. }
  39. // auth failed
  40. catch(e){
  41. return res.status(401).send(e);
  42. }
  43. }
  44. module.exports = auth