143 lines
3.9 KiB
Go
143 lines
3.9 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/mail"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
)
|
|
|
|
type RegisterToken struct {
|
|
OperateType // 操作的类型, 验证的token 必须要继承这个
|
|
Id int64 // 注册的 id google_id 或 facebook_id ...
|
|
GuestId int64 // guest_id 需要继承
|
|
Wid string // websocket 通道id
|
|
Email string // email
|
|
Password string // 密码
|
|
Platform string // 平台
|
|
CreateAt time.Time // 创建时间
|
|
}
|
|
|
|
type ResetToken struct {
|
|
OperateType // 操作的类型, 验证的token 必须要继承这个
|
|
UserId int64 // guest_id 需要继承
|
|
Wid string // websocket 通道id
|
|
Email string // email
|
|
OldPassword string // 旧密码
|
|
CreateAt time.Time // 创建时间
|
|
}
|
|
|
|
func ParseJwtTokenUint64SecretByRequest(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))
|
|
}
|
|
|
|
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)
|
|
return err == nil
|
|
}
|
|
|
|
// ValidatePassword checks if the provided password is strong enough.
|
|
// In this example, we just check if the password length is 8 or more.
|
|
func ValidatePassword(password string) bool {
|
|
const minPasswordLength = 8
|
|
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
|
|
}
|
|
|
|
// func handleConfirm(w http.ResponseWriter, r *http.Request) {
|
|
// // 从请求中获取 JWT。
|
|
// tokenString := r.URL.Query().Get("token")
|
|
|
|
// // 解析和验证 JWT。
|
|
// token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
// return secret, nil
|
|
// })
|
|
|
|
// if err != nil || !token.Valid {
|
|
// http.Error(w, "Invalid confirmation link", http.StatusBadRequest)
|
|
// return
|
|
// }
|
|
|
|
// claims, ok := token.Claims.(jwt.MapClaims)
|
|
|
|
// if !ok || !token.Valid {
|
|
// http.Error(w, "Invalid token", http.StatusBadRequest)
|
|
// return
|
|
// }
|
|
|
|
// email := claims["sub"].(string)
|
|
|
|
// // 确认链接有效,可以创建用户账号了。
|
|
// createUser(email)
|
|
// }
|