@things-factory/auth-base 4.3.581 → 4.3.591

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.signup = void 0;
3
+ exports.UsernameValidator = exports.UsernameValidationError = exports.signup = void 0;
4
4
  const typeorm_1 = require("typeorm");
5
5
  const user_1 = require("../service/user/user");
6
6
  const verification_1 = require("./verification");
@@ -8,7 +8,8 @@ const signin_1 = require("./signin");
8
8
  const auth_error_1 = require("../errors/auth-error");
9
9
  const error_code_1 = require("../constants/error-code");
10
10
  async function signup(attrs, withEmailVerification) {
11
- const { email, password, domain, context } = attrs;
11
+ const { email, password, domain, context, name } = attrs;
12
+ UsernameValidator.validate(name);
12
13
  /* check if password is following the rule */
13
14
  user_1.User.validatePasswordByRule(password, context.lng);
14
15
  const repository = (0, typeorm_1.getRepository)(user_1.User);
@@ -40,4 +41,65 @@ async function signup(attrs, withEmailVerification) {
40
41
  }
41
42
  }
42
43
  exports.signup = signup;
44
+ class UsernameValidationError extends Error {
45
+ constructor(message) {
46
+ super(message);
47
+ this.name = 'UsernameValidationError';
48
+ }
49
+ }
50
+ exports.UsernameValidationError = UsernameValidationError;
51
+ class UsernameValidator {
52
+ static validate(username) {
53
+ // Check for empty or whitespace
54
+ if (!username || username.trim() === '') {
55
+ throw new UsernameValidationError('Invalid username.');
56
+ }
57
+ // Check length
58
+ if (username.length < this.MIN_LENGTH || username.length > this.MAX_LENGTH) {
59
+ throw new UsernameValidationError('Invalid username.');
60
+ }
61
+ // Check character set
62
+ if (!this.USERNAME_REGEX.test(username)) {
63
+ throw new UsernameValidationError('Invalid username.');
64
+ }
65
+ // Check word length
66
+ if (username.split('-').some(word => word.length > this.MAX_WORD_LENGTH)) {
67
+ throw new UsernameValidationError('Invalid username.');
68
+ }
69
+ // Check reserved words
70
+ if (this.RESERVED_USERNAMES.includes(username.toLowerCase())) {
71
+ throw new UsernameValidationError('Invalid username.');
72
+ }
73
+ // Check bad words
74
+ if (this.BAD_WORDS.some(word => username.toLowerCase().includes(word))) {
75
+ throw new UsernameValidationError('Invalid username.');
76
+ }
77
+ // Check repetitive patterns
78
+ if (this.REPETITIVE_PATTERN.test(username)) {
79
+ throw new UsernameValidationError('Invalid username.');
80
+ }
81
+ // Check numeric sequences
82
+ if (this.NUMERIC_SEQUENCE.test(username)) {
83
+ throw new UsernameValidationError('Invalid username.');
84
+ }
85
+ // Check for links
86
+ const urlPattern = /https?:\/\/[^\s]+/;
87
+ if (urlPattern.test(username)) {
88
+ throw new UsernameValidationError('Invalid username.');
89
+ }
90
+ }
91
+ }
92
+ exports.UsernameValidator = UsernameValidator;
93
+ UsernameValidator.MIN_LENGTH = 3;
94
+ UsernameValidator.MAX_LENGTH = 50;
95
+ UsernameValidator.MAX_WORD_LENGTH = 20;
96
+ UsernameValidator.USERNAME_REGEX = /^[a-zA-Z0-9_-]+$/;
97
+ UsernameValidator.REPETITIVE_PATTERN = /(\w)\1{3,}/;
98
+ UsernameValidator.NUMERIC_SEQUENCE = /[0-9]{6,}/;
99
+ UsernameValidator.RESERVED_USERNAMES = [
100
+ 'root', 'admin', 'system', 'user', 'administrator', 'superuser'
101
+ ];
102
+ UsernameValidator.BAD_WORDS = [
103
+ 'spam', 'test', 'sex', 'free', '123', 'fuck', 'shit', 'winning', 'bet', 'game'
104
+ ];
43
105
  //# sourceMappingURL=signup.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signup.js","sourceRoot":"","sources":["../../server/controllers/signup.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,+CAA2C;AAC3C,iDAAsD;AACtD,qCAAiC;AACjC,qDAAgD;AAChD,wDAAyD;AAElD,KAAK,UAAU,MAAM,CAAC,KAAK,EAAE,qBAA+B;IACjE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAA;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E,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE;YACtE,MAAM,IAAI,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;SACxD;QAED,4BAA4B;QAC5B,IAAI,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC1C,MAAM,IAAI,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;SACxD;QAED,0BAA0B;QAC1B,IAAI,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YACxC,MAAM,IAAI,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;SACxD;QAED,kBAAkB;QAClB,MAAM,UAAU,GAAG,mBAAmB,CAAC;QACvC,IAAI,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC7B,MAAM,IAAI,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;SACxD;IACH,CAAC;;AA9DH,8CA+DC;AA9DyB,4BAAU,GAAG,CAAC,CAAC;AACf,4BAAU,GAAG,EAAE,CAAC;AAChB,iCAAe,GAAG,EAAE,CAAC;AACrB,gCAAc,GAAG,kBAAkB,CAAC;AACpC,oCAAkB,GAAG,YAAY,CAAC;AAClC,kCAAgB,GAAG,WAAW,CAAC;AAE/B,oCAAkB,GAAG;IAC3C,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,WAAW;CAChE,CAAC;AAEsB,2BAAS,GAAG;IAClC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM;CAC/E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/auth-base",
