Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 6 additions & 94 deletions cmd/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,112 +11,24 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/adobe/imscli/cmd/promise"
"github.com/adobe/imscli/ims"
"github.com/spf13/cobra"
)

func promiseCmd(imsConfig *ims.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "promise",
Short: "Use the IMS Promise Token flows.",
Use: "promise",
Aliases: []string{"pr"},
Short: "Use the IMS Promise Token flows.",
Long: `The promise command performs requests against the IMS /ims/token/v4 endpoint.

This command has no effect by itself, the request needs to be specified as a subcommand.
`,
}
cmd.AddCommand(
accessForPromiseCmd(imsConfig),
promiseForAccessCmd(imsConfig),
promise.AccessForPromiseCmd(imsConfig),
promise.PromiseForAccessCmd(imsConfig),
)
return cmd
}

func accessForPromiseCmd(imsConfig *ims.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "access-for-promise",
Short: "Exchange an access token for a promise token.",
Long: `Perform the IMS Promise Token grant (grant_type=promise) to exchange an authenticating access token for a promise token.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

resp, err := imsConfig.PromiseTokenExchange()
if err != nil {
return fmt.Errorf("error during the promise token exchange: %w", err)
}
if imsConfig.FullOutput {
data := struct {
PromiseToken string `json:"promise_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
}{resp.PromiseToken, resp.TokenType, resp.Scope, resp.ExpiresIn}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("error marshalling full JSON response: %w", err)
}
fmt.Printf("%s\n", jsonData)
return nil
}
fmt.Println(resp.PromiseToken)
return nil
},
}

cmd.Flags().StringVarP(&imsConfig.ClientID, "clientID", "c", "", "IMS client ID.")
cmd.Flags().StringVarP(&imsConfig.ClientSecret, "clientSecret", "p", "", "IMS client secret.")
cmd.Flags().StringVarP(&imsConfig.AccessToken, "accessToken", "t", "", "Authenticating access token (only access tokens are accepted).")
cmd.Flags().StringVarP(&imsConfig.PromiseDefinitionID, "promiseDefinitionID", "d", "", "Promise definition ID.")
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", nil, "Scopes to request in the promise token.")
cmd.Flags().BoolVarP(&imsConfig.FullOutput, "fullOutput", "F", false, "Output a JSON with the promise token, token type, scope, and expiration.")

return cmd
}

func promiseForAccessCmd(imsConfig *ims.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "promise-for-access",
Short: "Exchange a promise token for an access token.",
Long: `Perform the IMS Promise Exchange grant (grant_type=promise_exchange) to redeem a promise token for a fresh access token and a rotated promise token.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

resp, err := imsConfig.PromiseExchange()
if err != nil {
return fmt.Errorf("error during the promise exchange: %w", err)
}
if imsConfig.FullOutput {
data := struct {
AccessToken string `json:"access_token"`
PromiseToken string `json:"promise_token"`
PromiseTokenID string `json:"promise_token_id"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
PromiseTokenExpiresIn int `json:"promise_token_expires_in"`
}{
resp.AccessToken, resp.PromiseToken, resp.PromiseTokenID, resp.TokenType,
resp.Scope, resp.ExpiresIn, resp.PromiseTokenExpiresIn,
}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("error marshalling full JSON response: %w", err)
}
fmt.Printf("%s\n", jsonData)
return nil
}
fmt.Println(resp.AccessToken)
return nil
},
}

cmd.Flags().StringVarP(&imsConfig.ClientID, "clientID", "c", "", "IMS client ID.")
cmd.Flags().StringVarP(&imsConfig.ClientSecret, "clientSecret", "p", "", "IMS client secret.")
cmd.Flags().StringVarP(&imsConfig.PromiseToken, "promiseToken", "t", "", "Promise token to exchange.")
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", nil, "Optional scopes to request in the new access token.")
cmd.Flags().BoolVarP(&imsConfig.FullOutput, "fullOutput", "F", false, "Output a JSON with the access token, rotated promise token, and their expirations.")

