Skip to content

Commit 04c35e6

Browse files
bartonipbartondc
authored andcommitted
Show link to close window on token transmission success
1 parent 48f8801 commit 04c35e6

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

cli/login_callback.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"net/http"
77
"net/url"
8+
"time"
89
)
910

1011
type TokenResponse struct {
@@ -27,6 +28,8 @@ func (h *HandlerWithContext) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2728
if r.URL.Path == "/auth" {
2829
token := r.URL.Query().Get("token")
2930
h.resultChan <- TokenResponse{token: token, err: nil}
31+
_, _ = fmt.Fprintf(w, "<html><head><script>alert('goobers')</script></head></html>")
32+
3033
h.Server.Stop()
3134
}
3235
}
@@ -37,6 +40,11 @@ func NewServer(authURL url.URL, addr string, handler http.Handler) *Server {
3740
httpServer: &http.Server{
3841
Addr: addr,
3942
Handler: handler,
43+
44+
ReadTimeout: 1 * time.Second,
45+
WriteTimeout: 1 * time.Second,
46+
IdleTimeout: 30 * time.Second,
47+
ReadHeaderTimeout: 2 * time.Second,
4048
},
4149
resultChan: make(chan TokenResponse),
4250
stopChan: make(chan struct{}),
@@ -47,21 +55,25 @@ func NewServer(authURL url.URL, addr string, handler http.Handler) *Server {
4755

4856
func (s *Server) Start() {
4957
go func() {
50-
fmt.Printf("HTTP server listening on %s\n", s.httpServer.Addr)
58+
_, _ = fmt.Printf("HTTP server listening on %s\n", s.httpServer.Addr)
5159
if err := s.httpServer.ListenAndServe(); err != http.ErrServerClosed {
5260
s.resultChan <- TokenResponse{token: "", err: err}
5361
}
5462
}()
5563
}
5664

5765
func (s *Server) Stop() {
58-
s.httpServer.Close()
66+
err := s.httpServer.Close()
67+
if err != nil {
68+
_, _ = fmt.Printf("Unable to stop HTTP Server")
69+
}
70+
5971
close(s.stopChan)
6072
}
6173

6274
func (s *Server) Wait() {
6375
<-s.stopChan
64-
fmt.Println("HTTP server stopped.")
76+
_, _ = fmt.Println("HTTP server stopped.")
6577
}
6678

6779
func getListenAddr(port int) string {
@@ -70,7 +82,6 @@ func getListenAddr(port int) string {
7082

7183
func getServer(authURL url.URL) (*Server, error) {
7284
port, err := getAvailablePort()
73-
7485
if err != nil {
7586
return &Server{}, err
7687
}
@@ -79,20 +90,21 @@ func getServer(authURL url.URL) (*Server, error) {
7990

8091
server.Start()
8192

82-
server.authURL.Query().Add("callback", server.GenerateCallbackPath())
93+
values := server.authURL.Query()
94+
values.Add("callback", server.GenerateCallbackPath())
95+
96+
server.authURL.RawQuery = values.Encode()
8397

8498
return server, nil
8599
}
86100

87101
func getAvailablePort() (int, error) {
88102
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
89-
90103
if err != nil {
91104
return 0, err
92105
}
93106

94107
l, err := net.ListenTCP("tcp", addr)
95-
96108
if err != nil {
97109
return 0, err
98110
}

0 commit comments

Comments
 (0)