-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,12 @@ const ( | |
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN_NAME = "INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN" | ||
INFISICAL_VAULT_FILE_PASSPHRASE_ENV_NAME = "INFISICAL_VAULT_FILE_PASSPHRASE" // This works because we've forked the keyring package and added support for this env variable. This explains why you won't find any occurrences of it in the CLI codebase. | ||
|
||
// JWT configuration | ||
INFISICAL_JWT_EXPIRATION_NAME = "INFISICAL_JWT_EXPIRATION" | ||
DEFAULT_JWT_EXPIRATION = 86400 // 24 hours in seconds | ||
MIN_JWT_EXPIRATION = 3600 // 1 hour in seconds | ||
MAX_JWT_EXPIRATION = 2592000 // 30 days in seconds | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
aaryan182
Author
Owner
|
||
VAULT_BACKEND_AUTO_MODE = "auto" | ||
VAULT_BACKEND_FILE_MODE = "file" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Infisical/infisical-merge/packages/api" | ||
"github.com/Infisical/infisical-merge/packages/models" | ||
"github.com/go-resty/resty/v2" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// RefreshJWTToken attempts to refresh an expired JWT token using the refresh token | ||
func RefreshJWTToken(httpClient *resty.Client, refreshToken string) (string, error) { | ||
if refreshToken == "" { | ||
return "", fmt.Errorf("no refresh token available") | ||
} | ||
|
||
accessTokenResponse, err := api.CallGetNewAccessTokenWithRefreshToken(httpClient, refreshToken) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to refresh token: %w", err) | ||
} | ||
|
||
if accessTokenResponse.Token == "" { | ||
return "", fmt.Errorf("received empty token from refresh attempt") | ||
} | ||
|
||
return accessTokenResponse.Token, nil | ||
} | ||
|
||
// IsTokenExpired checks if the given token is expired by validating with the server | ||
func IsTokenExpired(httpClient *resty.Client) bool { | ||
return !api.CallIsAuthenticated(httpClient) | ||
} | ||
|
||
// HandleTokenRefresh handles the complete token refresh flow | ||
func HandleTokenRefresh(userCreds *models.UserCredentials) error { | ||
httpClient := resty.New(). | ||
SetAuthToken(userCreds.JWTToken). | ||
SetHeader("Accept", "application/json") | ||
|
||
if IsTokenExpired(httpClient) && userCreds.RefreshToken != "" { | ||
newToken, err := RefreshJWTToken(httpClient, userCreds.RefreshToken) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userCreds.JWTToken = newToken | ||
err = StoreUserCredsInKeyRing(userCreds) | ||
if err != nil { | ||
log.Debug().Msg("unable to store refreshed credentials in keyring") | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
This comment has been minimized.
Sorry, something went wrong.
aaryan182
Author
Owner
|
||
} |
Fixed JWT Token Typo
This commit addressed a typographical error where "JWTTOKEN" was incorrectly spelled as "JTWTOKEN". This misspelling could have potentially caused issues with token handling. The code has been corrected to ensure proper JWT token functionality.