-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jwt-generator & improve jwt middleware
- Loading branch information
Showing
6 changed files
with
157 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.env | ||
./listener/tmp/* | ||
./listener/bin/* | ||
./listener/bin/* | ||
jwt | ||
private.pem | ||
public.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# JWT Token Generator | ||
|
||
This is a simple JWT token generator written in Go. It creates a JWT token signed with an RSA private key and includes a custom expiration date. | ||
|
||
## Prerequisites | ||
|
||
- Go 1.15 or later | ||
- RSA key pair (private key in PEM format) | ||
|
||
## Installation | ||
|
||
1. **Clone the repository (or download the jwt_generator.go file):** | ||
|
||
```sh | ||
git clone https://github.com/yourusername/jwt-generator.git | ||
cd jwt-generator | ||
``` | ||
|
||
2. **Install dependencies:** | ||
|
||
Ensure you have the `golang-jwt/jwt/v5` library installed: | ||
|
||
```sh | ||
go get github.com/golang-jwt/jwt/v5 | ||
``` | ||
|
||
3. **Generate an RSA key pair:** | ||
|
||
Use OpenSSL or a similar tool to generate an RSA key pair (in PEM format): | ||
|
||
```sh | ||
openssl genrsa -out private.pem 2048 | ||
openssl rsa -in private.pem -pubout -out public.pem | ||
``` | ||
|
||
4. **Compile the program:** | ||
|
||
```sh | ||
go build -o jwt_generator main.go | ||
``` | ||
|
||
## Usage | ||
|
||
Run the compiled binary to generate a JWT token: | ||
|
||
```sh | ||
./jwt_generator -private-key=private.pem [email protected] -exp=24h -kid=key1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/dappnode/validator-monitoring/jwt-generator | ||
|
||
go 1.22.3 | ||
|
||
require github.com/golang-jwt/jwt/v5 v5.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= | ||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func main() { | ||
// Define flags for the command-line input | ||
privateKeyPath := flag.String("private-key", "private.pem", "Path to the RSA private key file") | ||
subject := flag.String("sub", "[email protected]", "Subject claim for the JWT") | ||
expiration := flag.String("exp", "24h", "Expiration duration for the JWT (e.g., '24h' for 24 hours)") | ||
kid := flag.String("kid", "key1", "Key ID (kid) for the JWT") | ||
flag.Parse() | ||
|
||
// Read the private key file | ||
privateKeyData, err := os.ReadFile(*privateKeyPath) | ||
if err != nil { | ||
log.Fatalf("Failed to read private key file: %v", err) | ||
} | ||
|
||
// Parse the RSA private key | ||
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyData) | ||
if err != nil { | ||
log.Fatalf("Failed to parse private key: %v", err) | ||
} | ||
|
||
// Parse the expiration duration | ||
duration, err := time.ParseDuration(*expiration) | ||
if err != nil { | ||
log.Fatalf("Failed to parse expiration duration: %v", err) | ||
} | ||
|
||
// Create a new token object, specifying signing method and claims | ||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{ | ||
"sub": *subject, | ||
"exp": time.Now().Add(duration).Unix(), | ||
}) | ||
|
||
// Set the key ID (kid) in the token header | ||
token.Header["kid"] = *kid | ||
|
||
// Sign the token with the private key | ||
tokenString, err := token.SignedString(privateKey) | ||
if err != nil { | ||
log.Fatalf("Failed to sign token: %v", err) | ||
} | ||
|
||
// Output the token | ||
fmt.Println(tokenString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters