2022-11-29 11:43:01 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|