Files
planka/server/api/controllers/access-tokens/create.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
2019-08-31 04:07:25 +05:00
const bcrypt = require('bcrypt');
const { isEmailOrUsername } = require('../../../utils/validators');
const { getRemoteAddress } = require('../../../utils/remote-address');
2019-08-31 04:07:25 +05:00
const Errors = {
INVALID_CREDENTIALS: {
invalidCredentials: 'Invalid credentials',
},
2020-04-03 00:35:25 +05:00
INVALID_EMAIL_OR_USERNAME: {
invalidEmailOrUsername: 'Invalid email or username',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
INVALID_PASSWORD: {
invalidPassword: 'Invalid password',
},
2023-10-17 19:18:19 +02:00
USE_SINGLE_SIGN_ON: {
useSingleSignOn: 'Use single sign-on',
},
2025-08-21 15:10:02 +02:00
TERMS_ACCEPTANCE_REQUIRED: {
termsAcceptanceRequired: 'Terms acceptance required',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
2020-04-03 00:35:25 +05:00
emailOrUsername: {
2019-08-31 04:07:25 +05:00
type: 'string',
maxLength: 256,
custom: isEmailOrUsername,
2019-08-31 04:07:25 +05:00
required: true,
},
password: {
type: 'string',
maxLength: 256,
required: true,
},
withHttpOnlyToken: {
type: 'boolean',
},
2019-08-31 04:07:25 +05:00
},
exits: {
invalidCredentials: {
responseType: 'unauthorized',
},
2020-04-03 00:35:25 +05:00
invalidEmailOrUsername: {
responseType: 'unauthorized',
},
invalidPassword: {
responseType: 'unauthorized',
},
2023-10-17 19:18:19 +02:00
useSingleSignOn: {
responseType: 'forbidden',
},
2025-08-21 15:10:02 +02:00
termsAcceptanceRequired: {
responseType: 'forbidden',
},
adminLoginRequiredToInitializeInstance: {
responseType: 'forbidden',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
if (sails.config.custom.oidcEnforced) {
throw Errors.USE_SINGLE_SIGN_ON;
}
const remoteAddress = getRemoteAddress(this.req);
const user = await User.qm.getOneActiveByEmailOrUsername(inputs.emailOrUsername);
2019-08-31 04:07:25 +05:00
if (!user) {
sails.log.warn(
`Invalid email or username: "${inputs.emailOrUsername}"! (IP: ${remoteAddress})`,
);
throw sails.config.custom.showDetailedAuthErrors
? Errors.INVALID_EMAIL_OR_USERNAME
: Errors.INVALID_CREDENTIALS;
2019-08-31 04:07:25 +05:00
}
if (user.isSsoUser) {
throw Errors.USE_SINGLE_SIGN_ON;
}
const isPasswordValid = await bcrypt.compare(inputs.password, user.password);
if (!isPasswordValid) {
sails.log.warn(`Invalid password! (IP: ${remoteAddress})`);
throw sails.config.custom.showDetailedAuthErrors
? Errors.INVALID_PASSWORD
: Errors.INVALID_CREDENTIALS;
2019-08-31 04:07:25 +05:00
}
2025-08-21 15:10:02 +02:00
return sails.helpers.accessTokens.handleSteps
.with({
user,
remoteAddress,
request: this.req,
response: this.res,
withHttpOnlyToken: inputs.withHttpOnlyToken,
})
.intercept('adminLoginRequiredToInitializeInstance', (error) => ({
adminLoginRequiredToInitializeInstance: error.raw,
}))
.intercept('termsAcceptanceRequired', (error) => ({
termsAcceptanceRequired: error.raw,
}));
},
2019-08-31 04:07:25 +05:00
};