Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fsobh/token
A Go module for seamless JWT & PASETO token integration
https://github.com/fsobh/token
Last synced: about 2 months ago
JSON representation
A Go module for seamless JWT & PASETO token integration
- Host: GitHub
- URL: https://github.com/fsobh/token
- Owner: fsobh
- License: mit
- Created: 2024-11-30T07:40:47.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-12-07T21:12:59.000Z (about 2 months ago)
- Last Synced: 2024-12-07T21:21:06.422Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 105 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.txt
Awesome Lists containing this project
README
Authentication token module for Golang
Built with the tools and technologies:
## ๐ Table of Contents
- [๐ Overview](#-overview)
- [๐พ Features](#-features)
- [๐ Project Structure](#-project-structure)
- [๐ Project Index](#-project-index)
- [๐ Getting Started](#-getting-started)
- [โ๏ธ Prerequisites](#-prerequisites)
- [โ๏ธ Installation](#-installation)
- [๐ค Usage](#-usage)
- [๐งช Testing](#-testing)
- [๐ Project Roadmap](#-project-roadmap)
- [๐ฐ Contributing](#-contributing)
- [๐ License](#-license)
- [๐ Acknowledgments](#-acknowledgments)---
## ๐ Overview
**A Go module for seamless JWT & PASETO token integration**
---
## ๐พ Features
- **JWT**
- **PASETO V2**
- **PASETO V3**
---## ๐ Project Structure
```sh
โโโ 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`** ย [](https://golang.org/)
```sh
โฏ go get github.com/fsobh/token
```### ๐ค Usage
- **Paseto V2**
```go
package mainimport (
"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**
```go
package mainimport (
"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))
}
```
- **JWT**
```go
package mainimport (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/fsobh/token"
"golang.org/x/crypto/ed25519"
"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() {
// ** JWT Asymmetric **
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
fmt.Println("error while generating keys: ", err)
return
}jwtMakerAsymmetric, err := token.NewAsymJWTMaker(privateKey, publicKey)
if err != nil {
_ = fmt.Errorf("Cannot create JWT jwtMakerAsymmetric: %v\n", err)
}jwtTokenAsymmetric, jwtPayloadAsymmetric, err := jwtMakerAsymmetric.CreateToken("charlie", 24*time.Hour)
if err != nil {
_ = fmt.Errorf("failed to create token: %w", err)
}fmt.Println("Created Token (JSON):")
fmt.Println(toJSON(map[string]interface{}{
"token": jwtTokenAsymmetric,
"payload": jwtPayloadAsymmetric,
"expiration": jwtPayloadAsymmetric.ExpiredAt,
}))jwtVerifiedPayloadAsymmetric, err := jwtMakerAsymmetric.VerifyToken(jwtTokenAsymmetric)
if err != nil {
_ = fmt.Errorf("failed to verify token: %w", err)
}fmt.Println("Verified Payload (JSON):")
fmt.Println(toJSON(jwtVerifiedPayloadAsymmetric))// ** JWT Symmetric **
symmetricKey := "abcdefghijklmnopqrstuvwxyzABCDEF"
jwtMakerSymmetric, err := token.NewJWTMaker(symmetricKey)
if err != nil {
_ = fmt.Errorf("Cannot create JWT jwtMakerSymmetric: %v\n", err)
}jwtTokenSymmetric, jwtPayloadSymmetric, err := jwtMakerSymmetric.CreateToken("charlie", 24*time.Hour)
if err != nil {
_ = fmt.Errorf("failed to create token: %w", err)
}// Display token and payload in JSON
fmt.Println("Created Token (JSON):")
fmt.Println(toJSON(map[string]interface{}{
"token": jwtTokenSymmetric,
"payload": jwtPayloadSymmetric,
"expiration": jwtPayloadSymmetric.ExpiredAt,
}))// Verify the token
jwtVerifiedPayloadSymmetric, err := jwtMakerSymmetric.VerifyToken(jwtTokenSymmetric)
if err != nil {
_ = fmt.Errorf("failed to verify token: %w", err)
}// Display verified payload in JSON
fmt.Println("Verified Payload (JSON):")
fmt.Println(toJSON(jwtVerifiedPayloadSymmetric))
}```
### ๐งช Testing
Run the test suite using the following command:
**Using `go modules`** ย [](https://golang.org/)```sh
โฏ make test
```---
## ๐ Project Roadmap- [X] **`Task 1`**: Implement JWT options.
- [X] **`Task 2`**: Implement Paseto V2-V3 public and local options
- [ ] **`Task 3`**: Implement feature Paseto V4 public and local options.---
## ๐ฐ Contributing
- **๐ฌ [Join the Discussions](https://github.com/fsobh/token/discussions)**: Share your insights, provide feedback, or ask questions.
- **๐ [Report Issues](https://github.com/fsobh/token/issues)**: Submit bugs found or log feature requests for the `token` project.
- **๐ก [Submit Pull Requests](https://github.com/fsobh/token/blob/main/CONTRIBUTING.md)**: Review open PRs, and submit your own PRs.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.
```sh
git clone https://github.com/fsobh/token
```
3. **Create a New Branch**: Always work on a new branch, giving it a descriptive name.
```sh
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.
```sh
git commit -m 'Implemented new feature x.'
```
6. **Push to github**: Push the changes to your forked repository.
```sh
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](https://choosealicense.com/licenses/mit/) License. For more details, refer to the [LICENSE](./LICENSE.txt) file.
---
## ๐ Acknowledgments
- [aidantwoods](https://github.com/aidantwoods) for implementing [go-paseto](https://github.com/aidantwoods/go-paseto) for the core functionality
---