| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | import jwt | 
					
						
							|  |  |  | from flask import current_app | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from werkzeug.exceptions import Unauthorized | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 15:21:20 +08:00
										 |  |  | class PassportService: | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.sk = current_app.config.get('SECRET_KEY') | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     def issue(self, payload): | 
					
						
							|  |  |  |         return jwt.encode(payload, self.sk, algorithm='HS256') | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     def verify(self, token): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return jwt.decode(token, self.sk, algorithms=['HS256']) | 
					
						
							|  |  |  |         except jwt.exceptions.InvalidSignatureError: | 
					
						
							|  |  |  |             raise Unauthorized('Invalid token signature.') | 
					
						
							|  |  |  |         except jwt.exceptions.DecodeError: | 
					
						
							|  |  |  |             raise Unauthorized('Invalid token.') | 
					
						
							|  |  |  |         except jwt.exceptions.ExpiredSignatureError: | 
					
						
							|  |  |  |             raise Unauthorized('Token has expired.') |