Categorygithub.com/fsobh/token
modulepackage
1.0.2
Repository: https://github.com/fsobh/token.git
Documentation: pkg.go.dev

# README

Authentication token module for Golang

license coverage last-commit repo-top-language repo-language-count

Built with the tools and technologies:

Go GitHub%20Actions


๐Ÿ”— Table of Contents


๐Ÿ“ Overview

A Go module for seamless JWT & PASETO token integration


๐Ÿ‘พ Features

  • JWT
  • PASETO V2
  • PASETO V3

๐Ÿ“ Project Structure

โ””โ”€โ”€ token/
    โ”œโ”€โ”€ .github
    โ”‚   โ””โ”€โ”€ workflows
    โ”‚       โ””โ”€โ”€ test-and-coverage.yml
    โ”œโ”€โ”€ LICENSE.txt
    โ”œโ”€โ”€ README.MD
    โ”œโ”€โ”€ coverage.svg
    โ”œโ”€โ”€ go.mod
    โ”œโ”€โ”€ go.sum
    โ”œโ”€โ”€ jwt_asym_maker.go
    โ”œโ”€โ”€ jwt_asym_maker_test.go
    โ”œโ”€โ”€ jwt_maker.go
    โ”œโ”€โ”€ jwt_maker_test.go
    โ”œโ”€โ”€ main
    โ”‚   โ”œโ”€โ”€ demo
    โ”‚   โ”‚   โ”œโ”€โ”€ util.go
    โ”‚   โ”‚   โ”œโ”€โ”€ v2.go
    โ”‚   โ”‚   โ””โ”€โ”€ v3.go
    โ”‚   โ””โ”€โ”€ test.go
    โ”œโ”€โ”€ makefile
    โ”œโ”€โ”€ maker.go
    โ”œโ”€โ”€ paseto_v2_local_maker.go
    โ”œโ”€โ”€ paseto_v2_local_maker_test.go
    โ”œโ”€โ”€ paseto_v2_public_maker.go
    โ”œโ”€โ”€ paseto_v2_public_maker_test.go
    โ”œโ”€โ”€ paseto_v3_local_maker.go
    โ”œโ”€โ”€ paseto_v3_local_maker_test.go
    โ”œโ”€โ”€ paseto_v3_public_maker.go
    โ”œโ”€โ”€ paseto_v3_public_maker_test.go
    โ”œโ”€โ”€ payload.go
    โ”œโ”€โ”€ payload_test.go
    โ””โ”€โ”€ testCoverage.out

๐Ÿ“‚ Project Index

token/
__root__
LICENSE.txtโฏ License file
jwt_maker.goโฏ Token maker for Symmetric JWT tokens
maker.goโฏ Token maker interface
paseto_v3_local_maker_test.goโฏ Test file
jwt_asym_maker_test.goโฏ Test file
makefileโฏ make file for tests
paseto_v2_public_maker.goโฏ Token maker for Paseto V2 Public tokens (Asymmetrical)
go.modโฏ go mod file
jwt_maker_test.goโฏ Test file
go.sumโฏ go sum
paseto_v2_public_maker_test.goโฏ Test file
paseto_v2_local_maker.goโฏ Token maker for Paseto V2 Local tokens (Symmetrical)
paseto_v3_local_maker.goโฏ Token maker for Paseto V3 Local tokens (Symmetrical)
payload.goโฏ Predefined payload with username and token id fields
testCoverage.outโฏ Test coverage results
jwt_asym_maker.goโฏ Token maker for Asymmetric JWT tokens
payload_test.goโฏ Test file
paseto_v2_local_maker_test.goโฏ Test file
paseto_v3_public_maker_test.goโฏ Test file
paseto_v3_public_maker.goโฏ Token maker for Paseto V3 Public tokens (Asymmetrical)
README.MDโฏ Documentation
.github
workflows
test-and-coverage.ymlโฏ Work flow to run unit tests (on push) and update readme with test coverage badge
main
test.goโฏ Playground file for development
demo
util.goโฏ Playground file for development
v3.goโฏ Playground file for development
v2.goโฏ Playground file for development

