大改jwt
This commit is contained in:
59
utils/auth/jwt_token.go
Normal file
59
utils/auth/jwt_token.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseJwtTokenHeader[T any](r *http.Request) (string, *T, error) {
|
||||
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return "", nil, nil
|
||||
}
|
||||
if len(AuthKey) <= 15 {
|
||||
return "", nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
|
||||
}
|
||||
AuthKey = AuthKey[7:]
|
||||
|
||||
parts := strings.Split(AuthKey, ".")
|
||||
if len(parts) != 3 {
|
||||
return "", nil, fmt.Errorf("Invalid JWT token")
|
||||
}
|
||||
|
||||
payload, err := base64.URLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("Error unmarshalling JWT DecodeString: %s", err.Error())
|
||||
}
|
||||
|
||||
var p T
|
||||
err = json.Unmarshal(payload, &p)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("Error unmarshalling JWT payload: %s", err)
|
||||
}
|
||||
|
||||
return AuthKey, &p, nil
|
||||
|
||||
// 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))
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
func ParseJwtTokenUint64Secret(r *http.Request, AccessSecret uint64) (jwt.MapClaims, error) {
|
||||
func ParseJwtTokenUint64SecretByRequest(r *http.Request, AccessSecret uint64) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
@@ -46,6 +46,32 @@ func ParseJwtTokenUint64Secret(r *http.Request, AccessSecret uint64) (jwt.MapCla
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
||||
|
||||
func ParseJwtTokenUint64Secret(AuthKey string, AccessSecret uint64) (jwt.MapClaims, error) {
|
||||
|
||||
// Convert uint64 to []byte
|
||||
key := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(key, AccessSecret)
|
||||
|
||||
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 key, 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))
|
||||
}
|
||||
|
||||
// ValidateEmail checks if the provided string is a valid email address.
|
||||
func ValidateEmail(email string) bool {
|
||||
_, err := mail.ParseAddress(email)
|
||||
|
||||
Reference in New Issue
Block a user