package main import ( "errors" "time" "github.com/google/uuid" ) // Different types of error returned by the VerifyToken function var ( ErrInvalidToken = errors.New("token is invalid") ErrExpiredToken = errors.New("token has expired") ) // NewPayload creates a new token payload with a specific username and duration func NewPayload(username string, admin bool, authtoken string, duration time.Duration) (*Payload, error) { tokenID, err := uuid.NewRandom() if err != nil { return nil, err } payload := &Payload{ ID: tokenID, Username: username, Admin: admin, AuthToken: authtoken, IssuedAt: time.Now(), ExpiredAt: time.Now().Add(duration), } return payload, nil } // Valid checks if the token payload is valid or not func (payload *Payload) Valid() error { if time.Now().After(payload.ExpiredAt) { return ErrExpiredToken } return nil }