aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-10-04 00:09:34 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-10-04 00:09:34 +0530
commit50f211800946318ff4e3c40c1fc497c149b0380d (patch)
treed4a8f15f05c1b21749593495c99437aad5385b04
parent1b45e30a8f0cbeb8b28a1464dc4342bffa958627 (diff)
Use the macli.toml file to store and get login info automatically
-rw-r--r--auth/auth.go15
-rw-r--r--auth/client.go30
-rw-r--r--auth/token.go44
-rw-r--r--cmd/login.go29
-rw-r--r--macli.toml2
5 files changed, 107 insertions, 13 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)
diff --git a/cmd/login.go b/cmd/login.go
index 39bc7d4..92a4c92 100644
--- a/cmd/login.go
+++ b/cmd/login.go
@@ -22,6 +22,7 @@ import (
"os"
"fmt"
"github.com/spf13/cobra"
+ "github.com/spf13/viper"
"github.com/MikunoNaka/macli/auth"
)
@@ -42,17 +43,38 @@ var loginCmd = &cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
var storeClientId bool
- s, _ := cmd.Flags().GetString("store-client-id")
+ var err error
+ if cmd.Flags().Lookup("no-sys-keyring").Changed {
+ auth.NoSysKeyring, err = cmd.Flags().GetBool("no-sys-keyring")
+ if err != nil {
+ fmt.Println("Error reading \x1b[33m`--no-sys-keyring`\x1b[0m flag:", err.Error())
+ }
+ } else {
+ auth.NoSysKeyring = viper.GetBool("auth.no_system_keyring")
+ }
+
+ var s, errmsg string
+ if cmd.Flags().Lookup("store-client-id").Changed {
+ s, _ = cmd.Flags().GetString("store-client-id")
+ errmsg = "\x1b[33m`--store-client-id`\x1b[0m flag only accepts \x1b[33m\"yes\"\x1b[0m or \x1b[33m\"no\"\x1b[0m"
+ } else {
+ s = viper.GetString("auth.save_client_id")
+ errmsg = fmt.Sprintf("%s: \x1b[33m`auth.save_client_id`\x1b[0m option only accepts \x1b[33m\"yes\"\x1b[0m or \x1b[33m\"no\"\x1b[0m", viper.ConfigFileUsed())
+ }
switch s {
- case "yes":
+ case "yes", "":
storeClientId = true
case "no":
storeClientId = false
default:
- fmt.Println("\x1b[33m`--store-client-id`\x1b[0m flag only accepts \x1b[33m\"yes\"\x1b[0m or \x1b[33m\"no\"\x1b[0m")
+ fmt.Println(errmsg)
os.Exit(1)
}
+ if auth.NoSysKeyring {
+ viper.WriteConfigAs(viper.ConfigFileUsed())
+ }
+
tk, _ := cmd.Flags().GetString("authentication-token")
clientId, _ := cmd.Flags().GetString("client-id")
@@ -65,4 +87,5 @@ func init() {
loginCmd.Flags().StringP("authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)")
loginCmd.Flags().StringP("client-id", "c", "", "MyAnimeList Client ID")
loginCmd.Flags().StringP("store-client-id", "s", "yes", "Save Client ID to keyring")
+ loginCmd.Flags().BoolP("no-sys-keyring", "k", false, "Don't use system keyring to store login info")
}
diff --git a/macli.toml b/macli.toml
index 169addf..69ee259 100644
--- a/macli.toml
+++ b/macli.toml
@@ -20,6 +20,8 @@ token = ""
client_id = ""
# save newly entered client id when logging in (yes/no)
save_client_id = "yes"
+# set this to true if you do not wanna use the system kerying (unsafe)
+no_system_keyring = false # `macli login -k` works similarly
# default settings (can be overridden by corresponding flags, `macli --help` for more details)
[searching]