Initial commit
This commit is contained in:
commit
068f092f37
4 changed files with 81 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
jwtgen
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module git.kcbark.net/kcbark/jwtgen
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
3
go.sum
Normal file
3
go.sum
Normal file
|
@ -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=
|
72
main.go
Normal file
72
main.go
Normal file
|
@ -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("-----------------------")
|
||||
}
|
Loading…
Reference in a new issue