From 2620ff7277fe27fc235f20f736dca3000abef83f Mon Sep 17 00:00:00 2001 From: Dr Nic Williams Date: Sat, 30 Jun 2018 19:58:15 +1000 Subject: [PATCH] use 'uaa context --access_token' and '--auth_header' --- cmd/context.go | 14 +++++++++++++- cmd/context_test.go | 25 +++++++++++++++++++++---- cmd/root.go | 6 ++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/cmd/context.go b/cmd/context.go index dbe8990..6442da0 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -1,10 +1,12 @@ package cmd import ( + "fmt" + "os" + "code.cloudfoundry.org/uaa-cli/cli" "code.cloudfoundry.org/uaa-cli/help" "github.com/spf13/cobra" - "os" ) var contextCmd = &cobra.Command{ @@ -27,6 +29,14 @@ var contextCmd = &cobra.Command{ } activeContext := c.GetActiveContext() + if showAccessToken { + fmt.Print(activeContext.Token.AccessToken) + os.Exit(0) + } + if showAuthHeader { + fmt.Printf(`%s %s`, activeContext.Token.TokenType, activeContext.Token.AccessToken) + os.Exit(0) + } err := cli.NewJsonPrinter(log).Print(activeContext) if err != nil { log.Error(err.Error()) @@ -39,4 +49,6 @@ func init() { RootCmd.AddCommand(contextCmd) contextCmd.Annotations = make(map[string]string) contextCmd.Annotations[INTRO_CATEGORY] = "true" + contextCmd.Flags().BoolVarP(&showAccessToken, "access_token", "", false, "Display context's access token") + contextCmd.Flags().BoolVarP(&showAuthHeader, "auth_header", "a", false, "Display context's token type and access token") } diff --git a/cmd/context_test.go b/cmd/context_test.go index 42a4a59..1aaf5a7 100644 --- a/cmd/context_test.go +++ b/cmd/context_test.go @@ -43,6 +43,8 @@ var _ = Describe("Context", func() { BeforeEach(func() { c := config.NewConfigWithServerURL("http://login.somewhere.com") ctx := config.UaaContext{ClientId: "admin", Username: "woodstock"} + ctx.Token.TokenType = "bearer" + ctx.Token.AccessToken = "token" c.AddContext(ctx) config.WriteConfig(c) }) @@ -52,15 +54,30 @@ var _ = Describe("Context", func() { "client_id": "admin", "grant_type": "", "username": "woodstock", - "Token": { - "access_token": "", - "expiry": "0001-01-01T00:00:00Z" - } + "Token": { + "access_token": "token", + "token_type": "bearer", + "expiry": "0001-01-01T00:00:00Z" + } }` session := runCommand("context") Expect(session.Out.Contents()).To(MatchJSON(activeContextJson)) Eventually(session).Should(Exit(0)) }) + + It("displays the context access_token", func() { + session := runCommand("context", "--access_token") + + Expect(session.Out).To(Say(`token`)) + Eventually(session).Should(Exit(0)) + }) + + It("displays the context authentication header", func() { + session := runCommand("context", "--auth_header") + + Expect(session.Out).To(Say(`bearer token`)) + Eventually(session).Should(Exit(0)) + }) }) }) diff --git a/cmd/root.go b/cmd/root.go index 0ad7821..8295fb2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,6 +27,12 @@ var ( verbose bool ) +// Context flags +var ( + showAccessToken bool + showAuthHeader bool +) + // Client flags var ( clientSecret string