3
- "version": "4.3.581",
3
+ "version": "4.3.591",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -26,11 +26,11 @@
26
26
  "migration:create": "node ../../node_modules/typeorm/cli.js migration:create -d ./server/migrations"
27
27
  },
28
28
  "dependencies": {
29
- "@things-factory/email-base": "^4.3.535",
30
- "@things-factory/env": "^4.3.534",
31
- "@things-factory/i18n-base": "^4.3.535",
32
- "@things-factory/shell": "^4.3.535",
33
- "@things-factory/utils": "^4.3.534",
29
+ "@things-factory/email-base": "^4.3.591",
30
+ "@things-factory/env": "^4.3.591",
31
+ "@things-factory/i18n-base": "^4.3.591",
32
+ "@things-factory/shell": "^4.3.591",
33
+ "@things-factory/utils": "^4.3.591",
34
34
  "jsonwebtoken": "^8.5.1",
35
35
  "koa-passport": "^4.1.4",
36
36
  "koa-session": "^6.0.0",
@@ -40,5 +40,5 @@
40
40
  "passport-local": "^1.0.0",
41
41
  "uuid": "^3.4.0"
42
42
  },
43
- "gitHead": "6e051a996f46ff64a22b0237b746c16a1629409e"
43
+ "gitHead": "b8528f316a234ad95d0f285d5ade988b44f1195f"
44
44
  }
@@ -6,7 +6,9 @@ import { AuthError } from '../errors/auth-error'
6
6
  import { USER_DUPLICATED } from '../constants/error-code'
7
7
 
8
8
  export async function signup(attrs, withEmailVerification?: Boolean) {
9
- const { email, password, domain, context } = attrs
9
+ const { email, password, domain, context, name } = attrs
10
+
11
+ UsernameValidator.validate(name)
10
12
 
11
13
  /* check if password is following the rule */
12
14
  User.validatePasswordByRule(password, context.lng)
@@ -52,3 +54,76 @@ export async function signup(attrs, withEmailVerification?: Boolean) {
52
54
  return { token: null }
53
55
  }
54
56
  }
57
+
58
+
59
+ export class UsernameValidationError extends Error {
60
+ constructor(message: string) {
61
+ super(message);
62
+ this.name = 'UsernameValidationError';
63
+ }
64
+ }
65
+
66
+ export class UsernameValidator {
67
+ private static readonly MIN_LENGTH = 3;
68
+ private static readonly MAX_LENGTH = 50;
69
+ private static readonly MAX_WORD_LENGTH = 20;
70
+ private static readonly USERNAME_REGEX = /^[a-zA-Z0-9_-]+$/;
71
+ private static readonly REPETITIVE_PATTERN = /(\w)\1{3,}/;
72
+ private static readonly NUMERIC_SEQUENCE = /[0-9]{6,}/;
73
+
74
+ private static readonly RESERVED_USERNAMES = [
75
+ 'root', 'admin', 'system', 'user', 'administrator', 'superuser'
76
+ ];
77
+
78
+ private static readonly BAD_WORDS = [
79
+ 'spam', 'test', 'sex', 'free', '123', 'fuck', 'shit', 'winning', 'bet', 'game'
80
+ ];
81
+
82
+ static validate(username: string): void {
83
+ // Check for empty or whitespace
84
+ if (!username || username.trim() === '') {
85
+ throw new UsernameValidationError('Invalid username.');
86
+ }
87
+
88
+ // Check length
89
+ if (username.length < this.MIN_LENGTH || username.length > this.MAX_LENGTH) {
90
+ throw new UsernameValidationError('Invalid username.');
91
+ }
92
+
93
+ // Check character set
94
+ if (!this.USERNAME_REGEX.test(username)) {
95
+ throw new UsernameValidationError('Invalid username.');
96
+ }
97
+
98
+ // Check word length
99
+ if (username.split('-').some(word => word.length > this.MAX_WORD_LENGTH)) {
100
+ throw new UsernameValidationError('Invalid username.');
101
+ }
102
+
103
+ // Check reserved words
104
+ if (this.RESERVED_USERNAMES.includes(username.toLowerCase())) {
105
+ throw new UsernameValidationError('Invalid username.');
106
+ }
107
+
108
+ // Check bad words
109
+ if (this.BAD_WORDS.some(word => username.toLowerCase().includes(word))) {
110
+ throw new UsernameValidationError('Invalid username.');
111
+ }
112
+
113
+ // Check repetitive patterns
114
+ if (this.REPETITIVE_PATTERN.test(username)) {
115
+ throw new UsernameValidationError('Invalid username.');
116
+ }
117
+
118
+ // Check numeric sequences
119
+ if (this.NUMERIC_SEQUENCE.test(username)) {
120
+ throw new UsernameValidationError('Invalid username.');
121
+ }
122
+
123
+ // Check for links
124
+ const urlPattern = /https?:\/\/[^\s]+/;
125
+ if (urlPattern.test(username)) {
126
+ throw new UsernameValidationError('Invalid username.');
127
+ }
128
+ }
129
+ }