Skip to content

Commit

Permalink
Add claims through cli
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Jun 6, 2020
1 parent 6d94886 commit c1f846a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions cmd/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var key string
var addDefaults bool
var filePath string
var claims map[string]string

// jwtCmd represents the jwt command
var jwtCmd = &cobra.Command{
Expand All @@ -31,14 +32,20 @@ var jwtCmd = &cobra.Command{
log.Fatal(err)
}
}
if filePath != "" {
token, err = defaultjwt.AddJSONFileClaimsToToken(token, filePath)
if err != nil {
log.Fatal(err)
}
}
if len(args) == 1 {
token, err = defaultjwt.AddJSONStringClaimsToToken(token, args[0])
if err != nil {
log.Fatal(err)
}
}
if filePath != "" {
token, err = defaultjwt.AddJSONFileClaimsToToken(token, filePath)
if len(claims) > 0 {
token, err = defaultjwt.AddMapClaimsToToken(token, claims)
if err != nil {
log.Fatal(err)
}
Expand All @@ -55,6 +62,7 @@ var jwtCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(jwtCmd)

jwtCmd.Flags().StringToStringVarP(&claims, "claims", "c", map[string]string{}, "claims to be added to the jwt")
jwtCmd.Flags().StringVarP(&key, "key", "k", "a", "[abc] jwt key to use")
jwtCmd.Flags().BoolVarP(&addDefaults, "defaults", "d", true, "adds default oidc parameters, true by default")
jwtCmd.Flags().StringVarP(&filePath, "file", "f", "", "json file to read claims from")
Expand Down
6 changes: 3 additions & 3 deletions pkg/defaultjwt/defaultjwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func AddDefaultClaims(token jwt.Token) (jwt.Token, error) {
return token, nil
}

// addMapClaimsToToken adds all the properties of a map to the token
func addMapClaimsToToken(t jwt.Token, cs map[string]string) (jwt.Token, error) {
// AddMapClaimsToToken adds all the properties of a map to the token
func AddMapClaimsToToken(t jwt.Token, cs map[string]string) (jwt.Token, error) {
for k, v := range cs {
err := t.Set(k, v)
if err != nil {
Expand All @@ -55,7 +55,7 @@ func AddJSONStringClaimsToToken(t jwt.Token, s string) (jwt.Token, error) {
if err != nil {
return nil, err
}
return addMapClaimsToToken(t, values)
return AddMapClaimsToToken(t, values)
}

// AddJSONFileClaimsToToken adds content of json file to token
Expand Down

0 comments on commit c1f846a

Please sign in to comment.