Documentation
¶
Index ¶
Constants ¶
const ( TokenTypeJWT = "jwt" TokenTypePaseto = "paseto" )
const Author = "github.com/lordofthemind"
Author of the gophertoken package
const Description = "" /* 127-byte string literal not displayed */
Description of the gophertoken package
const Version = "1.0.1"
Version of the gophertoken package
Variables ¶
var ( ErrInvalidToken = errors.New("token validation failed: signature invalid or claims malformed") ErrExpiredToken = errors.New("token validation failed: token has expired") )
Errors related to token validation.
Functions ¶
This section is empty.
Types ¶
type JWTMaker ¶
type JWTMaker struct {
// contains filtered or unexported fields
}
JWTMaker is a struct for handling JWT token creation and validation.
func (*JWTMaker) GenerateToken ¶
func (j *JWTMaker) GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error)
GenerateToken creates a new JWT token for a specific user with a given duration.
Example usage:
token, err := maker.GenerateToken(userID, "username123", time.Hour) if err != nil { log.Fatal(err) }
type PasetoMaker ¶
type PasetoMaker struct {
// contains filtered or unexported fields
}
PasetoMaker is a struct for handling Paseto token creation and validation.
func (*PasetoMaker) GenerateToken ¶
func (maker *PasetoMaker) GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error)
GenerateToken creates a new Paseto token for a specific user with a given duration.
Example usage:
token, err := maker.GenerateToken(userID, "username123", time.Hour) if err != nil { log.Fatal(err) }
func (*PasetoMaker) ValidateToken ¶
func (maker *PasetoMaker) ValidateToken(token string) (*Payload, error)
ValidateToken checks if the given Paseto token is valid.
Example usage:
payload, err := maker.ValidateToken(tokenString) if err != nil { log.Fatal("Invalid token") }
type Payload ¶
type Payload struct { ID uuid.UUID `json:"id"` UserID uuid.UUID `json:"user_id"` Username string `json:"username"` IssuedAt time.Time `json:"issued_at"` ExpiredAt time.Time `json:"expired_at"` }
Payload contains the data embedded within a token.
func NewPayload ¶
NewPayload creates a new token payload with a specific username and token duration.
Example usage:
payload, err := NewPayload(userID, "username123", time.Hour) if err != nil { log.Fatal(err) }
type TokenManager ¶
type TokenManager interface { GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error) ValidateToken(token string) (*Payload, error) }
TokenManager is the interface for creating and verifying tokens.
Example usage:
var manager TokenManager manager, err = NewTokenManager("jwt", "your-secret-key") if err != nil { log.Fatal(err) }
func NewJWTMaker ¶
func NewJWTMaker(secretKey string) (TokenManager, error)
NewJWTMaker creates a new JWTMaker with the given symmetric key.
Example usage:
maker, err := NewJWTMaker("your-secret-key") if err != nil { log.Fatal(err) }
func NewPasetoMaker ¶
func NewPasetoMaker(secretKey string) (TokenManager, error)
NewPasetoMaker creates a new PasetoMaker with the given symmetric key.
Example usage:
maker, err := NewPasetoMaker("your-secret-key") if err != nil { log.Fatal(err) }
func NewTokenManager ¶
func NewTokenManager(tokenType, secretKey string) (TokenManager, error)
NewTokenManager creates a new token manager (JWT or Paseto) depending on the provided type.
Example usage:
manager, err := NewTokenManager("jwt", "your-secret-key") if err != nil { log.Fatal(err) }