feat(ESISSO): refactor callback handling into a dedicated function and improve request logging
This commit is contained in:
74
esi_sso.go
74
esi_sso.go
@@ -229,6 +229,37 @@ func (s *ESISSO) BuildAuthorizeURL() (string, error) {
|
|||||||
return issuerAuthorizeURL + "?" + q.Encode(), nil
|
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, "<html><body><h1>Login successful!</h1><p>You can close this window.</p></body></html>")
|
||||||
|
go func() {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
_ = s.server.Shutdown(context.Background())
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ESISSO) StartCallbackServerAsync() error {
|
func (s *ESISSO) StartCallbackServerAsync() error {
|
||||||
u, err := url.Parse(s.redirectURI)
|
u, err := url.Parse(s.redirectURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -247,35 +278,22 @@ func (s *ESISSO) StartCallbackServerAsync() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
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) {
|
mux.HandleFunc(u.Path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Printf("Callback received: %s %s\n", r.Method, r.URL.String())
|
s.handleCallback(w, r)
|
||||||
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, "<html><body><h1>Login successful!</h1><p>You can close this window.</p></body></html>")
|
|
||||||
go func() {
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
_ = s.server.Shutdown(context.Background())
|
|
||||||
}()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", hostPort)
|
ln, err := net.Listen("tcp", hostPort)
|
||||||
|
|||||||
Reference in New Issue
Block a user