feat(ESISSO): refactor callback handling into a dedicated function and improve request logging
This commit is contained in:
56
esi_sso.go
56
esi_sso.go
@@ -229,25 +229,7 @@ func (s *ESISSO) BuildAuthorizeURL() (string, error) {
|
||||
return issuerAuthorizeURL + "?" + q.Encode(), nil
|
||||
}
|
||||
|
||||
func (s *ESISSO) StartCallbackServerAsync() error {
|
||||
u, err := url.Parse(s.redirectURI)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return errors.New("redirect URI must be http(s)")
|
||||
}
|
||||
hostPort := u.Host
|
||||
if !strings.Contains(hostPort, ":") {
|
||||
if u.Scheme == "https" {
|
||||
hostPort += ":443"
|
||||
} else {
|
||||
hostPort += ":80"
|
||||
}
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc(u.Path, func(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
@@ -276,6 +258,42 @@ func (s *ESISSO) StartCallbackServerAsync() error {
|
||||
time.Sleep(1 * time.Second)
|
||||
_ = s.server.Shutdown(context.Background())
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *ESISSO) StartCallbackServerAsync() error {
|
||||
u, err := url.Parse(s.redirectURI)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return errors.New("redirect URI must be http(s)")
|
||||
}
|
||||
hostPort := u.Host
|
||||
if !strings.Contains(hostPort, ":") {
|
||||
if u.Scheme == "https" {
|
||||
hostPort += ":443"
|
||||
} else {
|
||||
hostPort += ":80"
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
s.handleCallback(w, r)
|
||||
})
|
||||
|
||||
ln, err := net.Listen("tcp", hostPort)
|
||||
|
||||
Reference in New Issue
Block a user