mailPasswords table should work with oidc clients not apps

This commit is contained in:
Johannes Zellner
2026-02-18 15:17:08 +01:00
parent 5e7e739589
commit 43acecfc6e
4 changed files with 27 additions and 30 deletions
+14 -14
View File
@@ -3,13 +3,13 @@ import BoxError from './boxerror.js';
import database from './database.js';
import safe from 'safetydance';
const MAIL_PASSWORD_FIELDS = [ 'appId', 'userId', 'password', 'creationTime' ].join(',');
const MAIL_PASSWORD_FIELDS = [ 'clientId', 'userId', 'password', 'creationTime' ].join(',');
async function get(appId, userId) {
assert.strictEqual(typeof appId, 'string');
async function get(clientId, userId) {
assert.strictEqual(typeof clientId, 'string');
assert.strictEqual(typeof userId, 'string');
const result = await database.query('SELECT ' + MAIL_PASSWORD_FIELDS + ' FROM mailPasswords WHERE appId = ? AND userId = ?', [ appId, userId ]);
const result = await database.query('SELECT ' + MAIL_PASSWORD_FIELDS + ' FROM mailPasswords WHERE clientId = ? AND userId = ?', [ clientId, userId ]);
if (result.length === 0) return null;
return result[0];
}
@@ -20,37 +20,37 @@ async function getByUserId(userId) {
return await database.query('SELECT ' + MAIL_PASSWORD_FIELDS + ' FROM mailPasswords WHERE userId = ?', [ userId ]);
}
async function add(appId, userId, password) {
assert.strictEqual(typeof appId, 'string');
async function add(clientId, userId, password) {
assert.strictEqual(typeof clientId, 'string');
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof password, 'string');
if (appId.length < 1) throw new BoxError(BoxError.BAD_FIELD, 'appId must be at least 1 char');
if (clientId.length < 1) throw new BoxError(BoxError.BAD_FIELD, 'clientId must be at least 1 char');
if (userId.length < 1) throw new BoxError(BoxError.BAD_FIELD, 'userId must be at least 1 char');
if (password.length < 1) throw new BoxError(BoxError.BAD_FIELD, 'password must be at least 1 char');
const query = 'INSERT INTO mailPasswords (appId, userId, password) VALUES (?, ?, ?)';
const args = [ appId, userId, password ];
const query = 'INSERT INTO mailPasswords (clientId, userId, password) VALUES (?, ?, ?)';
const args = [ clientId, userId, password ];
const [error] = await safe(database.query(query, args));
if (error && error.sqlCode === 'ER_DUP_ENTRY') throw new BoxError(BoxError.ALREADY_EXISTS, 'mail password for this app and user already exists');
if (error && error.sqlCode === 'ER_NO_REFERENCED_ROW_2') throw new BoxError(BoxError.NOT_FOUND, 'app or user not found');
if (error) throw error;
return { appId, userId };
return { clientId, userId };
}
async function list(userId) {
assert.strictEqual(typeof userId, 'string');
return await database.query('SELECT ' + MAIL_PASSWORD_FIELDS + ' FROM mailPasswords WHERE userId = ? ORDER BY appId', [ userId ]);
return await database.query('SELECT ' + MAIL_PASSWORD_FIELDS + ' FROM mailPasswords WHERE userId = ? ORDER BY clientId', [ userId ]);
}
async function del(appId, userId) {
assert.strictEqual(typeof appId, 'string');
async function del(clientId, userId) {
assert.strictEqual(typeof clientId, 'string');
assert.strictEqual(typeof userId, 'string');
const result = await database.query('DELETE FROM mailPasswords WHERE appId = ? AND userId = ?', [ appId, userId ]);
const result = await database.query('DELETE FROM mailPasswords WHERE clientId = ? AND userId = ?', [ clientId, userId ]);
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'mail password not found');
}