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 | |
parent | 1b45e30a8f0cbeb8b28a1464dc4342bffa958627 (diff) |
Use the macli.toml file to store and get login info automatically
Diffstat (limited to 'auth')
-rw-r--r-- | auth/auth.go | 15 | ||||
-rw-r--r-- | auth/client.go | 30 | ||||
-rw-r--r-- | auth/token.go | 44 |
3 files changed, 79 insertions, 10 deletions
diff --git a/auth/auth.go b/auth/auth.go index 5a768ff..15ad854 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -26,6 +26,7 @@ import ( "errors" "fmt" "github.com/zalando/go-keyring" + "github.com/spf13/viper" ) var serviceName string = "macli" @@ -43,10 +44,16 @@ func init() { // asks for all the details func Login(tk, clientId string, storeClientId bool) { - /* check if an auth token already exists - * if there is an error with keyring, askClientId would handle it - * can safely ignore error here */ - existingToken, _ := keyring.Get(serviceName, userName) + // check if an auth token already exists + var existingToken string + if NoSysKeyring { + existingToken = viper.GetString("auth.token") + } else { + /* if there is an error with keyring, askClientId would handle it + * can safely ignore error here */ + existingToken, _ = keyring.Get(serviceName, userName) + } + if existingToken != "" { if !confirmInput("Already logged in. Log in again? [Y/n] ", true) { fmt.Println("Login process aborted") diff --git a/auth/client.go b/auth/client.go index 6ec75ca..5a3e501 100644 --- a/auth/client.go +++ b/auth/client.go @@ -22,15 +22,36 @@ import ( "os" "fmt" "github.com/zalando/go-keyring" + "github.com/spf13/viper" + "errors" ) var clientSuffix string = "-client-id" func getClientId() (string, error) { - return keyring.Get(serviceName + clientSuffix, userName) + var id string + var err error + + + if NoSysKeyring { + id = viper.GetString("auth.client_id") + if id == "" { + err = errors.New("secret not found in keyring") + } + } else { + id, err = keyring.Get(serviceName + clientSuffix, userName) + } + + return id, err } func setClientId(clientId string) { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.client_id", clientId) + return + } + err := keyring.Set(serviceName + clientSuffix, userName, clientId) if err != nil { fmt.Println("Error while writing Client ID to keychain", err) @@ -39,9 +60,14 @@ func setClientId(clientId string) { } func deleteClientId() { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.client_id", "") + } + err := keyring.Delete(serviceName + clientSuffix, 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 Client ID", err.Error()) os.Exit(1) 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) |