监控网页实现

This commit is contained in:
Z
2026-04-09 00:14:57 +08:00
parent 6b1c7242c7
commit 1cce5634b1
17 changed files with 1186 additions and 38 deletions

View File

@@ -13,26 +13,36 @@ import (
// In production, load this from environment variables
var jwtKey = []byte("hightube_super_secret_key_MVP_only")
type TokenClaims struct {
Username string `json:"username"`
Role string `json:"role"`
jwt.RegisteredClaims
}
// GenerateToken generates a JWT token for a given user ID
func GenerateToken(userID uint) (string, error) {
claims := &jwt.RegisteredClaims{
func GenerateToken(userID uint, username, role string) (string, error) {
claims := &TokenClaims{
Username: username,
Role: role,
RegisteredClaims: jwt.RegisteredClaims{
Subject: fmt.Sprintf("%d", userID),
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(jwtKey)
}
// ParseToken parses the JWT string and returns the user ID (Subject)
func ParseToken(tokenStr string) (string, error) {
claims := &jwt.RegisteredClaims{}
func ParseToken(tokenStr string) (*TokenClaims, error) {
claims := &TokenClaims{}
token, err := jwt.ParseWithClaims(tokenStr, claims, func(t *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
if err != nil || !token.Valid {
return "", err
return nil, err
}
return claims.Subject, nil
return claims, nil
}
// HashPassword creates a bcrypt hash of the password