Skip to content

Commit

Permalink
expires in seconds consistently and for keys create and apps genkey only
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtkanaskie committed Dec 13, 2023
1 parent c88639f commit 685dd23
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
8 changes: 5 additions & 3 deletions cmd/apps/createkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ var CreateKeyCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if _, err = strconv.Atoi(expires); err != nil {
return fmt.Errorf("expires must be an integer: %v", err)
if expires != "" {
if _, err = strconv.Atoi(expires); err != nil {
return fmt.Errorf("expires must be an integer: %v", err)
}
}
_, err = apps.CreateKey(developerEmail, name, key, secret, apiProducts, scopes, expires, attrs)
return
Expand All @@ -52,7 +54,7 @@ func init() {
CreateKeyCmd.Flags().StringArrayVarP(&scopes, "scopes", "s",
[]string{}, "OAuth scopes")
CreateKeyCmd.Flags().StringVarP(&expires, "expires", "x",
"", "A setting, in milliseconds, for the lifetime of the consumer key")
"", "A setting, in seconds, for the lifetime of the consumer key")
CreateKeyCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")

Expand Down
13 changes: 11 additions & 2 deletions cmd/apps/genkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -31,6 +34,12 @@ var GenKeyCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if expires != "" {
if _, err = strconv.Atoi(expires); err != nil {
return fmt.Errorf("expires must be an integer: %v", err)
}
expires += "000"
}
_, err = apps.GenerateKey(name, devID, apiProducts, callback, expires, scopes)
return
},
Expand All @@ -42,9 +51,9 @@ func init() {
GenKeyCmd.Flags().StringVarP(&name, "name", "n",
"", "Name of the developer app")
GenKeyCmd.Flags().StringVarP(&devID, "devid", "d",
"", "Developer Id")
"", "The developer's id or email")
GenKeyCmd.Flags().StringVarP(&expires, "expires", "x",
"", "A setting, in milliseconds, for the lifetime of the consumer key")
"", "A setting, in seconds, for the lifetime of the consumer key")
GenKeyCmd.Flags().StringVarP(&callback, "callback", "c",
"", "The callbackUrl is used by OAuth")
GenKeyCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p",
Expand Down
13 changes: 1 addition & 12 deletions cmd/apps/updatekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -34,30 +31,22 @@ var UpdateKeyCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if _, err = strconv.Atoi(expires); err != nil {
return fmt.Errorf("expires must be an integer: %v", err)
}
_, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, expires, attrs)
_, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, attrs)
return
},
}

func init() {
UpdateKeyCmd.Flags().StringVarP(&key, "key", "k",
"", "Developer app consumer key")
UpdateKeyCmd.Flags().StringVarP(&secret, "secret", "r",
"", "Developer app consumer secret")
UpdateKeyCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p",
[]string{}, "A list of api products")
UpdateKeyCmd.Flags().StringArrayVarP(&scopes, "scopes", "s",
[]string{}, "OAuth scopes")
UpdateKeyCmd.Flags().StringVarP(&expires, "expires", "x",
"", "A setting, in milliseconds, for the lifetime of the consumer key")
UpdateKeyCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")

_ = UpdateKeyCmd.MarkFlagRequired("name")
_ = UpdateKeyCmd.MarkFlagRequired("key")
_ = UpdateKeyCmd.MarkFlagRequired("secret")
_ = UpdateKeyCmd.MarkFlagRequired("prods")
}
21 changes: 8 additions & 13 deletions internal/client/apps/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer
key = append(key, "\"consumerSecret\":\""+consumerSecret+"\"")

if expires != "" {
key = append(key, "\"expiresAt\":\""+expires+"\"")
key = append(key, "\"expiresInSeconds\":\""+expires+"\"")
}

payload := "{" + strings.Join(key, ",") + "}"

u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerEmail, "apps", appID, "keys")

if len(apiProducts) > 0 {
Expand All @@ -62,9 +61,8 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer

// since the API does not support adding products when creating a key, use a second API call to add products
if len(apiProducts) > 0 {
apiclient.ClientPrintHttpResponse.Set(false)
respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, expires)
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
apiclient.ClientPrintHttpResponse.Set(true)
respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, scopes)
}

return respBody, err
Expand All @@ -88,7 +86,7 @@ func GetKey(developerEmail string, appID string, key string) (respBody []byte, e

// UpdateKey
func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string,
apiProducts []string, scopes []string, expires string, attrs map[string]string) (respBody []byte, err error) {
apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)

key := []string{}
Expand All @@ -101,10 +99,6 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer
key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]")
}

if expires != "" {
key = append(key, "\"expiresAt\":\""+expires+"\"")
}

if len(attrs) > 0 {
attributes := []string{}
for keyattr, value := range attrs {
Expand All @@ -128,13 +122,14 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer
return respBody, err
}

func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string, expires string) (respBody []byte, err error) {
func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string, scopes []string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)

key := []string{}
key = append(key, "\"apiProducts\":[\""+getArrayStr(apiProducts)+"\"]")
if expires != "" {
key = append(key, "\"expiresAt\":\""+expires+"\"")

if len(scopes) > 0 {
key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]")
}

payload := "{" + strings.Join(key, ",") + "}"
Expand Down

0 comments on commit 685dd23

Please sign in to comment.