From 5686c94f5fc24f7ec6927ab4b80b30d8644fba8f Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 16 Aug 2022 13:35:50 +0530 Subject: reading Client ID and/or Auth Token from flags in login command --- auth/auth.go | 21 ++++++++++++++++++--- auth/client.go | 40 ++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'auth') diff --git a/auth/auth.go b/auth/auth.go index 5955073..be17271 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -28,7 +28,7 @@ import ( "github.com/zalando/go-keyring" ) -var serviceName string = "macli" +var serviceName string = "macliTesting" // "macli" var userName string func init() { @@ -42,7 +42,7 @@ func init() { } // asks for all the details -func Login() { +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 */ @@ -54,7 +54,22 @@ func Login() { } } - clientId := askClientId() + if clientId == "" { + clientId = askClientId(storeClientId) + } else { + validateClientId(clientId) + if storeClientId { + setClientId(clientId) + } + } + + if tk != "" { + setToken(tk) + fmt.Println("\x1b[32mYou have successfully logged into macli.\x1b[0m") + fmt.Println("\x1b[32mYou can close the web browser tab now.\x1b[0m") + return + } + challenge := codeChallenge() link := generateLink(clientId, challenge) diff --git a/auth/client.go b/auth/client.go index e123086..6ec75ca 100644 --- a/auth/client.go +++ b/auth/client.go @@ -51,7 +51,7 @@ func deleteClientId() { // if client id isn't in keyring // it will ask the user to enter/create one -func askClientId() string { +func askClientId(storeClientId bool) string { clientId, err := getClientId() if err != nil { if err.Error() == "secret not found in keyring" { @@ -61,24 +61,10 @@ 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) - } - } + validateClientId(clientId) + if storeClientId { + setClientId(clientId) } - - setClientId(clientId) } else { fmt.Println("Error while reading Client ID from keychain:", err) os.Exit(1) @@ -87,3 +73,21 @@ func askClientId() string { return clientId } + +func validateClientId(clientId string) { + /* 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) + } + } + } +} -- cgit v1.2.3