diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-10-04 00:09:34 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-10-04 00:09:34 +0530 |
commit | 50f211800946318ff4e3c40c1fc497c149b0380d (patch) | |
tree | d4a8f15f05c1b21749593495c99437aad5385b04 /auth/token.go | |
parent | 1b45e30a8f0cbeb8b28a1464dc4342bffa958627 (diff) |
Use the macli.toml file to store and get login info automatically
Diffstat (limited to 'auth/token.go')
-rw-r--r-- | auth/token.go | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/auth/token.go b/auth/token.go index fb1073f..5573231 100644 --- a/auth/token.go +++ b/auth/token.go @@ -22,10 +22,12 @@ import ( "os" "fmt" "github.com/zalando/go-keyring" + "github.com/spf13/viper" ) var refreshPrefix string = "-refresh-token" var expiresPrefix string = "-expires-in" +var NoSysKeyring bool func GetToken() string { secret, err := keyring.Get(serviceName, userName) @@ -39,17 +41,29 @@ func GetToken() string { } func setToken(secret string) { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.token", secret) + viper.Set("auth.no_system_keyring", true) + return + } + err := keyring.Set(serviceName, userName, secret) if err != nil { - fmt.Println("Error while writing access token to keychain", err) + fmt.Println("Error while writing access token to keychain", err.Error()) os.Exit(1) } } func deleteToken() { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.token", "") + } + err := keyring.Delete(serviceName, userName) // if secret doesnt exist dont show error - if err != nil { + if err != nil && !NoSysKeyring { if err.Error() != "secret not found in keyring" { fmt.Println("Error while deleting authentication token", err.Error()) os.Exit(1) @@ -59,6 +73,12 @@ func deleteToken() { // currently refreshtoken has no use func setRefreshToken(secret string) { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.refresh", secret) + return + } + err := keyring.Set(serviceName + refreshPrefix, userName, secret) if err != nil { fmt.Println("Error while writing access token to keychain", err) @@ -78,9 +98,14 @@ func getRefreshToken() string { } func deleteRefreshToken() { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.refresh", "") + } + err := keyring.Delete(serviceName + refreshPrefix, userName) // if secret doesnt exist dont show error - if err != nil { + if err != nil && !NoSysKeyring { if err.Error() != "secret not found in keyring" { fmt.Println("Error while deleting refresh token", err.Error()) os.Exit(1) @@ -89,6 +114,12 @@ func deleteRefreshToken() { } func setExpiresIn(secret string) { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.refresh", secret) + return + } + err := keyring.Set(serviceName + expiresPrefix, userName, secret) if err != nil { fmt.Println("Error while writing token expire time to keychain", err) @@ -108,9 +139,14 @@ func getExpiresIn() string { } func deleteExpiresIn() { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.refresh", "") + } + err := keyring.Delete(serviceName + expiresPrefix, userName) // if secret doesnt exist dont show error - if err != nil { + if err != nil && !NoSysKeyring { if err.Error() != "secret not found in keyring" { fmt.Println("Error while deleting token expires in data", err.Error()) os.Exit(1) |