Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify local pem key #33

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions cmd/jwt.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package cmd

import (
"crypto"
"fmt"
"io/ioutil"
"log"

"github.com/benfdking/jen/pkg/defaultjwt"
"github.com/benfdking/jen/pkg/rsapem"
"github.com/benfdking/jen/pkg/url"
"github.com/spf13/cobra"
)

var key string
var keyFilePath string
var addDefaults bool
var filePath string
var claims map[string]string
Expand All @@ -20,38 +24,55 @@ var jwtCmd = &cobra.Command{
Short: "Generate a jwt",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
_, pem, err := url.ReturnJWKSAndPrivateKey(key)
if err != nil {
log.Fatal(err)
var privateKey crypto.PrivateKey
if keyFilePath != "" {
keyBytes, err := ioutil.ReadFile(keyFilePath)
if err != nil {
log.Fatal(err)
}
privateKey, err = rsapem.RSAPrivateFromPen(keyBytes)
if err != nil {
log.Fatal(err)
}
} else {
var err error
_, privateKey, err = url.ReturnJWKSAndPrivateKey(key)
if err != nil {
log.Fatal(err)
}
}

token := defaultjwt.NewToken()
if addDefaults {
var err error
token, err = defaultjwt.AddDefaultClaims(token)
if err != nil {
log.Fatal(err)
}
}
if filePath != "" {
var err error
token, err = defaultjwt.AddJSONFileClaimsToToken(token, filePath)
if err != nil {
log.Fatal(err)
}
}
if len(args) == 1 {
var err error
token, err = defaultjwt.AddJSONStringClaimsToToken(token, args[0])
if err != nil {
log.Fatal(err)
}
}
if len(claims) > 0 {
var err error
token, err = defaultjwt.AddMapClaimsToToken(token, claims)
if err != nil {
log.Fatal(err)
}
}

s, err := defaultjwt.SignToken(token, pem)
s, err := defaultjwt.SignToken(token, privateKey)
if err != nil {
log.Fatal(err)
}
Expand All @@ -66,4 +87,5 @@ func init() {
jwtCmd.Flags().StringVarP(&key, "key", "k", "a", "[abc] jwt key to use")
jwtCmd.Flags().BoolVarP(&addDefaults, "defaults", "d", true, "Add default claims")
jwtCmd.Flags().StringVarP(&filePath, "file", "f", "", "Add claims from JSON file")
jwtCmd.Flags().StringVarP(&keyFilePath, "private", "p", "", "Use private key to sign jwt")
}