|
3 | 3 | const AuthRequest = require('./auth-request') |
4 | 4 | const WebIdTlsCertificate = require('../models/webid-tls-certificate') |
5 | 5 | const debug = require('../debug').accounts |
| 6 | +const blacklistService = require('../services/blacklist-service') |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Represents a 'create new user account' http request (either a POST to the |
@@ -115,30 +116,28 @@ class CreateAccountRequest extends AuthRequest { |
115 | 116 | /** |
116 | 117 | * Creates an account for a given user (from a POST to `/api/accounts/new`) |
117 | 118 | * |
118 | | - * @throws {Error} An http 400 error if an account already exists |
| 119 | + * @throws {Error} If errors were encountering while validating the username. |
119 | 120 | * |
120 | 121 | * @return {Promise<UserAccount>} Resolves with newly created account instance |
121 | 122 | */ |
122 | | - createAccount () { |
| 123 | + async createAccount () { |
123 | 124 | let userAccount = this.userAccount |
124 | 125 | let accountManager = this.accountManager |
125 | 126 |
|
126 | | - return Promise.resolve(userAccount) |
127 | | - .then(this.cancelIfUsernameInvalid.bind(this)) |
128 | | - .then(this.cancelIfAccountExists.bind(this)) |
129 | | - .then(this.createAccountStorage.bind(this)) |
130 | | - .then(this.saveCredentialsFor.bind(this)) |
131 | | - .then(this.sendResponse.bind(this)) |
132 | | - .then(userAccount => { |
133 | | - // 'return' not used deliberately, no need to block and wait for email |
134 | | - if (userAccount && userAccount.email) { |
135 | | - debug('Sending Welcome email') |
136 | | - accountManager.sendWelcomeEmail(userAccount) |
137 | | - } |
138 | | - }) |
139 | | - .then(() => { |
140 | | - return userAccount |
141 | | - }) |
| 127 | + this.cancelIfUsernameInvalid(userAccount) |
| 128 | + this.cancelIfBlacklistedUsername(userAccount) |
| 129 | + await this.cancelIfAccountExists(userAccount) |
| 130 | + await this.createAccountStorage(userAccount) |
| 131 | + await this.saveCredentialsFor(userAccount) |
| 132 | + await this.sendResponse(userAccount) |
| 133 | + |
| 134 | + // 'return' not used deliberately, no need to block and wait for email |
| 135 | + if (userAccount && userAccount.email) { |
| 136 | + debug('Sending Welcome email') |
| 137 | + accountManager.sendWelcomeEmail(userAccount) |
| 138 | + } |
| 139 | + |
| 140 | + return userAccount |
142 | 141 | } |
143 | 142 |
|
144 | 143 | /** |
@@ -196,12 +195,33 @@ class CreateAccountRequest extends AuthRequest { |
196 | 195 | * @throws {Error} If errors were encountering while validating the |
197 | 196 | * username. |
198 | 197 | * |
199 | | - * @return {Promise<UserAccount>} Chainable |
| 198 | + * @return {UserAccount} Chainable |
200 | 199 | */ |
201 | 200 | cancelIfUsernameInvalid (userAccount) { |
202 | 201 | if (!userAccount.username || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(userAccount.username)) { |
203 | 202 | debug('Invalid username ' + userAccount.username) |
204 | | - const error = new Error('Invalid username') |
| 203 | + const error = new Error('Invalid username (contains invalid characters)') |
| 204 | + error.status = 400 |
| 205 | + throw error |
| 206 | + } |
| 207 | + |
| 208 | + return userAccount |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Check if a username is a valid slug. |
| 213 | + * |
| 214 | + * @param userAccount {UserAccount} Instance of the account to be created |
| 215 | + * |
| 216 | + * @throws {Error} If username is blacklisted |
| 217 | + * |
| 218 | + * @return {UserAccount} Chainable |
| 219 | + */ |
| 220 | + cancelIfBlacklistedUsername (userAccount) { |
| 221 | + const validUsername = blacklistService.validate(userAccount.username) |
| 222 | + if (!validUsername) { |
| 223 | + debug('Invalid username ' + userAccount.username) |
| 224 | + const error = new Error('Invalid username (username is blacklisted)') |
205 | 225 | error.status = 400 |
206 | 226 | throw error |
207 | 227 | } |
|
0 commit comments