添加节点分布式

This commit is contained in:
eson
2023-07-25 19:32:51 +08:00
parent a248c6cbeb
commit 7200531c27
27 changed files with 633 additions and 58 deletions

View File

@@ -1,9 +1,51 @@
package auth
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"net/http"
"net/mail"
"github.com/golang-jwt/jwt"
)
func ParseJwtTokenUint64Secret(r *http.Request, AccessSecret uint64) (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)))
}
// 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)
@@ -17,6 +59,14 @@ func ValidatePassword(password string) bool {
return len(password) >= minPasswordLength
}
func StringToHash(s string) uint64 {
hash := sha256.New()
hash.Write([]byte(s))
hashed := hash.Sum(nil)
intHash := binary.BigEndian.Uint64(hashed)
return intHash
}
var secret = []byte("your-secret")
// func generateConfirmationLink(id, email, password, name string, platform string) (string, error) {