mirror of
https://github.com/aunefyren/wrapperr
synced 2024-12-13 21:02:28 +00:00
41 lines
885 B
Go
41 lines
885 B
Go
|
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
|
||
|
}
|