๐Ÿš€ Getting Started

โ˜‘๏ธ Prerequisites

Before getting started with token, ensure your runtime environment meets the following requirements:

  • Programming Language: Go
  • Package Manager: Go modules

โš™๏ธ Installation

Install the project dependencies:

Using go modules ย 

โฏ go get github.com/fsobh/token

๐Ÿค– Usage

  • Paseto V2
package main

import (
	"aidanwoods.dev/go-paseto"
	"encoding/json"
	"fmt"
	"github.com/fsobh/token"
	"log"

	"time"
)

func toJSON(data interface{}) string {
	jsonBytes, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		log.Fatalf("Failed to marshal to JSON: %v", err)
	}
	return string(jsonBytes)
}

func main() {

	// ** Paseto V2 Local **

	// Generate Keys
	symmetricKeyV2 := paseto.NewV2SymmetricKey()

	// convert keys to Hex
	symmetricKeyStringV2 := symmetricKeyV2.ExportHex()

	localMakerV2, err := token.NewPasetoV2Local(symmetricKeyStringV2)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V2 local token maker: %w", err)
	}

	localTokenStringV2, localPayloadV2, err := localMakerV2.CreateToken("alice", 24*time.Hour)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V2 local token: %w", err)
	}

	fmt.Println("Created Paseto V2 local Token (JSON):")
	fmt.Println(toJSON(map[string]interface{}{
		"token":      localTokenStringV2,
		"payload":    localPayloadV2,
		"expiration": localPayloadV2.ExpiredAt,
	}))

	// Verify the token
	verifiedPayload, err := localMakerV2.VerifyToken(localTokenStringV2)
	if err != nil {
		_ = fmt.Errorf("failed to verify Paseto V2 local token: %w", err)
	}

	// Display verified payload in JSON
	fmt.Println("Verified Paseto V2 local Payload (JSON):")
	fmt.Println(toJSON(verifiedPayload))




	// ** Paseto V2 Public **

	// generate keys
	privateKeyV2 := paseto.NewV2AsymmetricSecretKey()
	publicKeyV2 := privateKeyV2.Public()

	// convert keys to Hex
	privateKeyStringV2 := privateKeyV2.ExportHex()
	publicKeyStringV2 := publicKeyV2.ExportHex()

	publicMakerV2, err := token.NewPasetoV2Public(privateKeyStringV2, publicKeyStringV2)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V2 public token maker: %w", err)
	}


	publicTokenStringV2, publicPayloadV2, err := publicMakerV2.CreateToken("charlie", 24*time.Hour)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V2 public token: %w", err)
	}


	fmt.Println("Created Paseto V2 public Token (JSON):")
	fmt.Println(toJSON(map[string]interface{}{
		"token":      publicTokenStringV2,
		"payload":    publicPayloadV2,
		"expiration": publicPayloadV2.ExpiredAt,
	}))


	publicVerifiedPayloadV2, err := publicMakerV2.VerifyToken(publicTokenStringV2)
	if err != nil {
		_ = fmt.Errorf("failed to verify Paseto V2 public token: %w", err)
	}


	fmt.Println("Verified Paseto V2 public Payload (JSON):")
	fmt.Println(toJSON(publicVerifiedPayloadV2))

}
  • Paseto V3
package main

import (
	"aidanwoods.dev/go-paseto"
	"encoding/json"
	"fmt"
	"github.com/fsobh/token"
	"log"

	"time"
)

func toJSON(data interface{}) string {
	jsonBytes, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		log.Fatalf("Failed to marshal to JSON: %v", err)
	}
	return string(jsonBytes)
}

