Skip to content

Commit

Permalink
Adding refresh option and updating readme to reflect update and refre…
Browse files Browse the repository at this point in the history
…sh commands
  • Loading branch information
hunoz committed Jun 10, 2023
1 parent 7c213ae commit 6ffbc09
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 16 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Init is used to first set up the CLI or to update the client ID, pool ID, and/or
1. Run `spark init` (or `spark init --overwrite` if you need to update any of the client ID, pool ID and/or region)
2. Follow the promps as necessary (or utilize the flags that can be specified via the CLI)

### Update
Update is used to update the CLI to the latest version
1. Run `spark update`

### First Sign In
First Sign In is used if you have been added or created in a Cognito pool but have not performed a first sign in to verify your email and change your password.
1. Run `spark first-sign-in`
Expand All @@ -42,5 +46,8 @@ Reset Password is for if you do not know your current password but have previous
1. Run `spark reset-password`
2. Follow the prompts to reset your password

### Refresh Tokens
Refresh Tokens is for refreshing your access and ID tokens without re-authenticating. This assumes the Cognito default of 30 day expiry for the client.
1. Run `spark refresh`

## Roadmap
* Add `update` subcommand to allow updating from the CLI
34 changes: 34 additions & 0 deletions cmd/refresh/refresh.go
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()

Check failure on line 32 in cmd/refresh/refresh.go

View workflow job for this annotation

GitHub Actions / Get Spark Version

cognitoClient.RefreshTokens undefined (type cognito.CognitoClient has no field or method RefreshTokens)
},
}
18 changes: 11 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
changepassword "github.com/hunoz/spark/cmd/change-password"
firstsignin "github.com/hunoz/spark/cmd/first-sign-in"
cmdInit "github.com/hunoz/spark/cmd/init"
"github.com/hunoz/spark/cmd/refresh"
registertotp "github.com/hunoz/spark/cmd/register-totp"
resetpassword "github.com/hunoz/spark/cmd/reset-password"
"github.com/hunoz/spark/cmd/update"
Expand All @@ -32,11 +33,14 @@ var RootCmd = &cobra.Command{

func init() {
RootCmd.Flags().BoolP("version", "v", false, "Current version of Spark")
RootCmd.AddCommand(auth.AuthCmd)
RootCmd.AddCommand(changepassword.ChangePasswordCmd)
RootCmd.AddCommand(registertotp.RegisterTotpCmd)
RootCmd.AddCommand(resetpassword.ResetPasswordCmd)
RootCmd.AddCommand(firstsignin.FirstSignInCmd)
RootCmd.AddCommand(cmdInit.InitCmd)
RootCmd.AddCommand(update.UpdateCmd)
RootCmd.AddCommand(
auth.AuthCmd,
changepassword.ChangePasswordCmd,
registertotp.RegisterTotpCmd,
resetpassword.ResetPasswordCmd,
firstsignin.FirstSignInCmd,
cmdInit.InitCmd,
update.UpdateCmd,
refresh.RefreshCmd,
)
}
2 changes: 1 addition & 1 deletion cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/fatih/color"
)

var CmdVersion = "v1.0.5"
var CmdVersion = "v1.0.6"

type Release struct {
Url string `json:"url,omitempty"`
Expand Down
53 changes: 53 additions & 0 deletions cognito/refresh.go
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")
}
16 changes: 9 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
)

type CognitoConfig struct {
Region string `json:",omitempty" type:"string"`
ClientId string `json:",omitempty" type:"string"`
PoolId string `json:",omitempty" type:"string"`
AccessToken string `json:",omitempty" type:"string"`
IdToken string `json:",omitempty" type:"string"`
Expires int64 `json:",omitempty" type:"integer"`
Session string `json:",omitempty" type:"string"`
Region string `json:",omitempty" type:"string"`
ClientId string `json:",omitempty" type:"string"`
PoolId string `json:",omitempty" type:"string"`
AccessToken string `json:",omitempty" type:"string"`
IdToken string `json:",omitempty" type:"string"`
RefreshToken string `json:",omitempty" type:"string"`
RefreshTokenExpiry int64 `json:",omitempty" type:"integer"`
Expires int64 `json:",omitempty" type:"integer"`
Session string `json:",omitempty" type:"string"`
}

type Config struct {
Expand Down

0 comments on commit 6ffbc09

Please sign in to comment.