mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-09-14 10:52:34 +00:00
Convert backend to ESM
- About 5 years overdue - Remove eslint, use bomejs instead
This commit is contained in:
@@ -3,16 +3,16 @@
|
||||
and then has abilities after that.
|
||||
*/
|
||||
|
||||
const _ = require('lodash');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const crypto = require('crypto');
|
||||
const config = require('../lib/config');
|
||||
const error = require('../lib/error');
|
||||
const logger = require('../logger').global;
|
||||
const ALGO = 'RS256';
|
||||
import crypto from "node:crypto";
|
||||
import jwt from "jsonwebtoken";
|
||||
import _ from "lodash";
|
||||
import { getPrivateKey, getPublicKey } from "../lib/config.js";
|
||||
import errs from "../lib/error.js";
|
||||
import { global as logger } from "../logger.js";
|
||||
|
||||
module.exports = function () {
|
||||
const ALGO = "RS256";
|
||||
|
||||
export default () => {
|
||||
let token_data = {};
|
||||
|
||||
const self = {
|
||||
@@ -21,28 +21,26 @@ module.exports = function () {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
create: (payload) => {
|
||||
if (!config.getPrivateKey()) {
|
||||
logger.error('Private key is empty!');
|
||||
if (!getPrivateKey()) {
|
||||
logger.error("Private key is empty!");
|
||||
}
|
||||
// sign with RSA SHA256
|
||||
const options = {
|
||||
algorithm: ALGO,
|
||||
expiresIn: payload.expiresIn || '1d'
|
||||
expiresIn: payload.expiresIn || "1d",
|
||||
};
|
||||
|
||||
payload.jti = crypto.randomBytes(12)
|
||||
.toString('base64')
|
||||
.substring(-8);
|
||||
payload.jti = crypto.randomBytes(12).toString("base64").substring(-8);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
jwt.sign(payload, config.getPrivateKey(), options, (err, token) => {
|
||||
jwt.sign(payload, getPrivateKey(), options, (err, token) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
token_data = payload;
|
||||
resolve({
|
||||
token: token,
|
||||
payload: payload
|
||||
token: token,
|
||||
payload: payload,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -53,42 +51,47 @@ module.exports = function () {
|
||||
* @param {String} token
|
||||
* @returns {Promise}
|
||||
*/
|
||||
load: function (token) {
|
||||
if (!config.getPublicKey()) {
|
||||
logger.error('Public key is empty!');
|
||||
load: (token) => {
|
||||
if (!getPublicKey()) {
|
||||
logger.error("Public key is empty!");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
if (!token || token === null || token === 'null') {
|
||||
reject(new error.AuthError('Empty token'));
|
||||
if (!token || token === null || token === "null") {
|
||||
reject(new errs.AuthError("Empty token"));
|
||||
} else {
|
||||
jwt.verify(token, config.getPublicKey(), {ignoreExpiration: false, algorithms: [ALGO]}, (err, result) => {
|
||||
if (err) {
|
||||
|
||||
if (err.name === 'TokenExpiredError') {
|
||||
reject(new error.AuthError('Token has expired', err));
|
||||
jwt.verify(
|
||||
token,
|
||||
getPublicKey(),
|
||||
{ ignoreExpiration: false, algorithms: [ALGO] },
|
||||
(err, result) => {
|
||||
if (err) {
|
||||
if (err.name === "TokenExpiredError") {
|
||||
reject(new errs.AuthError("Token has expired", err));
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
} else {
|
||||
reject(err);
|
||||
token_data = result;
|
||||
|
||||
// Hack: some tokens out in the wild have a scope of 'all' instead of 'user'.
|
||||
// For 30 days at least, we need to replace 'all' with user.
|
||||
if (
|
||||
typeof token_data.scope !== "undefined" &&
|
||||
_.indexOf(token_data.scope, "all") !== -1
|
||||
) {
|
||||
token_data.scope = ["user"];
|
||||
}
|
||||
|
||||
resolve(token_data);
|
||||
}
|
||||
|
||||
} else {
|
||||
token_data = result;
|
||||
|
||||
// Hack: some tokens out in the wild have a scope of 'all' instead of 'user'.
|
||||
// For 30 days at least, we need to replace 'all' with user.
|
||||
if ((typeof token_data.scope !== 'undefined' && _.indexOf(token_data.scope, 'all') !== -1)) {
|
||||
token_data.scope = ['user'];
|
||||
}
|
||||
|
||||
resolve(token_data);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -97,16 +100,14 @@ module.exports = function () {
|
||||
* @param {String} scope
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
hasScope: function (scope) {
|
||||
return typeof token_data.scope !== 'undefined' && _.indexOf(token_data.scope, scope) !== -1;
|
||||
},
|
||||
hasScope: (scope) => typeof token_data.scope !== "undefined" && _.indexOf(token_data.scope, scope) !== -1,
|
||||
|
||||
/**
|
||||
* @param {String} key
|
||||
* @return {*}
|
||||
*/
|
||||
get: function (key) {
|
||||
if (typeof token_data[key] !== 'undefined') {
|
||||
get: (key) => {
|
||||
if (typeof token_data[key] !== "undefined") {
|
||||
return token_data[key];
|
||||
}
|
||||
|
||||
@@ -117,7 +118,7 @@ module.exports = function () {
|
||||
* @param {String} key
|
||||
* @param {*} value
|
||||
*/
|
||||
set: function (key, value) {
|
||||
set: (key, value) => {
|
||||
token_data[key] = value;
|
||||
},
|
||||
|
||||
@@ -126,13 +127,13 @@ module.exports = function () {
|
||||
* @returns {Integer}
|
||||
*/
|
||||
getUserId: (default_value) => {
|
||||
const attrs = self.get('attrs');
|
||||
if (attrs && typeof attrs.id !== 'undefined' && attrs.id) {
|
||||
const attrs = self.get("attrs");
|
||||
if (attrs && typeof attrs.id !== "undefined" && attrs.id) {
|
||||
return attrs.id;
|
||||
}
|
||||
|
||||
return default_value || 0;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return self;
|
||||
|
Reference in New Issue
Block a user