2019-07-17 17:43:07 +03:00
|
|
|
const api = require('./index');
|
|
|
|
const web = require('../../web');
|
2019-07-24 15:53:09 +03:00
|
|
|
const models = require('../../models');
|
2019-07-24 13:40:18 +03:00
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const invitations = require('../../services/invitations');
|
2019-07-17 17:43:07 +03:00
|
|
|
|
|
|
|
module.exports = {
|
2019-07-23 19:30:17 +03:00
|
|
|
docName: 'authentication',
|
2019-07-17 17:43:07 +03:00
|
|
|
|
|
|
|
generateResetToken: {
|
|
|
|
permissions: true,
|
|
|
|
options: [
|
|
|
|
'email'
|
|
|
|
],
|
|
|
|
query(frame) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return auth.setup.assertSetupCompleted(true);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return auth.passwordreset.generateToken(frame.data.email, api.settings);
|
|
|
|
})
|
|
|
|
.then((token) => {
|
|
|
|
return auth.passwordreset.sendResetNotification(token, api.mail);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetPassword: {
|
2019-07-23 19:30:17 +03:00
|
|
|
validation: {
|
|
|
|
docName: 'passwordreset',
|
|
|
|
data: {
|
|
|
|
token: {required: true},
|
|
|
|
newPassword: {required: true},
|
|
|
|
ne2Password: {required: true}
|
|
|
|
}
|
|
|
|
},
|
2019-07-17 17:43:07 +03:00
|
|
|
permissions: false,
|
|
|
|
options: [
|
|
|
|
'ip'
|
|
|
|
],
|
|
|
|
query(frame) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return auth.setup.assertSetupCompleted(true);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return auth.passwordreset.extractTokenParts(frame);
|
|
|
|
})
|
|
|
|
.then((params) => {
|
|
|
|
return auth.passwordreset.protectBruteForce(params);
|
|
|
|
})
|
|
|
|
.then(({options, tokenParts}) => {
|
|
|
|
options = Object.assign(options, {context: {internal: true}});
|
|
|
|
return auth.passwordreset.doReset(options, tokenParts, api.settings)
|
|
|
|
.then((params) => {
|
|
|
|
web.shared.middlewares.api.spamPrevention.userLogin().reset(frame.options.ip, `${tokenParts.email}login`);
|
|
|
|
return params;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-07-24 13:40:18 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
acceptInvitation: {
|
|
|
|
validation: {
|
|
|
|
docName: 'invitations'
|
|
|
|
},
|
|
|
|
permissions: false,
|
|
|
|
query(frame) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return auth.setup.assertSetupCompleted(true);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return invitations.accept(frame.data);
|
|
|
|
});
|
|
|
|
}
|
2019-07-24 15:53:09 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isInvitation: {
|
|
|
|
validation: {
|
|
|
|
docName: 'invitations'
|
|
|
|
},
|
|
|
|
permissions: false,
|
|
|
|
query(frame) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return auth.setup.assertSetupCompleted(true);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
const email = frame.data.email;
|
|
|
|
|
2019-07-24 17:18:44 +03:00
|
|
|
return models.Invite.findOne({email: email, status: 'sent'}, frame.options);
|
2019-07-24 15:53:09 +03:00
|
|
|
});
|
|
|
|
}
|
2019-07-17 17:43:07 +03:00
|
|
|
}
|
|
|
|
};
|