Implemented user login and authentication
Browse files
postly/clients/postly_client.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import re
|
|
|
|
| 2 |
from collections import Counter
|
| 3 |
from typing import List
|
| 4 |
|
|
@@ -15,6 +16,7 @@ class PostlyClient:
|
|
| 15 |
|
| 16 |
def __init__(self) -> None:
|
| 17 |
self.userPosts = {}
|
|
|
|
| 18 |
self.post_max_length = 140
|
| 19 |
self.timestamp_iter = 0
|
| 20 |
|
|
@@ -30,16 +32,48 @@ class PostlyClient:
|
|
| 30 |
"""
|
| 31 |
return re.findall(pattern=r"#(\w+)", string=post)
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
Add new user to system.
|
| 36 |
|
| 37 |
Args:
|
| 38 |
user_name: The name of the user to add.
|
|
|
|
| 39 |
Returns:
|
| 40 |
None
|
| 41 |
"""
|
|
|
|
|
|
|
|
|
|
| 42 |
self.userPosts[user_name] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def add_post(self, user_name: str, post_text: str) -> None:
|
| 45 |
"""
|
|
@@ -78,6 +112,7 @@ class PostlyClient:
|
|
| 78 |
raise KeyError(f"User '{user_name}' not found.")
|
| 79 |
|
| 80 |
self.userPosts.pop(user_name, None)
|
|
|
|
| 81 |
|
| 82 |
def get_users(self):
|
| 83 |
"""
|
|
@@ -177,5 +212,6 @@ class PostlyClient:
|
|
| 177 |
if from_timestamp <= post_data.timestamp <= to_timestamp:
|
| 178 |
topics_frequency.update(post_data.topics)
|
| 179 |
|
| 180 |
-
#
|
| 181 |
return [topic for topic, _ in topics_frequency.most_common()]
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
import hashlib
|
| 3 |
from collections import Counter
|
| 4 |
from typing import List
|
| 5 |
|
|
|
|
| 16 |
|
| 17 |
def __init__(self) -> None:
|
| 18 |
self.userPosts = {}
|
| 19 |
+
self.user_passwords = {}
|
| 20 |
self.post_max_length = 140
|
| 21 |
self.timestamp_iter = 0
|
| 22 |
|
|
|
|
| 32 |
"""
|
| 33 |
return re.findall(pattern=r"#(\w+)", string=post)
|
| 34 |
|
| 35 |
+
@staticmethod
|
| 36 |
+
def hash_password(password: str) -> str:
|
| 37 |
+
"""
|
| 38 |
+
Hash a password using SHA-256.
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
password: The password to hash.
|
| 42 |
+
Returns:
|
| 43 |
+
The hashed password.
|
| 44 |
+
"""
|
| 45 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 46 |
+
|
| 47 |
+
def add_user(self, user_name: str, password: str) -> None:
|
| 48 |
"""
|
| 49 |
Add new user to system.
|
| 50 |
|
| 51 |
Args:
|
| 52 |
user_name: The name of the user to add.
|
| 53 |
+
password: The password of the user.
|
| 54 |
Returns:
|
| 55 |
None
|
| 56 |
"""
|
| 57 |
+
if user_name in self.userPosts:
|
| 58 |
+
raise ValueError(f"User '{user_name}' already exists.")
|
| 59 |
+
|
| 60 |
self.userPosts[user_name] = []
|
| 61 |
+
self.user_passwords[user_name] = self.hash_password(password)
|
| 62 |
+
|
| 63 |
+
def authenticate_user(self, user_name: str, password: str) -> bool:
|
| 64 |
+
"""
|
| 65 |
+
Authenticate a user.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
user_name: The name of the user.
|
| 69 |
+
password: The password of the user.
|
| 70 |
+
Returns:
|
| 71 |
+
True if authentication is successful, False otherwise.
|
| 72 |
+
"""
|
| 73 |
+
if user_name in self.user_passwords:
|
| 74 |
+
hashed_password = self.hash_password(password)
|
| 75 |
+
return self.user_passwords[user_name] == hashed_password
|
| 76 |
+
return False
|
| 77 |
|
| 78 |
def add_post(self, user_name: str, post_text: str) -> None:
|
| 79 |
"""
|
|
|
|
| 112 |
raise KeyError(f"User '{user_name}' not found.")
|
| 113 |
|
| 114 |
self.userPosts.pop(user_name, None)
|
| 115 |
+
self.user_passwords.pop(user_name, None)
|
| 116 |
|
| 117 |
def get_users(self):
|
| 118 |
"""
|
|
|
|
| 212 |
if from_timestamp <= post_data.timestamp <= to_timestamp:
|
| 213 |
topics_frequency.update(post_data.topics)
|
| 214 |
|
| 215 |
+
# retrieve top topics in descending order
|
| 216 |
return [topic for topic, _ in topics_frequency.most_common()]
|
| 217 |
+
|