-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial implementation of token refresh * Update dependencies * Add full output to show refresh token
- Loading branch information
1 parent
b808561
commit cd0e29b
Showing
6 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2021 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy | ||
// of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under | ||
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
// OF ANY KIND, either express or implied. See the License for the specific language | ||
// governing permissions and limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/adobe/imscli/ims" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func refreshCmd(imsConfig *ims.Config) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "refresh", | ||
Aliases: []string{"exch"}, | ||
Short: "Exchange a refresh token for new access and refresh tokens.", | ||
Long: "Exchange a refresh token for new access and refresh tokens.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
cmd.SilenceErrors = true | ||
|
||
resp, err := imsConfig.Refresh() | ||
if err != nil { | ||
return fmt.Errorf("error during the token refresh: %v", err) | ||
} | ||
if imsConfig.FullOutput { | ||
data := map[string]interface{}{ | ||
"access_token": resp.AccessToken, | ||
"refresh_token": resp.RefreshToken, | ||
} | ||
jsonData, err := json.MarshalIndent(data, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("error marshalling full JSON response: %v", err) | ||
} | ||
fmt.Printf("%s", 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.RefreshToken, "refreshToken", "t", "", "Refresh token.") | ||
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", []string{""}, | ||
"Scopes to request in the new token. Subset of the scopes of the original token. Optional value, if no "+ | ||
"scopes are requested the same scopes of the original token will be provided.") | ||
cmd.Flags().BoolVarP(&imsConfig.FullOutput, "fullOutput", "F", false, "Output a JSON with access and refresh tokens.") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2021 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy | ||
// of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under | ||
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
// OF ANY KIND, either express or implied. See the License for the specific language | ||
// governing permissions and limitations under the License. | ||
|
||
package ims | ||
|
||
import ( | ||
"fmt" | ||
"github.com/adobe/ims-go/ims" | ||
"time" | ||
) | ||
|
||
func (i Config) validateRefreshConfig() error { | ||
switch { | ||
case i.URL == "": | ||
return fmt.Errorf("missing IMS base URL parameter") | ||
case i.ClientID == "": | ||
return fmt.Errorf("missing client ID parameter") | ||
case i.ClientSecret == "": | ||
return fmt.Errorf("missing client secret parameter") | ||
case i.RefreshToken == "": | ||
return fmt.Errorf("missing refresh token parameter") | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
// Refresh performs the refresh token flow. | ||
func (i Config) Refresh() (RefreshInfo, error) { | ||
|
||
if err := i.validateRefreshConfig(); err != nil { | ||
return RefreshInfo{}, fmt.Errorf("invalid parameters for token refresh: %v", err) | ||
} | ||
|
||
httpClient, err := i.httpClient() | ||
if err != nil { | ||
return RefreshInfo{}, fmt.Errorf("error creating the HTTP Client: %v", err) | ||
} | ||
|
||
c, err := ims.NewClient(&ims.ClientConfig{ | ||
URL: i.URL, | ||
Client: httpClient, | ||
}) | ||
if err != nil { | ||
return RefreshInfo{}, fmt.Errorf("create client: %v", err) | ||
} | ||
|
||
r, err := c.RefreshToken(&ims.RefreshTokenRequest{ | ||
ClientID: i.ClientID, | ||
ClientSecret: i.ClientSecret, | ||
RefreshToken: i.RefreshToken, | ||
Scope: i.Scopes, | ||
}) | ||
if err != nil { | ||
return RefreshInfo{}, fmt.Errorf("error during the token refresh: %v", err) | ||
} | ||
|
||
return RefreshInfo{ | ||
TokenInfo: TokenInfo{ | ||
AccessToken: r.AccessToken, | ||
Expires: int(r.ExpiresIn * time.Second), | ||
}, | ||
RefreshToken: r.RefreshToken, | ||
}, nil | ||
} |