|
1 | 1 | import debugModule from '../debug.mjs'; |
| 2 | +import AuthRequest from './auth-request.mjs'; |
2 | 3 |
|
3 | 4 | const debug = debugModule.accounts; |
4 | 5 |
|
5 | | -export default class PasswordChangeRequest { |
6 | | - constructor(options) { |
7 | | - this.accountManager = options.accountManager; |
8 | | - this.userAccount = options.userAccount; |
9 | | - this.oldPassword = options.oldPassword; |
10 | | - this.newPassword = options.newPassword; |
11 | | - this.response = options.response; |
12 | | - } |
13 | | - static handle(req, res, accountManager) { |
14 | | - let request; |
15 | | - try { |
16 | | - request = PasswordChangeRequest.fromParams(req, res, accountManager); |
17 | | - } catch (error) { |
18 | | - return Promise.reject(error); |
19 | | - } |
20 | | - return PasswordChangeRequest.changePassword(request); |
21 | | - } |
22 | | - static fromParams(req, res, accountManager) { |
23 | | - const userAccount = accountManager.userAccountFrom(req.body); |
24 | | - const oldPassword = req.body.oldPassword; |
25 | | - const newPassword = req.body.newPassword; |
26 | | - if (!oldPassword || !newPassword) { |
27 | | - const error = new Error('Old and new passwords are required'); |
28 | | - error.status = 400; |
29 | | - throw error; |
| 6 | +export default class PasswordChangeRequest extends AuthRequest { |
| 7 | + constructor (options) { |
| 8 | + super(options) |
| 9 | + |
| 10 | + this.token = options.token |
| 11 | + this.returnToUrl = options.returnToUrl |
| 12 | + |
| 13 | + this.validToken = false |
| 14 | + |
| 15 | + this.newPassword = options.newPassword |
| 16 | + this.userStore = options.userStore |
| 17 | + this.response = options.response |
| 18 | + } |
| 19 | + |
| 20 | + static fromParams (req, res) { |
| 21 | + const locals = req.app && req.app.locals ? req.app.locals : {} |
| 22 | + const accountManager = locals.accountManager |
| 23 | + const userStore = locals.oidc ? locals.oidc.users : undefined |
| 24 | + |
| 25 | + const returnToUrl = this.parseParameter(req, 'returnToUrl') |
| 26 | + const token = this.parseParameter(req, 'token') |
| 27 | + const oldPassword = this.parseParameter(req, 'password') |
| 28 | + const newPassword = this.parseParameter(req, 'newPassword') |
| 29 | + |
| 30 | + const options = { |
| 31 | + accountManager, |
| 32 | + userStore, |
| 33 | + returnToUrl, |
| 34 | + token, |
| 35 | + oldPassword, |
| 36 | + newPassword, |
| 37 | + response: res |
30 | 38 | } |
31 | | - if (req.session.userId !== userAccount.webId) { |
32 | | - debug(`Cannot change password: signed in user is "${req.session.userId}"`); |
33 | | - const error = new Error("You are not logged in, so you can't change the password"); |
34 | | - error.status = 401; |
35 | | - throw error; |
| 39 | + |
| 40 | + return new PasswordChangeRequest(options) |
| 41 | + } |
| 42 | + |
| 43 | + static get (req, res) { |
| 44 | + const request = PasswordChangeRequest.fromParams(req, res) |
| 45 | + |
| 46 | + return Promise.resolve() |
| 47 | + .then(() => request.validateToken()) |
| 48 | + .then(() => request.renderForm()) |
| 49 | + .catch(error => request.error(error)) |
| 50 | + } |
| 51 | + |
| 52 | + static post (req, res) { |
| 53 | + const request = PasswordChangeRequest.fromParams(req, res) |
| 54 | + |
| 55 | + return PasswordChangeRequest.handlePost(request) |
| 56 | + } |
| 57 | + |
| 58 | + static handlePost (request) { |
| 59 | + return Promise.resolve() |
| 60 | + .then(() => request.validatePost()) |
| 61 | + .then(() => request.validateToken()) |
| 62 | + .then(tokenContents => request.changePassword(tokenContents)) |
| 63 | + .then(() => request.renderSuccess()) |
| 64 | + .catch(error => request.error(error)) |
| 65 | + } |
| 66 | + |
| 67 | + validatePost () { |
| 68 | + if (!this.newPassword) { |
| 69 | + throw new Error('Please enter a new password') |
36 | 70 | } |
37 | | - const options = { accountManager, userAccount, oldPassword, newPassword, response: res }; |
38 | | - return new PasswordChangeRequest(options); |
39 | | - } |
40 | | - static changePassword(request) { |
41 | | - const { accountManager, userAccount, oldPassword, newPassword } = request; |
42 | | - return accountManager.changePassword(userAccount, oldPassword, newPassword) |
43 | | - .catch(err => { |
44 | | - err.status = 400; |
45 | | - err.message = 'Error changing password: ' + err.message; |
46 | | - throw err; |
47 | | - }) |
| 71 | + } |
| 72 | + |
| 73 | + validateToken () { |
| 74 | + return Promise.resolve() |
48 | 75 | .then(() => { |
49 | | - request.sendResponse(); |
50 | | - }); |
| 76 | + if (!this.token) { return false } |
| 77 | + |
| 78 | + return this.accountManager.validateResetToken(this.token) |
| 79 | + }) |
| 80 | + .then(validToken => { |
| 81 | + if (validToken) { |
| 82 | + this.validToken = true |
| 83 | + } |
| 84 | + |
| 85 | + return validToken |
| 86 | + }) |
| 87 | + .catch(error => { |
| 88 | + this.token = null |
| 89 | + throw error |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + changePassword (tokenContents) { |
| 94 | + const user = this.accountManager.userAccountFrom(tokenContents) |
| 95 | + |
| 96 | + debug('Changing password for user:', user.webId) |
| 97 | + |
| 98 | + return this.userStore.findUser(user.id) |
| 99 | + .then(userStoreEntry => { |
| 100 | + if (userStoreEntry) { |
| 101 | + return this.userStore.updatePassword(user, this.newPassword) |
| 102 | + } else { |
| 103 | + return this.userStore.createUser(user, this.newPassword) |
| 104 | + } |
| 105 | + }) |
51 | 106 | } |
52 | | - sendResponse() { |
53 | | - const { response } = this; |
54 | | - response.status(200); |
55 | | - response.send({ message: 'Password changed successfully' }); |
| 107 | + |
| 108 | + renderForm (error) { |
| 109 | + const params = { |
| 110 | + validToken: this.validToken, |
| 111 | + returnToUrl: this.returnToUrl, |
| 112 | + token: this.token |
| 113 | + } |
| 114 | + |
| 115 | + if (error) { |
| 116 | + params.error = error.message |
| 117 | + this.response.status(error.statusCode) |
| 118 | + } |
| 119 | + |
| 120 | + this.response.render('auth/change-password', params) |
| 121 | + } |
| 122 | + |
| 123 | + renderSuccess () { |
| 124 | + this.response.render('auth/password-changed', { returnToUrl: this.returnToUrl }) |
| 125 | + } |
| 126 | + |
| 127 | + error (error) { |
| 128 | + error.statusCode = error.statusCode || 400 |
| 129 | + |
| 130 | + this.renderForm(error) |
56 | 131 | } |
57 | 132 | } |
0 commit comments