diff options
-rw-r--r-- | auth/auth.go | 21 | ||||
-rw-r--r-- | auth/client.go | 40 | ||||
-rw-r--r-- | cmd/login.go | 25 | ||||
-rw-r--r-- | cmd/version.go | 2 |
4 files changed, 63 insertions, 25 deletions
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) + } + } + } +} diff --git a/cmd/login.go b/cmd/login.go index 9f12d46..71e4b5a 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. package cmd import ( + "fmt" + "os" "github.com/spf13/cobra" "github.com/MikunoNaka/macli/auth" ) @@ -38,12 +40,29 @@ var loginCmd = &cobra.Command { " - \x1b[31mIf after running `macli login` it opens a dialogue box in the browser asking for credentials,\n and not the MyAnimeList login page, that means you have entered your Client ID wrong.\x1b[0m\n" + "", Run: func(cmd *cobra.Command, args []string) { - auth.Login() + var storeClientId bool + + s, _ := cmd.Flags().GetString("store-client-id") + switch s { + 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") + os.Exit(1) + } + + tk, _ := cmd.Flags().GetString("authentication-token") + clientId, _ := cmd.Flags().GetString("client-id") + + auth.Login(tk, clientId, storeClientId) }, } func init() { rootCmd.AddCommand(loginCmd) - // TODO: save given token to keyring - // rootCmd.Flags().StringVarP(&mal.Secret, "authentication-token", "t", "", "MyAnimeList authentication token to use (overrides system keyring if any)") + 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 (yes/no) (Default: yes)") } diff --git a/cmd/version.go b/cmd/version.go index 4eec975..a1c3ca2 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" ) -const version string = "v1.11.0" +const version string = "no-keyring test" var versionCmd = &cobra.Command { Use: "version", |