return cmd
}
51 changes: 51 additions & 0 deletions cmd/promise/access_for_promise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package promise

import (
"encoding/json"
"fmt"

"github.com/adobe/imscli/ims"
"github.com/spf13/cobra"
)

func AccessForPromiseCmd(imsConfig *ims.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "access-for-promise",
Aliases: []string{"afp"},
Short: "Exchange an access token for a promise token.",
Long: `Perform the IMS Promise Token grant (grant_type=promise) to exchange an authenticating access token for a promise token.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

resp, err := imsConfig.PromiseTokenExchange()
if err != nil {
return fmt.Errorf("error during the promise token exchange: %w", err)
}
if imsConfig.FullOutput {
data := struct {
PromiseToken string `json:"promise_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
}{resp.PromiseToken, resp.TokenType, resp.Scope, resp.ExpiresIn}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("error marshalling full JSON response: %w", err)
}
fmt.Printf("%s\n", jsonData)
return nil
}
fmt.Println(resp.PromiseToken)
return nil
},
}

cmd.Flags().StringVarP(&imsConfig.ClientID, "clientID", "c", "", "IMS client ID.")
cmd.Flags().StringVarP(&imsConfig.ClientSecret, "clientSecret", "p", "", "IMS client secret.")
cmd.Flags().StringVarP(&imsConfig.AccessToken, "accessToken", "t", "", "Authenticating access token (only access tokens are accepted).")
cmd.Flags().StringVarP(&imsConfig.PromiseDefinitionID, "promiseDefinitionID", "d", "", "Promise definition ID.")
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", nil, "Scopes to request in the promise token.")
cmd.Flags().BoolVarP(&imsConfig.FullOutput, "fullOutput", "F", false, "Output a JSON with the promise token, token type, scope, and expiration.")

return cmd
}
56 changes: 56 additions & 0 deletions cmd/promise/promise_for_access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package promise

import (
"encoding/json"
"fmt"

"github.com/adobe/imscli/ims"
"github.com/spf13/cobra"
)

func PromiseForAccessCmd(imsConfig *ims.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "promise-for-access",
Aliases: []string{"pfa"},
Short: "Exchange a promise token for an access token.",
Long: `Perform the IMS Promise Exchange grant (grant_type=promise_exchange) to redeem a promise token for a fresh access token and a rotated promise token.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

resp, err := imsConfig.PromiseExchange()
if err != nil {
return fmt.Errorf("error during the promise exchange: %w", err)
}
if imsConfig.FullOutput {
data := struct {
AccessToken string `json:"access_token"`
PromiseToken string `json:"promise_token"`
PromiseTokenID string `json:"promise_token_id"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
PromiseTokenExpiresIn int `json:"promise_token_expires_in"`
}{
resp.AccessToken, resp.PromiseToken, resp.PromiseTokenID, resp.TokenType,
resp.Scope, resp.ExpiresIn, resp.PromiseTokenExpiresIn,
}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("error marshalling full JSON response: %w", err)
}
fmt.Printf("%s\n", jsonData)
return nil
}
fmt.Println(resp.AccessToken)
return nil
},
}

cmd.Flags().StringVarP(&imsConfig.ClientID, "clientID", "c", "", "IMS client ID.")
cmd.Flags().StringVarP(&imsConfig.ClientSecret, "clientSecret", "p", "", "IMS client secret.")
cmd.Flags().StringVarP(&imsConfig.PromiseToken, "promiseToken", "t", "", "Promise token to exchange.")
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", nil, "Optional scopes to request in the new access token.")
cmd.Flags().BoolVarP(&imsConfig.FullOutput, "fullOutput", "F", false, "Output a JSON with the access token, rotated promise token, and their expirations.")

return cmd
}
Loading