goPasetoV4

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 8 Imported by: 0

README

goPasetoV4

The goPasetoV4 package provides a robust and secure implementation for creating and validating Paseto (Platform-Agnostic Security Tokens) tokens in Go. This package focuses on ease of use, security, and flexibility, making it suitable for enterprise-level applications that require reliable authentication mechanisms.

Features

  • Secure token generation using Paseto V4.
  • Flexible nonce handling with environmental configuration or automatic generation.
  • Comprehensive validation of tokens, including expiration and integrity checks.
  • Easy integration with Go applications.

Installation

To install goPasetoV4, use the go get command:

go get -u github.com/anfen93/goPasetoV4

This command retrieves the library and installs it.

Usage

Below is a quick example of how to use goPasetoV4:

Token Creation
package main

import (
    "github.com/anfen93/goPasetoV4"
    "log"
    "time"
)

func main() {
    maker := goPasetoV4.NewPasetoMaker()

    // Create a token for a specific username with a 1-hour duration
    token, err := maker.CreateToken("username", time.Hour)
    if err != nil {
        log.Fatalf("Error creating token: %v", err)
    }

    log.Printf("Generated Token: %s", token)
}
Token Verification
package main

import (
    "github.com/anfen93/goPasetoV4"
    "log"
)

func main() {
    maker := goPasetoV4.NewPasetoMaker()

    token := "your_generated_token"
    payload, err := maker.VerifyToken(token)
    if err != nil {
        log.Fatalf("Error verifying token: %v", err)
    }

    log.Printf("Token Payload: %+v", payload)
}

Configuration

You can configure the nonce used in token generation by setting the PASETO_NONCE environment variable. If not set, the system will generate a random nonce for each instance.

Contributions

Contributions are welcome! Please feel free to submit a pull request or open issues to improve the functionality, documentation, or code quality of goPasetoV4.

Version

The current version of goPasetoV4 is v1.0.0.

TODOS

  • Right now the implementation of the Payload is not so flexible. It should be possibile to personalize the payload with custom fields.
  • Improve Test Coverage

License

This project is licensed under the MIT License.

Documentation

Overview

Package goPasetoV4 provides an implementation of the Maker interface using Paseto (Platform-Agnostic Security Tokens). It includes functionality to create and verify tokens securely.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExpiredToken = errors.New("token has expired")
	ErrInvalidToken = errors.New("token is invalid")
)

Functions

This section is empty.

Types

type Maker

type Maker interface {
	// CreateToken creates a new token for a specific username and duration
	CreateToken(username string, duration time.Duration) (string, *Payload, error)

	// VerifyToken checks if the provided token is valid or not
	VerifyToken(token string) (*Payload, error)
}

Maker is an interface for managing tokens

func NewPasetoMaker

func NewPasetoMaker() Maker

NewPasetoMaker initializes and returns a new PasetoMaker. It attempts to acquire a nonce from the PASETO_NONCE environment variable. If not available, it generates a random nonce. This function returns a Maker interface.

type PasetoMaker

type PasetoMaker struct {
	// contains filtered or unexported fields
}

PasetoMaker is a struct that implements the Maker interface. It provides methods for creating and verifying Paseto tokens.

func (*PasetoMaker) CreateToken

func (maker *PasetoMaker) CreateToken(username string, duration time.Duration) (string, *Payload, error)

CreateToken generates a new Paseto token for a given username and duration. It returns an encrypted token string and the Payload struct, or an error if the token generation fails.

func (*PasetoMaker) VerifyToken

func (maker *PasetoMaker) VerifyToken(token string) (*Payload, error)

VerifyToken checks the validity of a provided Paseto token. It returns a Payload struct if the token is valid or an error if it is invalid or expired.

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Username  string    `json:"username"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

Payload contains the payload data of a token

func NewPayload

func NewPayload(username string, duration time.Duration) (*Payload, error)

NewPayload creates a new payload with a specific username and duration

func (Payload) Valid

func (p Payload) Valid() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL