portal/account/jwt.go

31 lines
768 B
Go
Raw Normal View History

package account
import (
"crypto/ed25519"
"github.com/golang-jwt/jwt/v5"
"time"
)
2024-02-14 00:07:24 +00:00
func GenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint) (string, error) {
return GenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24)
}
2024-02-14 00:07:24 +00:00
func GenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration) (string, error) {
// Define the claims
claims := jwt.MapClaims{
2024-02-14 00:07:24 +00:00
"iss": domain,
"sub": userID,
"exp": time.Now().Add(duration).Unix(),
}
// Create the token
token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims)
// Sign the token with the Ed25519 private key
tokenString, err := token.SignedString(privateKey)
if err != nil {
return "", err
}
return tokenString, nil
}