1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/access-tokens/exchange.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-09-04 10:06:59 -05:00
const { getRemoteAddress } = require('../../../utils/remoteAddress');
const Errors = {
INVALID_TOKEN: {
2023-10-17 19:18:19 +02:00
invalidToken: 'Invalid token',
},
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
2023-09-04 10:06:59 -05:00
},
MISSING_VALUES: {
2023-10-17 19:18:19 +02:00
missingValues: 'Unable to retrieve required values (email, name)',
2023-09-04 10:06:59 -05:00
},
};
module.exports = {
inputs: {
token: {
type: 'string',
required: true,
},
},
exits: {
invalidToken: {
responseType: 'unauthorized',
},
2023-10-17 19:18:19 +02:00
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
2023-09-04 10:06:59 -05:00
missingValues: {
2023-10-17 19:18:19 +02:00
responseType: 'unprocessableEntity',
2023-09-04 10:06:59 -05:00
},
},
async fn(inputs) {
2023-10-17 19:18:19 +02:00
const remoteAddress = getRemoteAddress(this.req);
2023-09-04 10:06:59 -05:00
2023-10-17 19:18:19 +02:00
const user = await sails.helpers.users
.getOrCreateOneByOidcToken(inputs.token)
.intercept('invalidToken', () => {
sails.log.warn(`Invalid token! (IP: ${remoteAddress})`);
return Errors.INVALID_TOKEN;
})
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE)
.intercept('missingValues', () => Errors.MISSING_VALUES);
2023-09-04 10:06:59 -05:00
2023-10-17 19:18:19 +02:00
const accessToken = sails.helpers.utils.createToken(user.id);
2023-09-04 10:06:59 -05:00
await Session.create({
2023-10-17 19:18:19 +02:00
accessToken,
2023-09-04 10:06:59 -05:00
remoteAddress,
userId: user.id,
userAgent: this.req.headers['user-agent'],
});
return {
2023-10-17 19:18:19 +02:00
item: accessToken,
2023-09-04 10:06:59 -05:00
};
},
};