diff --git a/cli/packages/api/api.go b/cli/packages/api/api.go index a414b03d3..7417a2594 100644 --- a/cli/packages/api/api.go +++ b/cli/packages/api/api.go @@ -5,6 +5,7 @@ import ( "github.com/Infisical/infisical-merge/packages/config" "github.com/go-resty/resty/v2" + log "github.com/sirupsen/logrus" ) const USER_AGENT = "cli" @@ -144,3 +145,24 @@ func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesR return workSpacesResponse, nil } + +func CallIsAuthenticated(httpClient *resty.Client) bool { + var workSpacesResponse GetWorkSpacesResponse + response, err := httpClient. + R(). + SetResult(&workSpacesResponse). + SetHeader("User-Agent", USER_AGENT). + Post(fmt.Sprintf("%v/v1/auth/checkAuth", config.INFISICAL_URL)) + + log.Debugln(fmt.Errorf("CallIsAuthenticated: Unsuccessful response: [response=%v]", response)) + + if err != nil { + return false + } + + if response.IsError() { + return false + } + + return true +} diff --git a/cli/packages/cmd/login.go b/cli/packages/cmd/login.go index c10f38fc4..74573c226 100644 --- a/cli/packages/cmd/login.go +++ b/cli/packages/cmd/login.go @@ -33,13 +33,13 @@ var loginCmd = &cobra.Command{ PreRun: toggleDebug, Run: func(cmd *cobra.Command, args []string) { currentLoggedInUserDetails, err := util.GetCurrentLoggedInUserDetails() - if err != nil && strings.Contains(err.Error(), "The specified item could not be found in the keyring") { // if the key can't be found allow them to override + if err != nil && (strings.Contains(err.Error(), "The specified item could not be found in the keyring") || strings.Contains(err.Error(), "unable to get key from Keyring")) { // if the key can't be found allow them to override log.Debug(err) } else if err != nil { util.HandleError(err) } - if currentLoggedInUserDetails.IsUserLoggedIn { + if currentLoggedInUserDetails.IsUserLoggedIn && !currentLoggedInUserDetails.LoginExpired { // if you are logged in but not expired shouldOverride, err := shouldOverrideLoginPrompt(currentLoggedInUserDetails.UserCredentials.Email) if err != nil { util.HandleError(err) diff --git a/cli/packages/util/constants.go b/cli/packages/util/constants.go index 6c21dd787..95b6ca228 100644 --- a/cli/packages/util/constants.go +++ b/cli/packages/util/constants.go @@ -11,5 +11,8 @@ const ( KEYRING_SERVICE_NAME = "infisical" PERSONAL_SECRET_TYPE_NAME = "personal" SHARED_SECRET_TYPE_NAME = "shared" - CLI_VERSION = "v0.2.9" // +) + +var ( + CLI_VERSION = "v0.2.9" ) diff --git a/cli/packages/util/credentials.go b/cli/packages/util/credentials.go index 6410002c0..1766f04c7 100644 --- a/cli/packages/util/credentials.go +++ b/cli/packages/util/credentials.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/99designs/keyring" - "github.com/Infisical/infisical-merge/packages/config" + "github.com/Infisical/infisical-merge/packages/api" "github.com/Infisical/infisical-merge/packages/models" "github.com/go-resty/resty/v2" ) @@ -87,17 +87,10 @@ func GetCurrentLoggedInUserDetails() (LoggedInUserDetails, error) { SetAuthToken(userCreds.JTWToken). SetHeader("Accept", "application/json") - response, err := httpClient. - R(). - Post(fmt.Sprintf("%v/v1/auth/checkAuth", config.INFISICAL_URL)) - - if err != nil { - return LoggedInUserDetails{}, err - } - - if response.StatusCode() > 299 { + isAuthenticated := api.CallIsAuthenticated(httpClient) + if !isAuthenticated { return LoggedInUserDetails{ - IsUserLoggedIn: true, + IsUserLoggedIn: true, // was logged in LoginExpired: true, UserCredentials: userCreds, }, nil