Skip to content

Commit fd6b00b

Browse files
committed
Extracted validation of username into user-utils
1 parent d1143b5 commit fd6b00b

File tree

4 files changed

+72
-30
lines changed

4 files changed

+72
-30
lines changed

bin/lib/blacklist.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const util = require('util')
33
const { URL } = require('url')
44

55
const { loadConfig } = require('./common')
6+
const { isValidUsername } = require('../../lib/common/user-utils')
67
const blacklistService = require('../../lib/services/blacklist-service')
78

8-
const AccountManager = require('../../lib/models/account-manager')
9-
const LDP = require('../../lib/ldp')
10-
const SolidHost = require('../../lib/models/solid-host')
9+
// const AccountManager = require('../../lib/models/account-manager')
10+
// const LDP = require('../../lib/ldp')
11+
// const SolidHost = require('../../lib/models/solid-host')
1112

1213
module.exports = function (program) {
1314
program
@@ -21,25 +22,29 @@ module.exports = function (program) {
2122
return console.error('You are running a single user server, no need to check for blacklisted users')
2223
}
2324

24-
const host = SolidHost.from({ port: config.port, serverUri: config.serverUri })
25+
// const host = SolidHost.from({ port: config.port, serverUri: config.serverUri })
2526
const invalidUsernames = await getInvalidUsernames(config)
26-
console.log(invalidUsernames)
2727

28-
const ldp = new LDP(config)
29-
const accountManager = AccountManager.from({
30-
// authMethod: argv.auth,
31-
// emailService: app.locals.emailService,
32-
// tokenService: app.locals.tokenService,
33-
host,
34-
// accountTemplatePath: argv.templates.account,
35-
store: ldp,
36-
multiuser: config.multiuser
37-
})
38-
const blacklistedUsernames = await getBlacklistedUsernames(accountManager)
39-
if (blacklistedUsernames.length === 0) {
40-
console.log('No blacklisted username was found')
28+
// const ldp = new LDP(config)
29+
// const accountManager = AccountManager.from({
30+
// // authMethod: argv.auth,
31+
// // emailService: app.locals.emailService,
32+
// // tokenService: app.locals.tokenService,
33+
// host,
34+
// // accountTemplatePath: argv.templates.account,
35+
// store: ldp,
36+
// multiuser: config.multiuser
37+
// })
38+
// const blacklistedUsernames = await getBlacklistedUsernames(accountManager)
39+
// if (blacklistedUsernames.length === 0) {
40+
// console.log('No blacklisted username was found')
41+
// }
42+
// console.log(`These blacklisted usernames were found:${blacklistedUsernames.map(username => `\n- ${username}`)}`)
43+
44+
if (invalidUsernames.length === 0) {
45+
console.log('No invalid username was found')
4146
}
42-
console.log(`These blacklisted usernames were found:${blacklistedUsernames.map(username => `\n- ${username}`)}`)
47+
console.log(`${invalidUsernames.length} invalid usernames were found:${invalidUsernames.map(username => `\n- ${username}`)}`)
4348
})
4449
}
4550

@@ -50,15 +55,16 @@ async function getInvalidUsernames (config) {
5055
return files
5156
.filter(file => isUserDirectory.test(file))
5257
.map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
58+
.filter(username => !isValidUsername(username) || !blacklistService.validate(username))
5359
}
5460

55-
async function getBlacklistedUsernames (accountManager) {
56-
const blacklistedUsernames = []
57-
await Promise.all(blacklistService.list.map(async (word) => {
58-
const accountExists = await accountManager.accountExists(word)
59-
if (accountExists) {
60-
blacklistedUsernames.push(word)
61-
}
62-
}))
63-
return blacklistedUsernames
64-
}
61+
// async function getBlacklistedUsernames (accountManager) {
62+
// const blacklistedUsernames = []
63+
// await Promise.all(blacklistService.list.map(async (word) => {
64+
// const accountExists = await accountManager.accountExists(word)
65+
// if (accountExists) {
66+
// blacklistedUsernames.push(word)
67+
// }
68+
// }))
69+
// return blacklistedUsernames
70+
// }

lib/common/user-utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports.isValidUsername = isValidUsername
2+
3+
function isValidUsername (username) {
4+
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(username)
5+
}

lib/requests/create-account-request.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const AuthRequest = require('./auth-request')
44
const WebIdTlsCertificate = require('../models/webid-tls-certificate')
55
const debug = require('../debug').accounts
66
const blacklistService = require('../services/blacklist-service')
7+
const { isValidUsername } = require('../common/user-utils')
78

89
/**
910
* Represents a 'create new user account' http request (either a POST to the
@@ -208,7 +209,7 @@ class CreateAccountRequest extends AuthRequest {
208209
* @return {UserAccount} Chainable
209210
*/
210211
cancelIfUsernameInvalid (userAccount) {
211-
if (!userAccount.username || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(userAccount.username)) {
212+
if (!userAccount.username || !isValidUsername(userAccount.username)) {
212213
debug('Invalid username ' + userAccount.username)
213214
const error = new Error('Invalid username (contains invalid characters)')
214215
error.status = 400

test/unit/user-utils-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const chai = require('chai')
2+
const expect = chai.expect
3+
const userUtils = require('../../lib/common/user-utils')
4+
5+
describe('user-utils', () => {
6+
describe('isValidUsername', () => {
7+
it('should accect valid usernames', () => {
8+
const usernames = [
9+
'foo',
10+
'bar'
11+
]
12+
const validUsernames = usernames.filter(username => userUtils.isValidUsername(username))
13+
expect(validUsernames.length).to.equal(usernames.length)
14+
})
15+
16+
it('should not accect invalid usernames', () => {
17+
const usernames = [
18+
'-',
19+
'-a',
20+
'a-',
21+
'9-',
22+
'alice--bob',
23+
'alice bob',
24+
'alice.bob'
25+
]
26+
const validUsernames = usernames.filter(username => userUtils.isValidUsername(username))
27+
expect(validUsernames.length).to.equal(0)
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)