|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const AuthRequest = require('./auth-request') |
| 4 | +const debug = require('./../debug').accounts |
| 5 | + |
| 6 | +class DeleteAccountConfirmRequest extends AuthRequest { |
| 7 | + /** |
| 8 | + * @constructor |
| 9 | + * @param options {Object} |
| 10 | + * @param options.accountManager {AccountManager} |
| 11 | + * @param options.userStore {UserStore} |
| 12 | + * @param options.response {ServerResponse} express response object |
| 13 | + * @param [options.token] {string} One-time reset password token (from email) |
| 14 | + */ |
| 15 | + constructor (options) { |
| 16 | + super(options) |
| 17 | + |
| 18 | + this.token = options.token |
| 19 | + this.validToken = false |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Factory method, returns an initialized instance of DeleteAccountConfirmRequest |
| 24 | + * from an incoming http request. |
| 25 | + * |
| 26 | + * @param req {IncomingRequest} |
| 27 | + * @param res {ServerResponse} |
| 28 | + * |
| 29 | + * @return {DeleteAccountConfirmRequest} |
| 30 | + */ |
| 31 | + static fromParams (req, res) { |
| 32 | + let locals = req.app.locals |
| 33 | + let accountManager = locals.accountManager |
| 34 | + let userStore = locals.oidc.users |
| 35 | + |
| 36 | + let token = this.parseParameter(req, 'token') |
| 37 | + |
| 38 | + let options = { |
| 39 | + accountManager, |
| 40 | + userStore, |
| 41 | + token, |
| 42 | + response: res |
| 43 | + } |
| 44 | + |
| 45 | + return new DeleteAccountConfirmRequest(options) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Handles a Change Password GET request on behalf of a middleware handler. |
| 50 | + * |
| 51 | + * @param req {IncomingRequest} |
| 52 | + * @param res {ServerResponse} |
| 53 | + * |
| 54 | + * @return {Promise} |
| 55 | + */ |
| 56 | + static get (req, res) { |
| 57 | + const request = DeleteAccountConfirmRequest.fromParams(req, res) |
| 58 | + |
| 59 | + return Promise.resolve() |
| 60 | + .then(() => request.validateToken()) |
| 61 | + .then(() => request.renderForm()) |
| 62 | + .catch(error => request.error(error)) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Handles a Change Password POST request on behalf of a middleware handler. |
| 67 | + * |
| 68 | + * @param req {IncomingRequest} |
| 69 | + * @param res {ServerResponse} |
| 70 | + * |
| 71 | + * @return {Promise} |
| 72 | + */ |
| 73 | + static post (req, res) { |
| 74 | + const request = DeleteAccountConfirmRequest.fromParams(req, res) |
| 75 | + |
| 76 | + return DeleteAccountConfirmRequest.handlePost(request) |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Performs the 'Change Password' operation, after the user submits the |
| 81 | + * password change form. Validates the parameters (the one-time token, |
| 82 | + * the new password), changes the password, and renders the success view. |
| 83 | + * |
| 84 | + * @param request {DeleteAccountConfirmRequest} |
| 85 | + * |
| 86 | + * @return {Promise} |
| 87 | + */ |
| 88 | + static handlePost (request) { |
| 89 | + return Promise.resolve() |
| 90 | + .then(() => request.validateToken()) |
| 91 | + .then(tokenContents => request.deleteAccount(tokenContents)) |
| 92 | + .then(() => request.renderSuccess()) |
| 93 | + .catch(error => request.error(error)) |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Validates the one-time Password Reset token that was emailed to the user. |
| 98 | + * If the token service has a valid token saved for the given key, it returns |
| 99 | + * the token object value (which contains the user's WebID URI, etc). |
| 100 | + * If no token is saved, returns `false`. |
| 101 | + * |
| 102 | + * @return {Promise<Object|false>} |
| 103 | + */ |
| 104 | + validateToken () { |
| 105 | + return Promise.resolve() |
| 106 | + .then(() => { |
| 107 | + if (!this.token) { return false } |
| 108 | + |
| 109 | + return this.accountManager.validateResetToken(this.token) |
| 110 | + }) |
| 111 | + .then(validToken => { |
| 112 | + if (validToken) { |
| 113 | + this.validToken = true |
| 114 | + } |
| 115 | + |
| 116 | + return validToken |
| 117 | + }) |
| 118 | + .catch(error => { |
| 119 | + this.token = null |
| 120 | + throw error |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Changes the password that's saved in the user store. |
| 126 | + * If the user has no user store entry, it creates one. |
| 127 | + * |
| 128 | + * @param tokenContents {Object} |
| 129 | + * @param tokenContents.webId {string} |
| 130 | + * |
| 131 | + * @return {Promise} |
| 132 | + */ |
| 133 | + deleteAccount (tokenContents) { |
| 134 | + let user = this.accountManager.userAccountFrom(tokenContents) |
| 135 | + |
| 136 | + debug('Delete account for user:', user.webId) |
| 137 | + |
| 138 | + return this.userStore.findUser(user.id) |
| 139 | + .then(userStoreEntry => { |
| 140 | + // TODO: @kjetilk delete the user here |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Renders the 'change password' form. |
| 146 | + * |
| 147 | + * @param [error] {Error} Optional error to display |
| 148 | + */ |
| 149 | + renderForm (error) { |
| 150 | + let params = { |
| 151 | + validToken: this.validToken, |
| 152 | + token: this.token |
| 153 | + } |
| 154 | + |
| 155 | + if (error) { |
| 156 | + params.error = error.message |
| 157 | + this.response.status(error.statusCode) |
| 158 | + } |
| 159 | + |
| 160 | + this.response.render('account/delete-confirm', params) |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Displays the 'password has been changed' success view. |
| 165 | + */ |
| 166 | + renderSuccess () { |
| 167 | + this.response.render('account/account-deleted') |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +module.exports = DeleteAccountConfirmRequest |
0 commit comments