Move UN / PW validation to helper _assert methods.
This commit is contained in:
		
							parent
							
								
									173c07a0bf
								
							
						
					
					
						commit
						16ac6f8b85
					
				
					 1 changed files with 27 additions and 18 deletions
				
			
		|  | @ -785,6 +785,30 @@ class PDBUserMixin: | ||||||
|         super().__init__() |         super().__init__() | ||||||
|         self._user_cache = cacheclass.Cache() |         self._user_cache = cacheclass.Cache() | ||||||
| 
 | 
 | ||||||
|  |     def _assert_valid_password(self, password): | ||||||
|  |         if len(password) < self.config['user']['min_password_length']: | ||||||
|  |             raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length']) | ||||||
|  | 
 | ||||||
|  |     def _assert_valid_username(self, username): | ||||||
|  |         ''' | ||||||
|  |         If something is wrong, raise an exception. Otherwise do nothing. | ||||||
|  |         ''' | ||||||
|  |         if len(username) < self.config['user']['min_length']: | ||||||
|  |             raise exceptions.UsernameTooShort( | ||||||
|  |                 username=username, | ||||||
|  |                 min_length=self.config['user']['min_length'] | ||||||
|  |             ) | ||||||
|  | 
 | ||||||
|  |         if len(username) > self.config['user']['max_length']: | ||||||
|  |             raise exceptions.UsernameTooLong( | ||||||
|  |                 username=username, | ||||||
|  |                 max_length=self.config['user']['max_length'] | ||||||
|  |             ) | ||||||
|  | 
 | ||||||
|  |         badchars = [c for c in username if c not in self.config['user']['valid_chars']] | ||||||
|  |         if badchars: | ||||||
|  |             raise exceptions.InvalidUsernameChars(username=username, badchars=badchars) | ||||||
|  | 
 | ||||||
|     def generate_user_id(self): |     def generate_user_id(self): | ||||||
|         ''' |         ''' | ||||||
|         User IDs are randomized instead of integers like the other objects, |         User IDs are randomized instead of integers like the other objects, | ||||||
|  | @ -874,28 +898,13 @@ class PDBUserMixin: | ||||||
| 
 | 
 | ||||||
|     @decorators.required_feature('user.new') |     @decorators.required_feature('user.new') | ||||||
|     @decorators.transaction |     @decorators.transaction | ||||||
|     def register_user(self, username, password, commit=True): |     def register_user(self, username, password, *, commit=True): | ||||||
|         if len(username) < self.config['user']['min_length']: |         self._assert_valid_username(username) | ||||||
|             raise exceptions.UsernameTooShort( |  | ||||||
|                 username=username, |  | ||||||
|                 min_length=self.config['user']['min_length'] |  | ||||||
|             ) |  | ||||||
| 
 |  | ||||||
|         if len(username) > self.config['user']['max_length']: |  | ||||||
|             raise exceptions.UsernameTooLong( |  | ||||||
|                 username=username, |  | ||||||
|                 max_length=self.config['user']['max_length'] |  | ||||||
|             ) |  | ||||||
| 
 |  | ||||||
|         badchars = [c for c in username if c not in self.config['user']['valid_chars']] |  | ||||||
|         if badchars: |  | ||||||
|             raise exceptions.InvalidUsernameChars(username=username, badchars=badchars) |  | ||||||
| 
 | 
 | ||||||
|         if not isinstance(password, bytes): |         if not isinstance(password, bytes): | ||||||
|             password = password.encode('utf-8') |             password = password.encode('utf-8') | ||||||
| 
 | 
 | ||||||
|         if len(password) < self.config['user']['min_password_length']: |         self._assert_valid_password(password) | ||||||
|             raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length']) |  | ||||||
| 
 | 
 | ||||||
|         try: |         try: | ||||||
|             existing_user = self.get_user(username=username) |             existing_user = self.get_user(username=username) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue