|
| 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 express = require('express') |
| 21 | +const router = new express.Router() |
| 22 | +const config = require('../config') |
| 23 | +const { invalidate_cached_user_by_id, get_user } = require('../helpers') |
| 24 | +const { DB_WRITE } = require('../services/database/consts') |
| 25 | + |
| 26 | +const jwt = require('jsonwebtoken'); |
| 27 | + |
| 28 | +// Ensure we don't expose branches with differing messages. |
| 29 | +const SAFE_NEGATIVE_RESPONSE = 'This password recovery token is no longer valid.'; |
| 30 | + |
| 31 | +// -----------------------------------------------------------------------// |
| 32 | +// POST /verify-pass-recovery-token |
| 33 | +// -----------------------------------------------------------------------// |
| 34 | +router.post('/verify-pass-recovery-token', express.json(), async (req, res, next)=>{ |
| 35 | + // check subdomain |
| 36 | + if(require('../helpers').subdomain(req) !== 'api' && require('../helpers').subdomain(req) !== '') |
| 37 | + next(); |
| 38 | + |
| 39 | + if ( ! req.body.token ) { |
| 40 | + return res.status(401).send('token is required') |
| 41 | + } |
| 42 | + |
| 43 | + const svc_edgeRateLimit = req.services.get('edge-rate-limit'); |
| 44 | + if ( ! svc_edgeRateLimit.check('verify-pass-recovery-token') ) { |
| 45 | + return res.status(429).send('Too many requests.'); |
| 46 | + } |
| 47 | + |
| 48 | + const { exp, user_uid, email } = jwt.verify(req.body.token, config.jwt_secret); |
| 49 | + |
| 50 | + const user = await get_user({ uuid: user_uid, force: true }); |
| 51 | + if ( user.email !== email ) { |
| 52 | + return res.status(400).send(SAFE_NEGATIVE_RESPONSE); |
| 53 | + } |
| 54 | + |
| 55 | + const current_time = Math.floor(Date.now() / 1000); |
| 56 | + const time_remaining = exp - current_time; |
| 57 | + |
| 58 | + return res.status(200).send({ time_remaining }); |
| 59 | +}) |
| 60 | + |
| 61 | +module.exports = router |
0 commit comments