|
| 1 | +package yaakcli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/browser" |
| 7 | + "github.com/pterm/pterm" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "os/signal" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +var loginCmd = &cobra.Command{ |
| 17 | + Use: "login", |
| 18 | + Short: "Login to Yaak via web browser", |
| 19 | + Long: "Open a web browser to authenticate with Yaak. Works with all browsers including Safari.", |
| 20 | + Run: func(cmd *cobra.Command, args []string) { |
| 21 | + CheckError(deleteAuthToken()) |
| 22 | + |
| 23 | + pterm.Info.Println("Starting browser-based login...") |
| 24 | + |
| 25 | + // Save the token to a config file |
| 26 | + confirm := pterm.DefaultInteractiveConfirm |
| 27 | + confirm.DefaultValue = true |
| 28 | + open, err := confirm.Show("Open default browser") |
| 29 | + CheckError(err) |
| 30 | + |
| 31 | + if !open { |
| 32 | + os.Exit(0) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // Create a channel to receive the auth token |
| 37 | + tokenChan := make(chan string, 1) |
| 38 | + |
| 39 | + // Set up a simple HTTP server to handle the OAuth callback |
| 40 | + server := &http.Server{ |
| 41 | + Addr: "localhost:8085", |
| 42 | + } |
| 43 | + |
| 44 | + // Define the handler for the callback |
| 45 | + http.HandleFunc("/oauth/callback", func(w http.ResponseWriter, r *http.Request) { |
| 46 | + // Get the token from the query parameters |
| 47 | + token := r.URL.Query().Get("token") |
| 48 | + if token == "" { |
| 49 | + w.WriteHeader(http.StatusBadRequest) |
| 50 | + _, _ = fmt.Fprintf(w, "Error: No token provided") |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // Send the token to the channel |
| 55 | + tokenChan <- token |
| 56 | + |
| 57 | + // Return a success message to the browser |
| 58 | + redirectTo := prodStagingDevStr( |
| 59 | + "https://yaak.app/login-cli/success", |
| 60 | + "https://todo.yaak.app/login-cli/success", |
| 61 | + "http://localhost:9444/login-cli/success", |
| 62 | + ) |
| 63 | + http.Redirect(w, r, redirectTo, http.StatusFound) |
| 64 | + }) |
| 65 | + |
| 66 | + // Start the server in a goroutine |
| 67 | + go func() { |
| 68 | + if err := server.ListenAndServe(); err != http.ErrServerClosed { |
| 69 | + pterm.Error.Printf("HTTP server error: %v\n", err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + // Set up a signal handler to gracefully shut down the server |
| 75 | + sigChan := make(chan os.Signal, 1) |
| 76 | + signal.Notify(sigChan, os.Interrupt) |
| 77 | + |
| 78 | + // Open the browser to the login page |
| 79 | + redirect := "http://localhost:8085/oauth/callback" |
| 80 | + loginURL := prodStagingDevStr( |
| 81 | + "https://yaak.app/login-cli?redirect=", |
| 82 | + "https://todo.yaak.app/login-cli?redirect=", |
| 83 | + "http://localhost:9444/login-cli?redirect=", |
| 84 | + ) + url.QueryEscape(redirect) |
| 85 | + |
| 86 | + // Open the browser based on the operating system |
| 87 | + err = browser.OpenURL(loginURL) |
| 88 | + if err != nil { |
| 89 | + pterm.Error.Printf("Failed to open browser: %v\n", err) |
| 90 | + pterm.Info.Println("Please open the following URL manually:") |
| 91 | + pterm.Info.Println(loginURL) |
| 92 | + } |
| 93 | + |
| 94 | + // Wait for either the token, a signal, or a timeout |
| 95 | + pterm.Info.Println("Waiting for authentication...") |
| 96 | + |
| 97 | + select { |
| 98 | + case token := <-tokenChan: |
| 99 | + pterm.Success.Println("Authentication successful!") |
| 100 | + |
| 101 | + // set password |
| 102 | + err = storeAuthToken(token) |
| 103 | + CheckError(err) |
| 104 | + |
| 105 | + // Shutdown the server |
| 106 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 107 | + defer cancel() |
| 108 | + if err := server.Shutdown(ctx); err != nil { |
| 109 | + pterm.Error.Printf("Server shutdown error: %v\n", err) |
| 110 | + } |
| 111 | + |
| 112 | + case <-sigChan: |
| 113 | + pterm.Warning.Println("Interrupted by user. Shutting down...") |
| 114 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 115 | + defer cancel() |
| 116 | + if err := server.Shutdown(ctx); err != nil { |
| 117 | + pterm.Error.Printf("Server shutdown error: %v\n", err) |
| 118 | + } |
| 119 | + |
| 120 | + case <-time.After(5 * time.Minute): |
| 121 | + pterm.Warning.Println("Timeout waiting for authentication. Shutting down...") |
| 122 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 123 | + defer cancel() |
| 124 | + if err := server.Shutdown(ctx); err != nil { |
| 125 | + pterm.Error.Printf("Server shutdown error: %v\n", err) |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | +} |
0 commit comments