From 34d9dcbfc608ed97e28a2069f759518ba484fbab Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 6 Jul 2022 13:24:54 +0530 Subject: added confirmation messages when re-logging in, etc --- auth/auth.go | 28 +++++++++++++++++++++------- auth/client.go | 22 ++++++++++++++++++++-- auth/input.go | 12 +++++++++--- 3 files changed, 50 insertions(+), 12 deletions(-) (limited to 'auth') diff --git a/auth/auth.go b/auth/auth.go index e1a9574..5955073 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -25,6 +25,7 @@ import ( "runtime" "errors" "fmt" + "github.com/zalando/go-keyring" ) var serviceName string = "macli" @@ -42,6 +43,17 @@ func init() { // asks for all the details func Login() { + /* 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) + if existingToken != "" { + if !confirmInput("Already logged in. Log in again? [Y/n] ", true) { + fmt.Println("Login process aborted") + os.Exit(0) + } + } + clientId := askClientId() challenge := codeChallenge() link := generateLink(clientId, challenge) @@ -61,16 +73,12 @@ func openInBrowser(url string) { switch runtime.GOOS { case "linux": err = exec.Command("xdg-open", url).Start() - break case "windows": err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() - break case "darwin": err = exec.Command("open", url).Start() - break default: err = errors.New("") - break } if err != nil { @@ -81,12 +89,18 @@ func openInBrowser(url string) { } func Logout() { + existingToken, _ := keyring.Get(serviceName, userName) deleteToken() deleteExpiresIn() deleteRefreshToken() - fmt.Println("Deleted user credentials.") - if confirmInput("Delete your Client ID? [y/N] ") { - fmt.Println("Deleting Client ID...") + if existingToken != "" { + fmt.Println("Deleted user credentials.") + } + + // only ask to delete Client ID if it actually exists + existingClientId, _ := getClientId() + if existingClientId != "" && confirmInput("Delete your Client ID? [y/N] ", false) { deleteClientId() + fmt.Println("Deleted Client ID.") } } diff --git a/auth/client.go b/auth/client.go index 7cb781f..e123086 100644 --- a/auth/client.go +++ b/auth/client.go @@ -61,10 +61,28 @@ func askClientId() string { // get clientId from user input clientId = secretInput("Enter your Client ID: ", "Client ID Can't be blank") + + /* I'm not sure if ALL client IDs are 32 characters + * but that's most likely the case */ + if len(clientId) != 32 { + fmt.Println("\x1b[33mWarning:\x1b[0m The Client ID you have entered doesn't have 32 characters.") + fmt.Println("It's not confirmed but MyAnimeList Client IDs have 32 characters. If you think this is a mistake, you can manually verify your Client ID.") + fmt.Println("macli doesn't have a way to verify a Client ID. If you think you entered it correctly you can move on with the login process. If you have problems do consider re-entering the Client ID.") + + if confirmInput("Show entered Client ID? [Y/n] ", true) { + fmt.Println("The Client ID you just entered:", clientId) + if !confirmInput("Is this correct? [Y/n] ", true) { + fmt.Println("Please verify your Client ID and run \x1b[33m`macli login`\x1b[0m again.") + os.Exit(1) + } + } + } + setClientId(clientId) + } else { + fmt.Println("Error while reading Client ID from keychain:", err) + os.Exit(1) } - fmt.Println("Error while reading Client ID from keychain:", err) - os.Exit(1) } return clientId diff --git a/auth/input.go b/auth/input.go index aa56f52..42b5907 100644 --- a/auth/input.go +++ b/auth/input.go @@ -54,11 +54,12 @@ func secretInput(label, errMessage string) string { os.Exit(1) } - return res + // trim leading and trailing whitespaces + return strings.TrimSpace(res) } // ask yes/no with no as default -func confirmInput(label string) bool { +func confirmInput(label string, def bool) bool { validResponses := []string{"y", "yes", "n", "no", ""} validate := func(input string) error { @@ -89,9 +90,14 @@ func confirmInput(label string) bool { os.Exit(1) } - if res == "y" || res == "yes" { + resp := strings.TrimSpace(strings.ToLower(res)) + if resp == "y" || resp == "yes" { return true } + if resp == "" { + return def + } + return false } -- cgit v1.2.3