| 
									
										
										
										
											2023-06-01 15:32:28 +08:00
										 |  |  | package svc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2023-06-08 11:03:20 +08:00
										 |  |  | 	"fusenapi/server/product/internal/config" | 
					
						
							| 
									
										
										
										
											2023-08-09 16:54:52 +08:00
										 |  |  | 	"fusenapi/shared" | 
					
						
							| 
									
										
										
										
											2023-06-19 10:33:51 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 14:12:50 +08:00
										 |  |  | 	"fusenapi/initalize" | 
					
						
							|  |  |  | 	"fusenapi/model/gmodel" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	"github.com/golang-jwt/jwt" | 
					
						
							| 
									
										
										
										
											2023-06-12 14:05:35 +08:00
										 |  |  | 	"gorm.io/gorm" | 
					
						
							| 
									
										
										
										
											2023-06-01 15:32:28 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ServiceContext struct { | 
					
						
							| 
									
										
										
										
											2023-07-25 19:32:51 +08:00
										 |  |  | 	Config      config.Config | 
					
						
							| 
									
										
										
										
											2023-08-09 16:54:52 +08:00
										 |  |  | 	SharedState *shared.SharedState | 
					
						
							| 
									
										
										
										
											2023-06-12 14:05:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	MysqlConn *gorm.DB | 
					
						
							| 
									
										
										
										
											2023-06-21 14:12:50 +08:00
										 |  |  | 	AllModels *gmodel.AllModelsGen | 
					
						
							| 
									
										
										
										
											2023-06-01 15:32:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewServiceContext(c config.Config) *ServiceContext { | 
					
						
							|  |  |  | 	return &ServiceContext{ | 
					
						
							| 
									
										
										
										
											2023-06-01 18:34:41 +08:00
										 |  |  | 		Config:    c, | 
					
						
							| 
									
										
										
										
											2023-06-19 10:33:51 +08:00
										 |  |  | 		MysqlConn: initalize.InitMysql(c.SourceMysql), | 
					
						
							| 
									
										
										
										
											2023-06-21 14:12:50 +08:00
										 |  |  | 		AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), | 
					
						
							| 
									
										
										
										
											2023-06-01 15:32:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 19:36:28 +08:00
										 |  |  | func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	AuthKey := r.Header.Get("Authorization") | 
					
						
							| 
									
										
										
										
											2023-06-25 11:31:37 +08:00
										 |  |  | 	if AuthKey == "" { | 
					
						
							|  |  |  | 		return nil, nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-10 17:54:10 +08:00
										 |  |  | 	AuthKey = AuthKey[7:] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	if len(AuthKey) <= 50 { | 
					
						
							|  |  |  | 		return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) { | 
					
						
							|  |  |  | 		// 检查签名方法是否为 HS256 | 
					
						
							|  |  |  | 		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// 返回用于验证签名的密钥 | 
					
						
							| 
									
										
										
										
											2023-06-21 14:12:50 +08:00
										 |  |  | 		return []byte(svcCtx.Config.Auth.AccessSecret), nil | 
					
						
							| 
									
										
										
										
											2023-06-12 16:47:48 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, errors.New(fmt.Sprint("Error parsing token:", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 验证成功返回 | 
					
						
							|  |  |  | 	if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | 
					
						
							|  |  |  | 		return claims, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, errors.New(fmt.Sprint("Invalid token", err)) | 
					
						
							|  |  |  | } |