/*
* This file is part of sdbapi
* Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
package auth
import (
"errors"
"os"
"time"
"github.com/golang-jwt/jwt/v4"
)
var JWTSecret = []byte(os.Getenv("SDBAPI_JWT_SECRET"))
type JWTClaims struct {
Username string `json:"username"`
Email string `json:"email"`
jwt.RegisteredClaims
}
func NewJWTToken(username string, email string) (token string, err error) {
claims := JWTClaims{
Username: username,
Email: email,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
},
}
jwtClaims := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
token, err = jwtClaims.SignedString(JWTSecret)
return
}
func ValidateJWTToken(userToken string) (err error) {
token, err := jwt.ParseWithClaims(
userToken,
&JWTClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(JWTSecret), nil
},
)
if err != nil {
return
}
claims, ok := token.Claims.(*JWTClaims)
if !ok {
err = errors.New("could not parse JWT claims")
return
}
if claims.ExpiresAt.Unix() < time.Now().Local().Unix() {
err = errors.New("token is expired")
return
}
return
}