| 
									
										
										
										
											2023-07-24 17:22:06 +08:00
										 |  |  | package svc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"fusenapi/server/auth/internal/config" | 
					
						
							| 
									
										
										
										
											2023-08-09 16:54:52 +08:00
										 |  |  | 	"fusenapi/shared" | 
					
						
							| 
									
										
										
										
											2023-07-27 10:18:49 +08:00
										 |  |  | 	"fusenapi/utils/auth" | 
					
						
							| 
									
										
										
										
											2023-07-24 17:22:06 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"fusenapi/initalize" | 
					
						
							|  |  |  | 	"fusenapi/model/gmodel" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/golang-jwt/jwt" | 
					
						
							|  |  |  | 	"gorm.io/gorm" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-07-24 17:22:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	MysqlConn *gorm.DB | 
					
						
							|  |  |  | 	AllModels *gmodel.AllModelsGen | 
					
						
							| 
									
										
										
										
											2023-07-27 10:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 14:19:47 +08:00
										 |  |  | 	OAuthTokenManger *auth.ConfirmationLink[auth.RegisterToken] | 
					
						
							|  |  |  | 	ResetTokenManger *auth.ConfirmationLink[auth.ResetToken] | 
					
						
							| 
									
										
										
										
											2023-07-24 17:22:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewServiceContext(c config.Config) *ServiceContext { | 
					
						
							| 
									
										
										
										
											2023-07-25 19:32:51 +08:00
										 |  |  | 	conn := initalize.InitMysql(c.SourceMysql) | 
					
						
							| 
									
										
										
										
											2023-08-10 12:04:36 +08:00
										 |  |  | 	// StateServer := shared.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) | 
					
						
							| 
									
										
										
										
											2023-08-29 10:34:10 +08:00
										 |  |  | 	registerAddress := fmt.Sprintf("%s/api/auth/email/confirmation", c.MainAddress) | 
					
						
							| 
									
										
										
										
											2023-07-24 17:22:06 +08:00
										 |  |  | 	return &ServiceContext{ | 
					
						
							| 
									
										
										
										
											2023-08-29 14:19:47 +08:00
										 |  |  | 		Config:           c, | 
					
						
							|  |  |  | 		MysqlConn:        conn, | 
					
						
							|  |  |  | 		SharedState:      nil, | 
					
						
							|  |  |  | 		AllModels:        gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), | 
					
						
							|  |  |  | 		OAuthTokenManger: auth.NewConfirmationLink[auth.RegisterToken](c.Auth.AccessSecret, registerAddress), | 
					
						
							|  |  |  | 		ResetTokenManger: auth.NewConfirmationLink[auth.ResetToken](c.Auth.AccessSecret, registerAddress), | 
					
						
							| 
									
										
										
										
											2023-07-24 17:22:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { | 
					
						
							|  |  |  | 	AuthKey := r.Header.Get("Authorization") | 
					
						
							|  |  |  | 	if AuthKey == "" { | 
					
						
							|  |  |  | 		return nil, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	AuthKey = AuthKey[7:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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"]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// 返回用于验证签名的密钥 | 
					
						
							|  |  |  | 		return []byte(svcCtx.Config.Auth.AccessSecret), nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	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)) | 
					
						
							|  |  |  | } |