2022-11-29 11:43:01 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
2023-10-29 17:03:55 +00:00
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/google/uuid"
|
2022-11-29 11:43:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Different types of error returned by the VerifyToken function
|
|
|
|
var (
|
|
|
|
ErrInvalidToken = errors.New("token is invalid")
|
|
|
|
ErrExpiredToken = errors.New("token has expired")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Valid checks if the token payload is valid or not
|
2023-10-21 15:17:05 +00:00
|
|
|
func (payload *Payload) Valid() (err error) {
|
|
|
|
now := time.Now()
|
|
|
|
if payload.RegisteredClaims.ExpiresAt.Time.Before(now) {
|
|
|
|
err = errors.New("Token has expired.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if payload.RegisteredClaims.NotBefore.Time.After(now) {
|
|
|
|
err = errors.New("Token has not begun.")
|
|
|
|
return
|
2022-11-29 11:43:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-29 17:03:55 +00:00
|
|
|
|
|
|
|
type Payload struct {
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
AuthToken string `json:"authtoken"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type JWTMaker struct {
|
|
|
|
secretKey string
|
|
|
|
}
|