40 lines
		
	
	
		
			946 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			946 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package token
 | |
| 
 | |
| 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")
 | |
| )
 | |
| 
 | |
| // Payload contains the payload data of the token
 | |
| type Payload struct {
 | |
| 	ID        string    `json:"id"`
 | |
| 	Username  string    `json:"username"`
 | |
| 	IssuedAt  time.Time `json:"issued_at"`
 | |
| 	ExpiredAt time.Time `json:"expired_at"`
 | |
| }
 | |
| 
 | |
| // NewPayload creates a new token payload with a specific username and duration
 | |
| func NewPayload(id string, username string, duration time.Duration) (*Payload, error) {
 | |
| 	payload := &Payload{
 | |
| 		ID:        id,
 | |
| 		Username:  username,
 | |
| 		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
 | |
| }
 |