| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | import base64 | 
					
						
							| 
									
										
										
										
											2024-04-12 16:22:24 +08:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | import secrets | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask_restful import Resource, reqparse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from constants.languages import supported_language | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | from controllers.console import api | 
					
						
							|  |  |  | from controllers.console.error import AlreadyActivateError | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  | from libs.helper import StrLen, email, timezone | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from libs.password import hash_password, valid_password | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from models.account import AccountStatus | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | from services.account_service import RegisterService | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ActivateCheckApi(Resource): | 
					
						
							|  |  |  |     def get(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("workspace_id", type=str, required=False, nullable=True, location="args") | 
					
						
							|  |  |  |         parser.add_argument("email", type=email, required=False, nullable=True, location="args") | 
					
						
							|  |  |  |         parser.add_argument("token", type=str, required=True, nullable=False, location="args") | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         workspaceId = args["workspace_id"] | 
					
						
							|  |  |  |         reg_email = args["email"] | 
					
						
							|  |  |  |         token = args["token"] | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-07 17:15:57 +08:00
										 |  |  |         invitation = RegisterService.get_invitation_if_token_valid(workspaceId, reg_email, token) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"is_valid": invitation is not None, "workspace_name": invitation["tenant"].name if invitation else None} | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ActivateApi(Resource): | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("workspace_id", type=str, required=False, nullable=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("email", type=email, required=False, nullable=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("token", type=str, required=True, nullable=False, location="json") | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         parser.add_argument("name", type=StrLen(30), required=True, nullable=False, location="json") | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("password", type=valid_password, required=True, nullable=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument( | 
					
						
							|  |  |  |             "interface_language", type=supported_language, required=True, nullable=False, location="json" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         parser.add_argument("timezone", type=timezone, required=True, nullable=False, location="json") | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         invitation = RegisterService.get_invitation_if_token_valid(args["workspace_id"], args["email"], args["token"]) | 
					
						
							| 
									
										
										
										
											2023-09-07 17:15:57 +08:00
										 |  |  |         if invitation is None: | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |             raise AlreadyActivateError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         RegisterService.revoke_token(args["workspace_id"], args["email"], args["token"]) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         account = invitation["account"] | 
					
						
							|  |  |  |         account.name = args["name"] | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # generate password salt | 
					
						
							|  |  |  |         salt = secrets.token_bytes(16) | 
					
						
							|  |  |  |         base64_salt = base64.b64encode(salt).decode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # encrypt password with salt | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         password_hashed = hash_password(args["password"], salt) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         base64_password_hashed = base64.b64encode(password_hashed).decode() | 
					
						
							|  |  |  |         account.password = base64_password_hashed | 
					
						
							|  |  |  |         account.password_salt = base64_salt | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         account.interface_language = args["interface_language"] | 
					
						
							|  |  |  |         account.timezone = args["timezone"] | 
					
						
							|  |  |  |         account.interface_theme = "light" | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         account.status = AccountStatus.ACTIVE.value | 
					
						
							| 
									
										
										
										
											2024-04-12 16:22:24 +08:00
										 |  |  |         account.initialized_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"result": "success"} | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(ActivateCheckApi, "/activate/check") | 
					
						
							|  |  |  | api.add_resource(ActivateApi, "/activate") |