(w http.ResponseWriter, r *http.Request)
| 462 | } |
| 463 | |
| 464 | func (h *Handler) discourseRedirect(w http.ResponseWriter, r *http.Request) { |
| 465 | if !h.discourse.Enabled() { |
| 466 | http.Redirect(w, r, "/login", http.StatusFound) |
| 467 | return |
| 468 | } |
| 469 | if r.URL.Query().Get("spa") == "1" { |
| 470 | http.SetCookie(w, &http.Cookie{ |
| 471 | Name: "discourse_spa", |
| 472 | Value: "1", |
| 473 | Path: "/", |
| 474 | MaxAge: 300, |
| 475 | HttpOnly: true, |
| 476 | Secure: true, |
| 477 | SameSite: http.SameSiteLaxMode, |
| 478 | }) |
| 479 | } |
| 480 | scheme := "https" |
| 481 | if r.TLS == nil { |
| 482 | scheme = "http" |
| 483 | } |
| 484 | returnURL := fmt.Sprintf("%s://%s/auth/discourse/callback", scheme, r.Host) |
| 485 | redirectURL, err := h.discourse.BuildRedirectURL(returnURL) |
| 486 | if err != nil { |
| 487 | http.Error(w, "SSO 初始化失败", http.StatusInternalServerError) |
| 488 | return |
| 489 | } |
| 490 | http.Redirect(w, r, redirectURL, http.StatusFound) |
| 491 | } |
| 492 | |
| 493 | func (h *Handler) discourseCallback(w http.ResponseWriter, r *http.Request) { |
| 494 | if !h.discourse.Enabled() { |
nothing calls this directly
no test coverage detected