mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
feat: extend is-authenticated.js logic with api key checking
This commit is contained in:
parent
ea8c42411f
commit
d6cbb889fb
1 changed files with 20 additions and 1 deletions
|
@ -3,10 +3,29 @@
|
||||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
|
const API_KEY_HEADER = 'x-api-key';
|
||||||
|
|
||||||
module.exports = async function isAuthenticated(req, res, proceed) {
|
module.exports = async function isAuthenticated(req, res, proceed) {
|
||||||
if (!req.currentUser) {
|
if (req.currentUser) return proceed();
|
||||||
|
|
||||||
|
const apiKeyHeader = req.headers[API_KEY_HEADER.toLowerCase()];
|
||||||
|
if (!apiKeyHeader) {
|
||||||
return res.unauthorized('Access token is missing, invalid or expired');
|
return res.unauthorized('Access token is missing, invalid or expired');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!apiKeyHeader.includes('.')) return res.unauthorized('Invalid API key');
|
||||||
|
|
||||||
|
const [prefix] = apiKeyHeader.split('.');
|
||||||
|
if (!prefix) return res.unauthorized('Invalid API key');
|
||||||
|
|
||||||
|
const user = await User.findOne({ apiKeyPrefix: prefix, apiKeyHash: { '!=': null } });
|
||||||
|
if (!user) return res.unauthorized('Invalid API key');
|
||||||
|
|
||||||
|
const isMatch = await bcrypt.compare(apiKeyHeader, user.apiKeyHash);
|
||||||
|
if (!isMatch) return res.unauthorized('Invalid API key');
|
||||||
|
|
||||||
|
req.currentUser = user;
|
||||||
return proceed();
|
return proceed();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue