-
Notifications
You must be signed in to change notification settings - Fork 0
Create userservice class #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import hashlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import hmac | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import secrets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional, Protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class User: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, username: str, password_hash: str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.username = username | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.password_hash = password_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+8
to
+11
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The User class only contains username and password_hash, lacking important user metadata like creation date, last login, account status (active/locked), etc. Consider enhancing the User model with these attributes.
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class UserDB(Protocol): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_user(self, username: str) -> Optional[User]: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def save_user(self, user: User) -> None: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
+15
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def delete_user(self, username: str) -> None: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class UserService: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, db: UserDB): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.db = db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def register(self, username: str, password: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+18
to
+23
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The UserService class doesn't implement any rate limiting for login attempts. This makes it vulnerable to brute force attacks. Consider implementing rate limiting for failed login attempts.
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.db.get_user(username): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("Username already taken") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hashed = self._hash_password(password) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.db.save_user(User(username, hashed)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.logger.info("Registered user: %s", username) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+28
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sensitive operations like user registration and deletion are only logged but don't require additional verification or authorization. Consider implementing a more robust authorization mechanism for these operations.
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def login(self, username: str, password: str) -> Optional[str]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user = self.db.get_user(username) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not user: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._verify_password(password, user.password_hash): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token = self._generate_token(username) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+37
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def delete_user(self, username: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.db.delete_user(username) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.logger.info("Deleted user: %s", username) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _hash_password(self, password: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| salt = secrets.token_bytes(16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| digest = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100_000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return salt.hex() + ":" + digest.hex() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+46
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The password hashing iteration count is hardcoded as 100_000. As per the repository context, this is a critical security parameter that should be configurable and should not be decreased in future updates. Consider making this configurable but with a secure default.
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _verify_password(self, password: str, stored: str) -> bool: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| salt_hex, hash_hex = stored.split(":") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| salt = bytes.fromhex(salt_hex) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected = bytes.fromhex(hash_hex) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| actual = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100_000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return hmac.compare_digest(actual, expected) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _generate_token(self, username: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raw = f"{username}:{time.time()}:{secrets.token_hex(16)}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rohitvinnakota-codecov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return hashlib.sha256(raw.encode()).hexdigest() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+57
to
+60
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Did we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.