Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Adds a method to login to the browser #66

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"cli/services"
"fmt"

"github.com/spf13/cobra"
)

var loginCmd = &cobra.Command{
Use: "login",
Short: "Log in to your RunPod account",
Run: func(cmd *cobra.Command, args []string) {
err := services.StartLoginProcess()
if err != nil {
// Handle error
fmt.Println(err)
}
fmt.Println("Login successful.")
},
}

func init() {
startCmd.AddCommand(loginCmd)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func init() {
// RootCmd.AddCommand(copyCmd)
RootCmd.AddCommand(createCmd)
RootCmd.AddCommand(getCmd)
RootCmd.AddCommand(loginCmd)
RootCmd.AddCommand(removeCmd)
RootCmd.AddCommand(startCmd)
RootCmd.AddCommand(stopCmd)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/nbrownus/go-metrics-prometheus v0.0.0-20210712211119-974a6260965f // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -698,6 +700,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
81 changes: 81 additions & 0 deletions services/loginservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package services

import (
"fmt"
"net/http"
"os"
"runtime"

"github.com/pkg/browser"
)

const configFilePath = "./config.txt" // Update this path as needed

func StartLoginProcess() error {
// Open the browser window to the login page
err := openBrowser("https://www.runpod.io/console/login")
if err != nil {
return err
}

// Start a local server to listen for the callback with the auth token
http.HandleFunc("/callback", handleAuthCallback)
fmt.Println("Starting local server for authentication callback. Please ensure your browser allows pop-ups.")
err = http.ListenAndServe(":8080", nil)
if err != nil {
return err
}

return nil
}

func openBrowser(url string) error {
// Handling different operating systems
switch runtime.GOOS {
case "linux":
return browser.OpenURL(url)
case "windows":
return browser.OpenURL(url)
case "darwin":
return browser.OpenURL(url)
default:
return fmt.Errorf("unsupported platform")
}
}

// Callback handler function
func handleAuthCallback(w http.ResponseWriter, r *http.Request) {
// Extract the token from the request
token := r.URL.Query().Get("token")
if token == "" {
http.Error(w, "No token found in the request", http.StatusBadRequest)
return
}

// Store it securely in a config file
err := writeTokenToConfigFile(token)
if err != nil {
http.Error(w, "Failed to store token", http.StatusInternalServerError)
return
}

fmt.Println("Received token:", token)

// Respond to the request
fmt.Fprintf(w, "Authentication successful. You can close this window.")
}

func writeTokenToConfigFile(token string) error {
file, err := os.OpenFile(configFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()

_, err = file.WriteString(token)
return err
}

func DeleteConfigFile() error {
return os.Remove(configFilePath)
}