-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding refresh option and updating readme to reflect update and refre…
…sh commands
- Loading branch information
Showing
6 changed files
with
116 additions
and
16 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
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,34 @@ | ||
package refresh | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/hunoz/spark/cognito" | ||
"github.com/hunoz/spark/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var RefreshCmd = &cobra.Command{ | ||
Use: "refresh", | ||
Short: "Refresh Cognito access and ID tokens", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var configuration *config.CognitoConfig | ||
config.CheckIfCognitoIsInitialized() | ||
if config, e := config.GetCognitoConfig(); e != nil { | ||
if strings.Contains(e.Error(), "Invalid region") { | ||
color.Red("Spark has not been initialized. Please run 'spark init' to initialize Spark.") | ||
} else { | ||
color.Red("Error getting config: %v", e.Error()) | ||
} | ||
os.Exit(1) | ||
} else { | ||
configuration = config | ||
} | ||
|
||
cognitoClient := cognito.New(configuration) | ||
|
||
cognitoClient.RefreshTokens() | ||
}, | ||
} |
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,53 @@ | ||
package cognito | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types" | ||
"github.com/fatih/color" | ||
"github.com/hunoz/spark/config" | ||
) | ||
|
||
func refreshTokens(client cognitoidentityprovider.Client, configuration *config.CognitoConfig) { | ||
if configuration.RefreshToken == "" { | ||
color.Red("Refresh token is missing, please run 'spark auth' to authenticate and obtain a refresh token") | ||
return | ||
} else if time.Until(time.Unix(configuration.RefreshTokenExpiry, 0)) <= 0 { | ||
color.Red("Refresh token is expired, please run 'spark auth' to obtain a new refresh token") | ||
return | ||
} | ||
|
||
input := cognitoidentityprovider.InitiateAuthInput{ | ||
AuthFlow: types.AuthFlowTypeRefreshTokenAuth, | ||
AuthParameters: map[string]string{ | ||
"REFRESH_TOKEN": configuration.RefreshToken, | ||
}, | ||
ClientId: aws.String(configuration.ClientId), | ||
} | ||
|
||
response := callCognitoInitiateAuth(client, input, false) | ||
|
||
now := time.Now() | ||
|
||
cognitoConfig := config.CognitoConfig{ | ||
ClientId: configuration.ClientId, | ||
Region: configuration.Region, | ||
PoolId: configuration.PoolId, | ||
AccessToken: *response.AuthenticationResult.AccessToken, | ||
IdToken: *response.AuthenticationResult.IdToken, | ||
RefreshToken: configuration.RefreshToken, | ||
RefreshTokenExpiry: configuration.RefreshTokenExpiry, | ||
Expires: now.Add(time.Second * time.Duration(response.AuthenticationResult.ExpiresIn)).Unix(), | ||
Session: configuration.Session, | ||
} | ||
|
||
if err := config.UpdateCognitoConfig(cognitoConfig); err != nil { | ||
color.Red("Error updating tokens: %v", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
color.Green("Successfully updated tokens") | ||
} |
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