55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
)
|
|
|
|
type JWTClaim struct {
|
|
Id uint64 `json:"id"`
|
|
Username string `json:"username"`
|
|
Email string `json:"email"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GenerateJWT(id uint64, email string, username string) (tokenString string, err error) {
|
|
expirationTime := time.Now().Add(24 * time.Hour)
|
|
claims := &JWTClaim{
|
|
Id: id,
|
|
Email: email,
|
|
Username: username,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(expirationTime),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
tokenString, err = token.SignedString([]byte(os.Getenv("JWT_SECRET")))
|
|
return
|
|
}
|
|
|
|
func ValidateToken(signedToken string) (claims *JWTClaim, err error) {
|
|
token, err := jwt.ParseWithClaims(
|
|
signedToken,
|
|
&JWTClaim{},
|
|
func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(os.Getenv("JWT_SECRET")), nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
claims, ok := token.Claims.(*JWTClaim)
|
|
if !ok {
|
|
err = errors.New("couldn't parse claims")
|
|
return
|
|
}
|
|
if claims.ExpiresAt.Unix() < jwt.NewNumericDate(time.Now().Local()).Unix() {
|
|
err = errors.New("token expired")
|
|
return
|
|
}
|
|
return
|
|
}
|