diff --git a/esi_sso.go b/esi_sso.go index 3e0508c..951d309 100644 --- a/esi_sso.go +++ b/esi_sso.go @@ -229,6 +229,37 @@ func (s *ESISSO) BuildAuthorizeURL() (string, error) { return issuerAuthorizeURL + "?" + q.Encode(), nil } +func (s *ESISSO) handleCallback(w http.ResponseWriter, r *http.Request) { + fmt.Printf("Callback received: %s %s\n", r.Method, r.URL.String()) + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } + q := r.URL.Query() + code := q.Get("code") + st := q.Get("state") + if code == "" || st == "" || st != s.state { + fmt.Printf("Invalid SSO response: code=%s, state=%s, expected_state=%s\n", code, st, s.state) + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte("Invalid SSO response")) + return + } + fmt.Printf("Exchanging token for code: %s\n", code) + if err := s.exchangeToken(r.Context(), code); err != nil { + fmt.Printf("Token exchange failed: %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte("Token exchange failed: " + err.Error())) + return + } + fmt.Printf("Login successful for character: %s (%d)\n", s.characterName, s.characterID) + w.Header().Set("Content-Type", "text/html") + _, _ = io.WriteString(w, "

Login successful!

You can close this window.

") + go func() { + time.Sleep(1 * time.Second) + _ = s.server.Shutdown(context.Background()) + }() +} + func (s *ESISSO) StartCallbackServerAsync() error { u, err := url.Parse(s.redirectURI) if err != nil { @@ -247,35 +278,22 @@ func (s *ESISSO) StartCallbackServerAsync() error { } mux := http.NewServeMux() + + // Add a catch-all handler to debug what's being requested + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Printf("DEBUG: Request received - Method: %s, URL: %s, Path: %s\n", r.Method, r.URL.String(), r.URL.Path) + if r.URL.Path == u.Path { + // This is our callback, handle it + s.handleCallback(w, r) + } else { + fmt.Printf("DEBUG: 404 - Path %s does not match expected %s\n", r.URL.Path, u.Path) + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte("Not found")) + } + }) + mux.HandleFunc(u.Path, func(w http.ResponseWriter, r *http.Request) { - fmt.Printf("Callback received: %s %s\n", r.Method, r.URL.String()) - if r.Method != http.MethodGet { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } - q := r.URL.Query() - code := q.Get("code") - st := q.Get("state") - if code == "" || st == "" || st != s.state { - fmt.Printf("Invalid SSO response: code=%s, state=%s, expected_state=%s\n", code, st, s.state) - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte("Invalid SSO response")) - return - } - fmt.Printf("Exchanging token for code: %s\n", code) - if err := s.exchangeToken(r.Context(), code); err != nil { - fmt.Printf("Token exchange failed: %v\n", err) - w.WriteHeader(http.StatusInternalServerError) - _, _ = w.Write([]byte("Token exchange failed: " + err.Error())) - return - } - fmt.Printf("Login successful for character: %s (%d)\n", s.characterName, s.characterID) - w.Header().Set("Content-Type", "text/html") - _, _ = io.WriteString(w, "

Login successful!

You can close this window.

") - go func() { - time.Sleep(1 * time.Second) - _ = s.server.Shutdown(context.Background()) - }() + s.handleCallback(w, r) }) ln, err := net.Listen("tcp", hostPort)