fusenapi/server/home-user-auth/internal/svc/servicecontext.go

78 lines
2.2 KiB
Go
Raw Normal View History

2023-05-31 10:33:02 +00:00
package svc
import (
2023-06-15 08:08:43 +00:00
"fusenapi/initalize"
2023-06-21 03:29:09 +00:00
"fusenapi/model/gmodel"
2023-08-09 08:54:52 +00:00
"fusenapi/shared"
2023-06-08 02:51:56 +00:00
2023-09-07 08:25:29 +00:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
2023-06-12 07:17:42 +00:00
"github.com/golang-jwt/jwt"
2023-06-15 08:08:43 +00:00
"gorm.io/gorm"
2023-06-21 04:21:56 +00:00
"errors"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"net/http"
2023-05-31 10:33:02 +00:00
)
type ServiceContext struct {
2023-07-25 11:32:51 +00:00
Config config.Config
2023-08-09 08:54:52 +00:00
SharedState *shared.SharedState
2023-06-21 04:21:56 +00:00
2023-09-07 08:25:29 +00:00
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
Repositories *initalize.Repositories
2023-05-31 10:33:02 +00:00
}
func NewServiceContext(c config.Config) *ServiceContext {
2023-07-25 11:32:51 +00:00
conn := initalize.InitMysql(c.SourceMysql)
2023-08-10 04:04:36 +00:00
// StateServer := shared.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn)
2023-09-07 08:25:29 +00:00
config := aws.Config{
Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token),
}
2023-05-31 10:33:02 +00:00
return &ServiceContext{
2023-07-25 11:32:51 +00:00
Config: c,
MysqlConn: conn,
2023-08-10 04:04:36 +00:00
SharedState: nil,
2023-07-25 11:32:51 +00:00
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
2023-09-07 08:25:29 +00:00
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
2023-10-10 09:17:28 +00:00
GormDB: initalize.InitMysql(c.SourceMysql),
BLMServiceUrls: c.BLMService.Urls,
AwsSession: session.Must(session.NewSession(&config)),
2023-09-07 08:25:29 +00:00
}),
2023-05-31 10:33:02 +00:00
}
}
2023-06-12 07:17:42 +00:00
2023-06-20 11:36:28 +00:00
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
2023-06-12 07:17:42 +00:00
AuthKey := r.Header.Get("Authorization")
if AuthKey == "" {
return nil, nil
}
2023-07-10 09:54:10 +00:00
AuthKey = AuthKey[7:]
2023-06-21 04:21:56 +00:00
if len(AuthKey) <= 50 {
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
2023-06-12 07:17:42 +00:00
}
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
2023-06-12 07:17:42 +00: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))
}