2024-01-16 18:30:36 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
2024-02-14 03:17:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-16 18:30:36 +00:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2024-02-14 03:17:34 +00:00
|
|
|
"strconv"
|
2024-01-16 18:30:36 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-02-14 01:58:17 +00:00
|
|
|
type JWTPurpose string
|
2024-02-14 03:17:34 +00:00
|
|
|
type VerifyTokenFunc func(claim jwt.RegisteredClaims) error
|
|
|
|
|
|
|
|
var (
|
|
|
|
nopVerifyFunc VerifyTokenFunc = func(claim jwt.RegisteredClaims) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrJWTUnexpectedClaimsType = errors.New("unexpected claims type")
|
|
|
|
ErrJWTUnexpectedIssuer = errors.New("unexpected issuer")
|
|
|
|
ErrJWTInvalid = errors.New("invalid JWT")
|
|
|
|
)
|
2024-02-14 01:58:17 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
JWTPurposeLogin JWTPurpose = "login"
|
2024-02-14 03:17:34 +00:00
|
|
|
JWTPurpose2FA JWTPurpose = "2fa"
|
2024-02-14 01:58:17 +00:00
|
|
|
)
|
|
|
|
|
2024-02-14 03:31:44 +00:00
|
|
|
func JWTGenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) {
|
|
|
|
return JWTGenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24, purpose)
|
2024-01-20 12:30:46 +00:00
|
|
|
}
|
2024-02-14 01:58:17 +00:00
|
|
|
|
2024-02-14 03:31:44 +00:00
|
|
|
func JWTGenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration, purpose JWTPurpose) (string, error) {
|
2024-02-14 03:17:34 +00:00
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
// Define the claims
|
2024-02-14 03:17:34 +00:00
|
|
|
claims := jwt.RegisteredClaims{
|
|
|
|
Issuer: domain,
|
|
|
|
Subject: strconv.Itoa(int(userID)),
|
|
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(duration)),
|
|
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
|
|
Audience: []string{string(purpose)},
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2024-02-14 03:17:34 +00:00
|
|
|
|
2024-02-14 03:31:44 +00:00
|
|
|
func JWTVerifyToken(token string, domain string, privateKey ed25519.PrivateKey, verifyFunc VerifyTokenFunc) (*jwt.RegisteredClaims, error) {
|
2024-02-14 03:17:34 +00:00
|
|
|
validatedToken, err := jwt.ParseWithClaims(token, jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey := privateKey.Public()
|
|
|
|
|
|
|
|
return publicKey, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if verifyFunc == nil {
|
|
|
|
verifyFunc = nopVerifyFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
claim, ok := validatedToken.Claims.(jwt.RegisteredClaims)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrJWTUnexpectedClaimsType, validatedToken.Claims)
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain != claim.Issuer {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrJWTUnexpectedIssuer, claim.Issuer)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = verifyFunc(validatedToken.Claims.(jwt.RegisteredClaims))
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|