func main() {
	
	// ** Paseto V3 Local **

	symmetricKeyV3 := paseto.NewV3SymmetricKey()
	symmetricKeyStringV3 := symmetricKeyV3.ExportHex()
	
	localMakerV3, err := token.NewPasetoV3Local(symmetricKeyStringV3)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V3 local token maker: %w", err)
	}

	localTokenStringV3, localPayloadV3, err := localMakerV3.CreateToken("bob", 24*time.Hour)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V3 local token: %w", err)
	}

	fmt.Println("Created Paseto V3 local Token (JSON):")
	fmt.Println(toJSON(map[string]interface{}{
		"token":      localTokenStringV3,
		"payload":    localPayloadV3,
		"expiration": localPayloadV3.ExpiredAt,
	}))

	localVerifiedPayloadV3, err := localMakerV3.VerifyToken(localTokenStringV3)
	if err != nil {
		_ = fmt.Errorf("failed to verify Paseto V3 local token: %w", err)
	}

	fmt.Println("Verified Paseto V3 local Payload (JSON):")
	fmt.Println(toJSON(localVerifiedPayloadV3))

	
	
	
	
	// ** Paseto V3 Public **

	privateKeyV3 := paseto.NewV3AsymmetricSecretKey()
	publicKeyV3 := privateKeyV3.Public()

	privateKeyStringV3 := privateKeyV3.ExportHex()
	publicKeyStringV3 := publicKeyV3.ExportHex()
	
	publicMakerV3, err := token.NewPasetoV3Public(privateKeyStringV3, publicKeyStringV3)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V3 public token maker: %w", err)
	}

	// Create a token
	publicTokenStringV3, publicPayloadV3, err := publicMakerV3.CreateToken("dave", 24*time.Hour)
	if err != nil {
		_ = fmt.Errorf("failed to create Paseto V3 public token: %w", err)
	}

	// Display token and payload in JSON
	fmt.Println("Created Paseto V3 public Token (JSON):")
	fmt.Println(toJSON(map[string]interface{}{
		"token":      publicTokenStringV3,
		"payload":    publicPayloadV3,
		"expiration": publicPayloadV3.ExpiredAt,
	}))

	// Verify the token
	publicVerifiedPayloadV3, err := publicMakerV3.VerifyToken(publicTokenStringV3)
	if err != nil {
		_ = fmt.Errorf("failed to verify Paseto V3 public token: %w", err)
	}

	// Display verified payload in JSON
	fmt.Println("Verified Paseto V3 public Payload (JSON):")
	fmt.Println(toJSON(publicVerifiedPayloadV3))
}

๐Ÿงช Testing

Run the test suite using the following command: Using go modules ย 

โฏ make test

๐Ÿ“Œ Project Roadmap

  • Task 1: Implement JWT options.
  • Task 2: Implement Paseto V2-V3 public and local options
  • Task 3: Implement feature Paseto V4 public and local options.

๐Ÿ”ฐ Contributing

Contributing Guidelines
  1. Fork the Repository: Start by forking the project repository to your github account.
  2. Clone Locally: Clone the forked repository to your local machine using a git client.
    git clone https://github.com/fsobh/token
    
  3. Create a New Branch: Always work on a new branch, giving it a descriptive name.
    git checkout -b new-feature-x
    
  4. Make Your Changes: Develop and test your changes locally.
  5. Commit Your Changes: Commit with a clear message describing your updates.
    git commit -m 'Implemented new feature x.'
    
  6. Push to github: Push the changes to your forked repository.
    git push origin new-feature-x
    
  7. Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
  8. Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
Contributor Graph


๐ŸŽ— License

This project is protected under the MIT License. For more details, refer to the LICENSE file.


๐Ÿ™Œ Acknowledgments


# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewPasetoV3Local initializes a new PASETO V3 Local instance with the given symmetric key (in hex format).
No description provided by the author
No description provided by the author

# Variables

Different types of token Errors we will return.
Different types of token Errors we will return.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PasetoV3Local handles PASETO V3 Local tokens.
No description provided by the author
Payload will hold payload data of token.

# Interfaces

Maker interface will be used to manage the token creation and verification We will be creating support for both JWT and PASETO tokens.