| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | # -*- coding:utf-8 -*- | 
					
						
							|  |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | from controllers.web import api | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from flask import request | 
					
						
							|  |  |  | from flask_restful import Resource | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | from libs.passport import PassportService | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from models.model import App, EndUser, Site | 
					
						
							|  |  |  | from werkzeug.exceptions import NotFound, Unauthorized | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class PassportResource(Resource): | 
					
						
							|  |  |  |     """Base resource for passport.""" | 
					
						
							|  |  |  |     def get(self): | 
					
						
							| 
									
										
										
										
											2023-08-13 17:32:23 +08:00
										 |  |  |         app_code = request.headers.get('X-App-Code') | 
					
						
							|  |  |  |         if app_code is None: | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |             raise Unauthorized('X-App-Code header is missing.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # get site from db and check if it is normal | 
					
						
							|  |  |  |         site = db.session.query(Site).filter( | 
					
						
							| 
									
										
										
										
											2023-08-13 17:32:23 +08:00
										 |  |  |             Site.code == app_code, | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |             Site.status == 'normal' | 
					
						
							|  |  |  |         ).first() | 
					
						
							|  |  |  |         if not site: | 
					
						
							|  |  |  |             raise NotFound() | 
					
						
							|  |  |  |         # get app from db and check if it is normal and enable_site | 
					
						
							|  |  |  |         app_model = db.session.query(App).filter(App.id == site.app_id).first() | 
					
						
							|  |  |  |         if not app_model or app_model.status != 'normal' or not app_model.enable_site: | 
					
						
							|  |  |  |             raise NotFound() | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         end_user = EndUser( | 
					
						
							|  |  |  |             tenant_id=app_model.tenant_id, | 
					
						
							|  |  |  |             app_id=app_model.id, | 
					
						
							|  |  |  |             type='browser', | 
					
						
							|  |  |  |             is_anonymous=True, | 
					
						
							|  |  |  |             session_id=generate_session_id(), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         db.session.add(end_user) | 
					
						
							|  |  |  |         db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         payload = { | 
					
						
							|  |  |  |             "iss": site.app_id, | 
					
						
							|  |  |  |             'sub': 'Web API Passport', | 
					
						
							|  |  |  |             'app_id': site.app_id, | 
					
						
							| 
									
										
										
										
											2023-08-13 17:32:23 +08:00
										 |  |  |             'app_code': app_code, | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  |             'end_user_id': end_user.id, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         tk = PassportService().issue(payload) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'access_token': tk, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | api.add_resource(PassportResource, '/passport') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def generate_session_id(): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Generate a unique session ID. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     while True: | 
					
						
							|  |  |  |         session_id = str(uuid.uuid4()) | 
					
						
							|  |  |  |         existing_count = db.session.query(EndUser) \ | 
					
						
							|  |  |  |             .filter(EndUser.session_id == session_id).count() | 
					
						
							|  |  |  |         if existing_count == 0: | 
					
						
							|  |  |  |             return session_id |