From 068f092f3709c4f42073998e10e61f6455f14981 Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Thu, 9 Sep 2021 16:34:53 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + go.mod | 5 ++++ go.sum | 3 +++ main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..deb3039 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +jwtgen diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f450966 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.kcbark.net/kcbark/jwtgen + +go 1.15 + +require github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f964c6b --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6c93081 --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "flag" + "fmt" + "os" + "time" + + "github.com/dgrijalva/jwt-go" +) + +func main() { + + secret := os.Getenv("JWT_SECRET") + + if secret == "" { + fmt.Println("No secret in environment") + } + + hostname, err := os.Hostname() + if err != nil { + fmt.Println(err) + return + } + fmt.Println(hostname) + + usernameFlag := flag.String("username", "demo", "username to use when generating jwt token") + issuerFlag := flag.String("issuer", "issuer.golang.com", "issuer to use when generating jwt token") + expireFlag := flag.Int("expire", 0, "expire how many hours when generating jwt token") + adminFlag := flag.Bool("admin", false, "should the user be admin?") + writeFlag := flag.Bool("write", false, "should the user be able to write?") + readFlag := flag.Bool("read", true, "should the user be able to read?") + + flag.Parse() + + var token *jwt.Token + if *expireFlag == 0 { + token = jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "username": *usernameFlag, + "admin": *adminFlag, + "write": *writeFlag, + "read": *readFlag, + "iss": *issuerFlag, + "iat": time.Now().Unix(), + }) + } else { + token = jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "username": *usernameFlag, + "admin": *adminFlag, + "write": *writeFlag, + "read": *readFlag, + "iss": *issuerFlag, + "exp": time.Now().Add(time.Hour * time.Duration(*expireFlag)).Unix(), + "iat": time.Now().Unix(), + }) + } + tokenString, err := token.SignedString([]byte(secret)) + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("Generated JWT token for\n") + fmt.Printf("Username: %s\n", *usernameFlag) + fmt.Printf("Admin: %v\n", *adminFlag) + fmt.Printf("Write: %v\n", *writeFlag) + fmt.Printf("Read: %v\n", *readFlag) + fmt.Printf("Issuer: %s\n", *issuerFlag) + fmt.Printf("Expire: %d hours\n", *expireFlag) + fmt.Println("-------- Token --------") + fmt.Printf("%s\n", tokenString) + fmt.Println("-----------------------") +}