Skip to content

Commit

Permalink
feat: adds support for key expiry #321
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Oct 20, 2023
1 parent 0f18af6 commit eeae278
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd/apps/createkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package apps

import (
"fmt"

Check failure on line 18 in cmd/apps/createkey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 18 in cmd/apps/createkey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"internal/apiclient"
"strconv"

Check failure on line 20 in cmd/apps/createkey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 20 in cmd/apps/createkey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

"internal/client/apps"

Expand All @@ -31,7 +33,10 @@ var CreateKeyCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
_, err = apps.CreateKey(developerEmail, name, key, secret, apiProducts, scopes, attrs)
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 @@ -45,6 +50,8 @@ func init() {
[]string{}, "A list of api products")
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")
CreateKeyCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")

Expand Down
9 changes: 8 additions & 1 deletion cmd/apps/updatekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package apps

import (
"fmt"

Check failure on line 18 in cmd/apps/updatekey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 18 in cmd/apps/updatekey.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"internal/apiclient"
"strconv"

"internal/client/apps"

Expand All @@ -31,7 +33,10 @@ var UpdateKeyCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
_, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, attrs)
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)
return
},
}
Expand All @@ -45,6 +50,8 @@ func init() {
[]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")

Expand Down
12 changes: 10 additions & 2 deletions internal/client/apps/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

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

key := []string{}
Expand All @@ -43,6 +43,10 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer
key = append(key, "\"consumerKey\":\""+consumerKey+"\"")
key = append(key, "\"consumerSecret\":\""+consumerSecret+"\"")

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

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

u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerEmail, "apps", appID, "keys")
Expand Down Expand Up @@ -83,7 +87,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, attrs map[string]string) (respBody []byte, err error) {
func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string, apiProducts []string, scopes []string, expires string, attrs map[string]string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)

key := []string{}
Expand All @@ -96,6 +100,10 @@ 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 Down

0 comments on commit eeae278

Please sign in to comment.