mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-16 08:14:06 +00:00
feat(cli): warning when logged in and using token at the same time
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Infisical/infisical-merge/packages/config"
|
||||
"github.com/Infisical/infisical-merge/packages/models"
|
||||
)
|
||||
|
||||
type AuthStrategyType string
|
||||
|
||||
var AuthStrategy = struct {
|
||||
@@ -43,3 +50,39 @@ func IsAuthMethodValid(authMethod string, allowUserAuth bool) (isValid bool, str
|
||||
}
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func ShouldUseInfisicalToken(token *models.TokenDetails, validTokenTypes []string) bool {
|
||||
|
||||
if token == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// If nil is passed, we assume both service and universal tokens are acceptable.
|
||||
if validTokenTypes == nil {
|
||||
validTokenTypes = []string{SERVICE_TOKEN_IDENTIFIER, UNIVERSAL_AUTH_TOKEN_IDENTIFIER}
|
||||
}
|
||||
|
||||
for _, tokenType := range validTokenTypes {
|
||||
if token.Type != tokenType {
|
||||
continue
|
||||
}
|
||||
|
||||
details, err := GetCurrentLoggedInUserDetails()
|
||||
if err == nil && details.IsUserLoggedIn && !details.LoginExpired && !config.INFISICAL_SILENT_MODE {
|
||||
|
||||
var usingFrom string
|
||||
if token.PassedAsFlag {
|
||||
usingFrom = "--token flag"
|
||||
} else {
|
||||
usingFrom = "INFISICAL_TOKEN environment variable"
|
||||
}
|
||||
PrintWarning(fmt.Sprintf("You are currently logged in, but the command will be using the token provided from the %s.", usingFrom))
|
||||
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
@@ -87,6 +87,8 @@ func GetInfisicalToken(cmd *cobra.Command) (token *models.TokenDetails, err erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
passedAsFlag := infisicalToken != ""
|
||||
|
||||
if infisicalToken == "" { // If no flag is passed, we first check for the universal auth access token env variable.
|
||||
infisicalToken = os.Getenv(INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN_NAME)
|
||||
|
||||
@@ -101,14 +103,16 @@ func GetInfisicalToken(cmd *cobra.Command) (token *models.TokenDetails, err erro
|
||||
|
||||
if strings.HasPrefix(infisicalToken, "st.") {
|
||||
return &models.TokenDetails{
|
||||
Type: SERVICE_TOKEN_IDENTIFIER,
|
||||
Token: infisicalToken,
|
||||
Type: SERVICE_TOKEN_IDENTIFIER,
|
||||
Token: infisicalToken,
|
||||
PassedAsFlag: passedAsFlag,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &models.TokenDetails{
|
||||
Type: UNIVERSAL_AUTH_TOKEN_IDENTIFIER,
|
||||
Token: infisicalToken,
|
||||
Type: UNIVERSAL_AUTH_TOKEN_IDENTIFIER,
|
||||
Token: infisicalToken,
|
||||
PassedAsFlag: passedAsFlag,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user