Skip to content
Snippets Groups Projects
Commit 99ecace7 authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

client: add info command for decoding token

parent cf764fb1
No related branches found
No related tags found
No related merge requests found
...@@ -17,8 +17,6 @@ type generalOptions struct { ...@@ -17,8 +17,6 @@ type generalOptions struct {
SuperToken string `long:"ST" description:"The passed super token is used instead of a stored one"` SuperToken string `long:"ST" description:"The passed super token is used instead of a stored one"`
} }
type providerOpt string
func (g *generalOptions) Check() (*model.Provider, string) { func (g *generalOptions) Check() (*model.Provider, string) {
p, pErr := g.checkProvider() p, pErr := g.checkProvider()
if len(g.SuperToken) > 0 { if len(g.SuperToken) > 0 {
......
package commands
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"github.com/zachmann/mytoken/internal/utils"
)
// infoCommand is a type for holding and handling the info command
type infoCommand struct {
generalOptions
// EventHistory historyCommand `command:"history" description:"List the event history for this token"`
// SubTree treeCommand `command:"tree" description:"List the tree of subtokens for this token"`
}
// Execute implements the flags.Commander interface
func (ic *infoCommand) Execute(args []string) error {
_, superToken := ic.Check()
if !utils.IsJWT(superToken) {
return fmt.Errorf("The token is not a JWT.")
}
payload := strings.Split(superToken, ".")[1]
decodedPayload, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
return err
}
var infoBuffer bytes.Buffer
if err = json.Indent(&infoBuffer, decodedPayload, "", " "); err != nil {
return err
}
info := infoBuffer.String()
fmt.Println(info)
return nil
}
...@@ -18,6 +18,7 @@ var options struct { ...@@ -18,6 +18,7 @@ var options struct {
ST stCommand ST stCommand
AT atCommand AT atCommand
Revoke revokeCommand Revoke revokeCommand
Info infoCommand
} }
var parser *flags.Parser var parser *flags.Parser
...@@ -35,6 +36,8 @@ func init() { ...@@ -35,6 +36,8 @@ func init() {
parser.AddGroup("Config Options", "", &options.ConfigOptions) parser.AddGroup("Config Options", "", &options.ConfigOptions)
parser.AddCommand("AT", "Obtain access token", "Obtain a new OpenID Connect access token", &options.AT) parser.AddCommand("AT", "Obtain access token", "Obtain a new OpenID Connect access token", &options.AT)
parser.AddCommand("revoke", "Revoke super token", "Revoke a mytoken super token", &options.Revoke) parser.AddCommand("revoke", "Revoke super token", "Revoke a mytoken super token", &options.Revoke)
info, _ := parser.AddCommand("info", "Get information about a super token", "Get information about a super token", &options.Info)
info.SubcommandsOptional = true
} }
// Parse parses the command line options and calls the specified command // Parse parses the command line options and calls the specified command
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment