-
Notifications
You must be signed in to change notification settings - Fork 10
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(account): add login command #375
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package account | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/UpCloudLtd/progress/messages" | ||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" | ||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/account/tokenreceiver" | ||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output" | ||
"github.com/zalando/go-keyring" | ||
) | ||
|
||
// LoginCommand creates the "account login" command | ||
func LoginCommand() commands.Command { | ||
return &loginCommand{ | ||
BaseCommand: commands.New( | ||
"login", | ||
"Configure a authentication token.", | ||
"upctl account login", | ||
), | ||
} | ||
} | ||
|
||
type loginCommand struct { | ||
*commands.BaseCommand | ||
} | ||
|
||
// ExecuteWithoutArguments implements commands.NoArgumentCommand | ||
func (s *loginCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) { | ||
msg := "Waiting to receive token from browser." | ||
exec.PushProgressStarted(msg) | ||
|
||
receiver := tokenreceiver.New() | ||
err := receiver.Start() | ||
if err != nil { | ||
return commands.HandleError(exec, msg, err) | ||
} | ||
|
||
err = receiver.OpenBrowser() | ||
if err != nil { | ||
url := receiver.GetLoginURL() | ||
exec.PushProgressUpdate(messages.Update{ | ||
Message: "Failed to open browser.", | ||
Status: messages.MessageStatusError, | ||
Details: fmt.Sprintf("Please open a browser and navigate to %s to continue with the login.", url), | ||
}) | ||
} | ||
|
||
token, err := receiver.Wait(exec.Context()) | ||
if err != nil { | ||
return commands.HandleError(exec, msg, err) | ||
} | ||
|
||
err = keyring.Set("UpCloud", "", token) | ||
if err != nil { | ||
return commands.HandleError(exec, msg, fmt.Errorf("failed to save token to keyring: %w", err)) | ||
} | ||
|
||
exec.PushProgressSuccess(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update progress message to something like |
||
|
||
return output.None{}, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should maybe output the token if saving to keyring fails 🤔 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package tokenreceiver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/cli/browser" | ||
) | ||
|
||
type ReceiverServer struct { | ||
server *http.Server | ||
token string | ||
port string | ||
} | ||
|
||
func New() *ReceiverServer { | ||
return &ReceiverServer{} | ||
} | ||
|
||
func getPort(listener net.Listener) string { | ||
_, port, _ := net.SplitHostPort(listener.Addr().String()) | ||
return port | ||
} | ||
|
||
func getURL(target string) string { | ||
return fmt.Sprintf("http://localhost:3000/account/upctl-login/%s", target) | ||
} | ||
|
||
func (s *ReceiverServer) GetLoginURL() string { | ||
return getURL(s.port) | ||
} | ||
|
||
func (s *ReceiverServer) Start() error { | ||
handler := http.NewServeMux() | ||
handler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | ||
token := req.URL.Query().Get("token") | ||
if token == "" { | ||
http.Redirect(w, req, getURL("error"), http.StatusSeeOther) | ||
return | ||
} | ||
s.token = token | ||
http.Redirect(w, req, getURL("success"), http.StatusSeeOther) | ||
Comment on lines
+40
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the token be delivered with fetch request instead of redirects? |
||
}) | ||
|
||
listener, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
return fmt.Errorf("failed to create receiver server: %w", err) | ||
} | ||
|
||
go func() { | ||
defer listener.Close() | ||
s.server = &http.Server{ | ||
Handler: handler, | ||
ReadHeaderTimeout: time.Second, | ||
} | ||
_ = s.server.Serve(listener) | ||
}() | ||
s.port = getPort(listener) | ||
return nil | ||
} | ||
|
||
func (s *ReceiverServer) OpenBrowser() error { | ||
return browser.OpenURL(s.GetLoginURL()) | ||
} | ||
|
||
func (s *ReceiverServer) Wait(ctx context.Context) (string, error) { | ||
ticker := time.NewTicker(time.Second * 2) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
_ = s.server.Shutdown(context.TODO()) | ||
return "", ctx.Err() | ||
case <-ticker.C: | ||
if s.token != "" { | ||
_ = s.server.Shutdown(context.TODO()) | ||
return s.token, nil | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to use config file as fallback? I guess the file should be generic to be able to use it also with e.g. Terraform provider 🤔