Skip to content

Commit

Permalink
Merge pull request #147 from rawsyntax/ocm-token-generate
Browse files Browse the repository at this point in the history
Add token generation command
  • Loading branch information
vkareh authored Sep 21, 2020
2 parents f2f5751 + 5b47043 commit 5d59acd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
33 changes: 28 additions & 5 deletions cmd/ocm/token/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"fmt"
"os"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/spf13/cobra"
Expand All @@ -33,6 +34,7 @@ var args struct {
payload bool
signature bool
refresh bool
generate bool
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -69,6 +71,12 @@ func init() {
false,
"Print the refresh token instead of the access token.",
)
flags.BoolVar(
&args.generate,
"generate",
false,
"Generate a new token.",
)
}

func run(cmd *cobra.Command, argv []string) error {
Expand All @@ -83,8 +91,12 @@ func run(cmd *cobra.Command, argv []string) error {
if args.signature {
count++
}
if args.generate {
count++
}

if count > 1 {
return fmt.Errorf("Options '--payload', '--header' and '--signature' are mutually exclusive")
return fmt.Errorf("Options '--payload', '--header', '--signature', and '--generate' are mutually exclusive")
}

// Load the configuration file:
Expand All @@ -111,10 +123,21 @@ func run(cmd *cobra.Command, argv []string) error {
return fmt.Errorf("Can't create connection: %v", err)
}

// Get the tokens:
accessToken, refreshToken, err := connection.Tokens()
if err != nil {
return fmt.Errorf("Can't get token: %v", err)
var accessToken string
var refreshToken string

if args.generate {
// Get new tokens:
accessToken, refreshToken, err = connection.Tokens(15 * time.Minute)
if err != nil {
return fmt.Errorf("Can't get new tokens: %v", err)
}
} else {
// Get the tokens:
accessToken, refreshToken, err = connection.Tokens()
if err != nil {
return fmt.Errorf("Can't get token: %v", err)
}
}

// Select the token according to the options:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
github.com/openshift-online/ocm-sdk-go v0.1.128
github.com/openshift-online/ocm-sdk-go v0.1.129
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/prometheus/client_golang v1.2.1 // indirect
github.com/prometheus/client_model v0.0.0-20191202183732-d1d2010b5bee // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ github.com/openshift-online/ocm-sdk-go v0.1.123 h1:2jB6fCf1ikeKWoxcQA7+f8VFzA58t
github.com/openshift-online/ocm-sdk-go v0.1.123/go.mod h1:3i3a/LEYtC7DMor3N94PTPn1+0qq+Fk+iLjt1Z0WVCs=
github.com/openshift-online/ocm-sdk-go v0.1.128 h1:AlX7PkZOuZJPBzMM2CgF22YGMp6M9+UUPp8bfGORTDM=
github.com/openshift-online/ocm-sdk-go v0.1.128/go.mod h1:3i3a/LEYtC7DMor3N94PTPn1+0qq+Fk+iLjt1Z0WVCs=
github.com/openshift-online/ocm-sdk-go v0.1.129 h1:LjqS4OfpTspoDk6+R8IqoqSt4DJl7hhSSHkZswlXSpk=
github.com/openshift-online/ocm-sdk-go v0.1.129/go.mod h1:3i3a/LEYtC7DMor3N94PTPn1+0qq+Fk+iLjt1Z0WVCs=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
Expand Down

0 comments on commit 5d59acd

Please sign in to comment.