Skip to content

Node.js based forum software built for the modern web

feat: require password for creating master token

Barış Soner Uşaklı7b7b7d288e47
diff --git a/public/src/admin/manage/api.js b/public/src/admin/manage/api.js
index 78bee71572..7c23f726f2 100644
--- a/public/src/admin/manage/api.js
+++ b/public/src/admin/manage/api.js
@@ -57,9 +57,25 @@ define('admin/manage/api', ['settings', 'clipboard', 'modals', 'benchpress', 'ap
const formData = new FormData(formEl);
const uid = formData.get('uid');
const description = formData.get('description');
+ const isMasterToken = parseInt(uid, 10) === 0;
try {
- const tokenObj = await api.post('/admin/tokens', { uid, description });
+ let password;
+ if (isMasterToken) {
+ password = await modals.promptPassword({
+ title: '[[user:current-password]]',
+ message: '[[user:emailUpdate.password-challenge]]',
+ });
+ if (!password) {
+ return false;
+ }
+ }
+
+ const tokenObj = await api.post('/admin/tokens', {
+ uid,
+ description,
+ ...(isMasterToken ? { password } : {}),
+ });
if (!tokensTableBody) {
modal.modal('hide');
return ajaxify.refresh();
diff --git a/public/src/modules/modals.js b/public/src/modules/modals.js
index e751285cdf..59856bc178 100644
--- a/public/src/modules/modals.js
+++ b/public/src/modules/modals.js
@@ -64,6 +64,23 @@ export async function prompt(opts, callback) {
return bootbox.prompt({ ...opts, title, message, callback });
}
+export async function promptPassword(opts) {
+ if (typeof opts === 'string') {
+ opts = { title: opts };
+ }
+ opts = opts || {};
+
+ return await new Promise((resolve) => {
+ prompt({
+ ...opts,
+ inputType: 'password',
+ required: true,
+ value: '',
+ callback: resolve,
+ });
+ });
+}
+
function normalizeMessage(opts) {
const msg = opts && opts.message ? opts.message : opts;
if (msg instanceof jQuery) {
diff --git a/src/controllers/write/admin.js b/src/controllers/write/admin.js
index a3f574a0e8..bbab1f0345 100644
--- a/src/controllers/write/admin.js
+++ b/src/controllers/write/admin.js
@@ -7,6 +7,7 @@ const messaging = require('../../messaging');
const events = require('../../events');
const activitypub = require('../../activitypub');
const utils = require('../../utils');
+const user = require('../../user');
const Admin = module.exports;
@@ -35,7 +36,19 @@ Admin.getAnalyticsData = async (req, res) => {
};
Admin.generateToken = async (req, res) => {
- const { uid, description } = req.body;
+ const uid = String(req.body.uid).trim();
+ if (uid === '0') {
+ const { password } = req.body;
+ if (!password) {
+ throw new Error('[[error:invalid-password]]');
+ }
+
+ const validPassword = await user.isPasswordCorrect(req.uid, password, req.ip);
+ if (!validPassword) {
+ throw new Error('[[error:invalid-password]]');
+ }
+ }
+ const { description } = req.body;
const token = await api.utils.tokens.generate({ uid, description });
await events.log({
type: 'token-add',