Skip to content

Commit

Permalink
Merge pull request #5 from oidc-mytoken/dev
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
zachmann authored Aug 3, 2021
2 parents 6bfb563 + c3bdc88 commit 7213309
Show file tree
Hide file tree
Showing 11 changed files with 641 additions and 264 deletions.
20 changes: 11 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ module github.com/oidc-mytoken/client
go 1.14

require (
github.com/Songmu/prompter v0.4.0
github.com/go-resty/resty/v2 v2.5.0 // indirect
github.com/Songmu/prompter v0.5.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/jessevdk/go-flags v1.4.0
github.com/oidc-mytoken/lib v0.2.0
github.com/oidc-mytoken/server v0.2.0
github.com/oidc-mytoken/api v0.3.0
github.com/oidc-mytoken/lib v0.2.1
github.com/oidc-mytoken/server v0.3.0
github.com/sirupsen/logrus v1.8.1
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
github.com/zachmann/cli/v2 v2.3.1-0.20210512144416-96dd678d93c7
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
97 changes: 77 additions & 20 deletions go.sum

Large diffs are not rendered by default.

58 changes: 47 additions & 11 deletions internal/commands/at.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,61 @@ import (
"io/ioutil"

"github.com/oidc-mytoken/client/internal/config"
"github.com/zachmann/cli/v2"
)

// atCommand is a type for holding and handling the AT command
type atCommand struct {
PTOptions
Scopes []string `long:"scope" short:"s" description:"Request the passed scope. Can be used multiple times"`
Audiences []string `long:"aud" description:"Request the passed audience. Can be used multiple times"`
Out string `long:"out" short:"o" default:"/dev/stdout" description:"The access token will be printed to this output."`
var atCommand = struct {
*PTOptions
Scopes cli.StringSlice
Audiences cli.StringSlice
Out string
}{}

func init() {
ptFlags, opts := getPTFlags()
atCommand.PTOptions = opts
app.Commands = append(app.Commands, &cli.Command{
Name: "AT",
Aliases: []string{"at", "access-token"},
Usage: "Obtain an OIDC access token",
Action: getAT,
Flags: append(ptFlags,
&cli.StringSliceFlag{
Name: "scope",
Aliases: []string{"s"},
Usage: "Request the passed scope.",
DefaultText: "all scopes allowed for the used mytoken",
Destination: &atCommand.Scopes,
Placeholder: "SCOPE",
},
&cli.StringSliceFlag{
Name: "aud",
Aliases: []string{"audience"},
Usage: "Request the passed audience.",
Destination: &atCommand.Audiences,
Placeholder: "AUD",
},
&cli.StringFlag{
Name: "out",
Aliases: []string{"o"},
Usage: "The access token will be printed to this output",
Value: "/dev/stdout",
Destination: &atCommand.Out,
Placeholder: "FILE",
},
),
})
}

// Execute implements the flags.Commander interface
func (atc *atCommand) Execute(args []string) error {
func getAT(context *cli.Context) error {
atc := atCommand
var comment string
if len(args) > 0 {
comment = args[0]
if context.Args().Len() > 0 {
comment = context.Args().Get(0)
}
mytoken := config.Get().Mytoken
provider, mToken := atc.Check()
at, err := mytoken.GetAccessToken(mToken, provider.Issuer, atc.Scopes, atc.Audiences, comment)
at, err := mytoken.GetAccessToken(mToken, provider.Issuer, atc.Scopes.Value(), atc.Audiences.Value(), comment)
if err != nil {
return err
}
Expand Down
70 changes: 61 additions & 9 deletions internal/commands/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,70 @@ import (
"github.com/oidc-mytoken/server/shared/utils"
"github.com/oidc-mytoken/server/shared/utils/jwtutils"
log "github.com/sirupsen/logrus"
"github.com/zachmann/cli/v2"

"github.com/oidc-mytoken/client/internal/config"
"github.com/oidc-mytoken/client/internal/model"
)

// PTOptions holds command line options that can be used with all commands
type PTOptions struct {
Provider string `short:"i" long:"provider" description:"The name or issuer url of the OpenID provider that should be used"`
Name string `short:"t" long:"name" description:"The name of the mytoken that should be used"`
Mytoken *string `long:"MT" optional:"true" optional-value:"" description:"The passed mytoken is used instead of a stored one. If cou want to use this, please check if one of the more secure options --MT-file or --MT-env can be used"`
MytokenFile string `long:"MT-file" description:"Read the mytoken that should be used from the first line of the passed file"`
MytokenEnv string `long:"MT-env" description:"Read the mytoken that should be used from the passed environment variable"`
Provider string
Name string
Mytoken string
MytokenPrompt bool
MytokenFile string
MytokenEnv string
}

func getPTFlags() ([]cli.Flag, *PTOptions) {
opts := PTOptions{}
flags := []cli.Flag{
&cli.StringFlag{
Name: "provider",
Aliases: []string{"i", "issuer"},
Usage: "The name or issuer url of the OpenID provider that should be used",
EnvVars: []string{"MYTOKEN_PROVIDER"},
Destination: &opts.Provider,
Placeholder: "PROVIDER",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"t", "n"},
Usage: "The `NAME` of the mytoken that should be used",
EnvVars: []string{"MYTOKEN_NAME"},
Destination: &opts.Name,
},
&cli.StringFlag{
Name: "MT",
Usage: "The passed `MYTOKEN` is used instead of a stored one. If you want to use this, please check if one of the more secure options --MT-prompt, --MT-file or --MT-env can be used",
Destination: &opts.Mytoken,
},
&cli.BoolFlag{
Name: "MT-prompt",
Usage: "If set, you are prompted for a mytoken to be passed",
Destination: &opts.MytokenPrompt,
HideDefaultValue: true,
},
&cli.StringFlag{
Name: "MT-file",
Usage: "Read the mytoken that should be used from the first line of the passed `FILE`",
TakesFile: true,
Destination: &opts.MytokenFile,
},
&cli.StringFlag{
Name: "MT-env",
Usage: "Read the mytoken that should be used from the passed environment variable `ENV`",
Destination: &opts.MytokenEnv,
},
}
return flags, &opts
}

func addPTFlags(cmd *cli.Command) *PTOptions {
flags, opts := getPTFlags()
cmd.Flags = append(cmd.Flags, flags...)
return opts
}

func (g *PTOptions) Check() (*model.Provider, string) {
Expand All @@ -45,12 +97,12 @@ func (g *PTOptions) Check() (*model.Provider, string) {
}

func (g *PTOptions) getToken() (string, error) {
if g.Mytoken != nil {
if *g.Mytoken != "" {
return *g.Mytoken, nil
}
if g.MytokenPrompt {
return prompter.Password("Enter mytoken"), nil
}
if g.Mytoken != "" {
return g.Mytoken, nil
}
if g.MytokenEnv != "" {
tok, ok := os.LookupEnv(g.MytokenEnv)
if ok {
Expand Down
94 changes: 51 additions & 43 deletions internal/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,51 @@ import (
"strings"

"github.com/oidc-mytoken/server/shared/utils"
"github.com/zachmann/cli/v2"

"github.com/oidc-mytoken/client/internal/config"
)

// infoCommand is a type for holding and handling the info command
type infoCommand struct {
*PTOptions
EventHistory historyCommand `command:"history" description:"List the event history for this token"`
SubTree subTreeCommand `command:"subtokens" description:"List the tree of subtokens for this token"`
Introspect introspectCommand `command:"introspect" description:"Gives basic information about this token and its usages"`
TokenList listMytokensCommand `command:"list-mytokens" description:"List all mytokens"`
}

// introspectCommand is a type for holding and handling the info command
type introspectCommand struct {
*PTOptions
}

// historyCommand is a type for holding and handling the info command
type historyCommand struct {
*PTOptions
}

// subTreeCommand is a type for holding and handling the info command
type subTreeCommand struct {
*PTOptions
}
var infoOptions *PTOptions

// listMytokensCommand is a type for holding and handling the info command
type listMytokensCommand struct {
*PTOptions
func init() {
var flags []cli.Flag
flags, infoOptions = getPTFlags()
cmd :=
&cli.Command{
Name: "info",
Usage: "Get information about a mytoken",
Action: info,
Flags: flags,
Subcommands: []*cli.Command{
{
Name: "history",
Usage: "List the event history for this token",
Action: history,
Flags: flags,
},
{
Name: "subtokens",
Aliases: []string{"token-tree", "tree"},
Usage: "List the tree of subtokens for this token",
Action: subTree,
Flags: flags,
},
{
Name: "introspect",
Usage: "Gives basic information about the token and its usages",
Action: introspect,
Flags: flags,
},
{
Name: "list-mytokens",
Usage: "List all mytokens",
Action: listMytokens,
Flags: flags,
},
},
}
app.Commands = append(app.Commands, cmd)
}

func prettyPrintJSON(obj interface{}) error {
Expand All @@ -57,14 +71,12 @@ func prettyPrintJSON(obj interface{}) error {
if err := json.Indent(&infoBuffer, data, "", " "); err != nil {
return err
}
info := infoBuffer.String()
fmt.Println(info)
fmt.Println(infoBuffer.String())
return nil
}

// Execute implements the flags.Commander interface
func (ic *infoCommand) Execute(args []string) error {
_, mToken := ic.Check()
func info(ctx *cli.Context) error {
_, mToken := infoOptions.Check()
if !utils.IsJWT(mToken) {
return fmt.Errorf("The token is not a JWT.")
}
Expand All @@ -76,43 +88,39 @@ func (ic *infoCommand) Execute(args []string) error {
return prettyPrintJSON(decodedPayload)
}

// Execute implements the flags.Commander interface
func (ic *introspectCommand) Execute(args []string) error {
func introspect(ctx *cli.Context) error {
mytoken := config.Get().Mytoken
_, mToken := ic.Check()
_, mToken := infoOptions.Check()
res, err := mytoken.TokeninfoIntrospect(mToken)
if err != nil {
return err
}
return prettyPrintJSON(res)
}

// Execute implements the flags.Commander interface
func (hc *historyCommand) Execute(args []string) error {
func history(ctx *cli.Context) error {
mytoken := config.Get().Mytoken
_, mToken := hc.Check()
_, mToken := infoOptions.Check()
res, err := mytoken.TokeninfoHistory(mToken)
if err != nil {
return err
}
return prettyPrintJSON(res)
}

// Execute implements the flags.Commander interface
func (sc *subTreeCommand) Execute(args []string) error {
func subTree(ctx *cli.Context) error {
mytoken := config.Get().Mytoken
_, mToken := sc.Check()
_, mToken := infoOptions.Check()
res, err := mytoken.TokeninfoSubtokens(mToken)
if err != nil {
return err
}
return prettyPrintJSON(res)
}

// Execute implements the flags.Commander interface
func (lc *listMytokensCommand) Execute(args []string) error {
func listMytokens(ctx *cli.Context) error {
mytoken := config.Get().Mytoken
_, mToken := lc.Check()
_, mToken := infoOptions.Check()
res, err := mytoken.TokeninfoListMytokens(mToken)
if err != nil {
return err
Expand Down
37 changes: 24 additions & 13 deletions internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ import (
"strings"

"github.com/oidc-mytoken/client/internal/config"
"github.com/zachmann/cli/v2"
)

// listCommand is a type for holding and handling the list command
type listCommand struct {
ListTokens listTokenCommand `command:"tokens" description:"List the stored mytokens"`
ListProviders listProviderCommand `command:"providers" description:"List the available providers"`
// 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"`
func init() {
cmd :=
&cli.Command{
Name: "list",
Usage: "List different information",
Subcommands: []*cli.Command{
{
Name: "tokens",
Aliases: []string{"MT", "mytokens"},
Usage: "List the stored mytokens",
Action: listTokens,
},
{
Name: "providers",
Aliases: []string{"issuers"},
Usage: "List the available providers",
Action: listProviders,
},
},
}
app.Commands = append(app.Commands, cmd)
}

type listTokenCommand struct{}
type listProviderCommand struct{}

// Execute implements the flags.Commander interface
func (lt *listTokenCommand) Execute(args []string) error {
func listTokens(ctx *cli.Context) error {
for iss, tokens := range config.Get().TokensFileContent.Tokens {
provider, found := config.Get().Providers.FindBy(iss, true)
header := iss
Expand All @@ -41,8 +53,7 @@ func (lt *listTokenCommand) Execute(args []string) error {
return nil
}

// Execute implements the flags.Commander interface
func (lp *listProviderCommand) Execute(args []string) error {
func listProviders(ctx *cli.Context) error {
defaultProvider := config.Get().DefaultProvider
instanceProviders := config.Get().Mytoken.ProvidersSupported
configProviders := config.Get().Providers
Expand Down
Loading

0 comments on commit 7213309

Please sign in to comment.