mirror of
https://github.com/wassname/talk.git
synced 2026-08-15 12:55:10 +08:00
fix: added username config (#2295)
This commit is contained in:
+14
-5
@@ -22,6 +22,10 @@ const {
|
||||
ROOT_URL,
|
||||
RECAPTCHA_WINDOW,
|
||||
RECAPTCHA_INCORRECT_TRIGGER,
|
||||
USERNAME_CAST_REGEXP,
|
||||
USERNAME_REPLACEMENT_CAST_REGEXP,
|
||||
USERNAME_REPLACEMENT_CHARACTER,
|
||||
USERNAME_VALIDATION_REGEX,
|
||||
} = require('../config');
|
||||
const { jwt: JWT_SECRET } = require('../secrets');
|
||||
const debug = require('debug')('talk:services:users');
|
||||
@@ -525,7 +529,10 @@ class Users {
|
||||
}
|
||||
|
||||
static castUsername(username) {
|
||||
return username.replace(/ /g, '_').replace(/[^a-zA-Z_]/g, '');
|
||||
return username
|
||||
.trim()
|
||||
.replace(USERNAME_REPLACEMENT_CAST_REGEXP, USERNAME_REPLACEMENT_CHARACTER)
|
||||
.replace(USERNAME_CAST_REGEXP, '');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -554,7 +561,11 @@ class Users {
|
||||
for (let i = 0; i < MAX_ATTEMPTS; i++) {
|
||||
// Generate `GROUP_ATTEMPTS` guesses for the username.
|
||||
const usernameGuesses = Array.from(Array(GROUP_ATTEMPTS)).map(
|
||||
() => `${castedName}_${random(0, END_NUMBER_MAX)}`
|
||||
() =>
|
||||
`${castedName}${USERNAME_REPLACEMENT_CHARACTER}${random(
|
||||
0,
|
||||
END_NUMBER_MAX
|
||||
)}`
|
||||
);
|
||||
|
||||
// Map them all to lowercase.
|
||||
@@ -684,13 +695,11 @@ class Users {
|
||||
* @return {Promise}
|
||||
*/
|
||||
static async isValidUsername(username, checkAgainstWordlist = true) {
|
||||
const onlyLettersNumbersUnderscore = /^[A-Za-z0-9_]+$/;
|
||||
|
||||
if (!username) {
|
||||
throw new ErrMissingUsername();
|
||||
}
|
||||
|
||||
if (!onlyLettersNumbersUnderscore.test(username)) {
|
||||
if (!USERNAME_VALIDATION_REGEX.test(username)) {
|
||||
throw new ErrSpecialChars();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user