login_check.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "log/slog"
  6. "net/http"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/http/cookie"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response/html"
  11. "miniflux.app/v2/internal/http/route"
  12. "miniflux.app/v2/internal/locale"
  13. "miniflux.app/v2/internal/ui/form"
  14. "miniflux.app/v2/internal/ui/session"
  15. "miniflux.app/v2/internal/ui/view"
  16. )
  17. func (h *handler) checkLogin(w http.ResponseWriter, r *http.Request) {
  18. clientIP := request.ClientIP(r)
  19. sess := session.New(h.store, request.SessionID(r))
  20. authForm := form.NewAuthForm(r)
  21. view := view.New(h.tpl, r, sess)
  22. view.Set("errorMessage", locale.NewLocalizedError("error.bad_credentials").Translate(request.UserLanguage(r)))
  23. view.Set("form", authForm)
  24. if validationErr := authForm.Validate(); validationErr != nil {
  25. translatedErrorMessage := validationErr.Translate(request.UserLanguage(r))
  26. slog.Warn("Validation error during login check",
  27. slog.Bool("authentication_failed", true),
  28. slog.String("client_ip", clientIP),
  29. slog.String("user_agent", r.UserAgent()),
  30. slog.String("username", authForm.Username),
  31. slog.Any("error", translatedErrorMessage),
  32. )
  33. html.OK(w, r, view.Render("login"))
  34. return
  35. }
  36. if err := h.store.CheckPassword(authForm.Username, authForm.Password); err != nil {
  37. slog.Warn("Incorrect username or password",
  38. slog.Bool("authentication_failed", true),
  39. slog.String("client_ip", clientIP),
  40. slog.String("user_agent", r.UserAgent()),
  41. slog.String("username", authForm.Username),
  42. slog.Any("error", err),
  43. )
  44. html.OK(w, r, view.Render("login"))
  45. return
  46. }
  47. sessionToken, userID, err := h.store.CreateUserSessionFromUsername(authForm.Username, r.UserAgent(), clientIP)
  48. if err != nil {
  49. html.ServerError(w, r, err)
  50. return
  51. }
  52. slog.Info("User authenticated successfully with username/password",
  53. slog.Bool("authentication_successful", true),
  54. slog.String("client_ip", clientIP),
  55. slog.String("user_agent", r.UserAgent()),
  56. slog.Int64("user_id", userID),
  57. slog.String("username", authForm.Username),
  58. )
  59. h.store.SetLastLogin(userID)
  60. user, err := h.store.UserByID(userID)
  61. if err != nil {
  62. html.ServerError(w, r, err)
  63. return
  64. }
  65. sess.SetLanguage(user.Language)
  66. sess.SetTheme(user.Theme)
  67. http.SetCookie(w, cookie.New(
  68. cookie.CookieUserSessionID,
  69. sessionToken,
  70. config.Opts.HTTPS,
  71. config.Opts.BasePath(),
  72. ))
  73. html.Redirect(w, r, route.Path(h.router, user.DefaultHomePage))
  74. }