forked from React-Group/interstellar_ai
		
	Backend #5
					 2 changed files with 29 additions and 13 deletions
				
			
		
							
								
								
									
										10
									
								
								py/api.py
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								py/api.py
									
										
									
									
									
								
							|  | @ -41,9 +41,15 @@ class API: | ||||||
|         @self.app.route('/interstellar/api/db', methods=['POST']) |         @self.app.route('/interstellar/api/db', methods=['POST']) | ||||||
|         def db_manipulate(): |         def db_manipulate(): | ||||||
|             action = request.args.get('action') |             action = request.args.get('action') | ||||||
|  |             data = request.args.get('data') | ||||||
|             if action == "create_account": |             if action == "create_account": | ||||||
|                 print("ahh") |                 self.db.add_user(data) | ||||||
| 
 |             if action == "change_password": | ||||||
|  |                 self.db.update_password(data) | ||||||
|  |             if action == "get_data": | ||||||
|  |                 self.db.get_additional_info(data) | ||||||
|  |             if action == "check_credentials": | ||||||
|  |                 self.db.check_credentials(data) | ||||||
| 
 | 
 | ||||||
|         ssl_context = ('cert.pem', 'key.pem') |         ssl_context = ('cert.pem', 'key.pem') | ||||||
|         self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context) |         self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context) | ||||||
|  |  | ||||||
							
								
								
									
										30
									
								
								py/db.py
									
										
									
									
									
								
							
							
						
						
									
										30
									
								
								py/db.py
									
										
									
									
									
								
							|  | @ -6,34 +6,44 @@ class DB: | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         self.database = {} |         self.database = {} | ||||||
| 
 | 
 | ||||||
|     def _hash_password(self, password: str) -> str: |     @staticmethod | ||||||
|  |     def hash_password(password): | ||||||
|         salt = "your_secret_salt" |         salt = "your_secret_salt" | ||||||
|         hashed_password = hashlib.sha256((password + salt).encode()).hexdigest() |         hashed_password = hashlib.sha256((password + salt).encode()).hexdigest() | ||||||
|         return hashed_password |         return hashed_password | ||||||
| 
 | 
 | ||||||
|     def add_user(self, username: str, password: str) -> None: |     def add_user(self, data): | ||||||
|         hashed_password = self._hash_password(password) |         username = data.get['username'] | ||||||
|  |         password = data.get['password'] | ||||||
|  |         hashed_password = self.hash_password(password) | ||||||
|         user_data = {"hashed_password": hashed_password} |         user_data = {"hashed_password": hashed_password} | ||||||
|         self.database[username] = user_data |         self.database[username] = user_data | ||||||
| 
 | 
 | ||||||
|     def update_password(self, username: str, old_password: str, new_password: str) -> bool: |     def update_password(self, data): | ||||||
|         if not self.check_credentials(username, old_password): |         username = data.get['username'] | ||||||
|  |         old_password = data.get['old_password'] | ||||||
|  |         new_password = data.get['new_password'] | ||||||
|  |         if not self.check_credentials(data): | ||||||
|             return False |             return False | ||||||
| 
 | 
 | ||||||
|         hashed_new_password = self._hash_password(new_password) |         hashed_new_password = self.hash_password(new_password) | ||||||
|         self.database[username].update({"hashed_password": hashed_new_password}) |         self.database[username].update({"hashed_password": hashed_new_password}) | ||||||
|         return True |         return True | ||||||
| 
 | 
 | ||||||
|     def check_credentials(self, username: str, password: str) -> bool: |     def check_credentials(self, data): | ||||||
|  |         username = data.get['username'] | ||||||
|  |         password = data.get['password'] | ||||||
|         if username not in self.database: |         if username not in self.database: | ||||||
|             return False |             return False | ||||||
| 
 | 
 | ||||||
|         stored_hashed_password = self.database[username]["hashed_password"] |         stored_hashed_password = self.database[username]["hashed_password"] | ||||||
|         entered_hashed_password = self._hash_password(password) |         entered_hashed_password = self.hash_password(password) | ||||||
|         return stored_hashed_password == entered_hashed_password |         return stored_hashed_password == entered_hashed_password | ||||||
| 
 | 
 | ||||||
|     def get_additional_info(self, username: str, password: str) -> dict | None: |     def get_additional_info(self, data): | ||||||
|         if not self.check_credentials(username, password): |         username = data.get['username'] | ||||||
|  |         password = data.get['password'] | ||||||
|  |         if not self.check_credentials(data): | ||||||
|             return None |             return None | ||||||
| 
 | 
 | ||||||
|         send_back = self.database[username] |         send_back = self.database[username] | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue