auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "context"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/http/request"
  11. "miniflux.app/v2/internal/locale"
  12. "miniflux.app/v2/internal/model"
  13. "miniflux.app/v2/internal/oauth2"
  14. "miniflux.app/v2/internal/storage"
  15. )
  16. const (
  17. sessionCookieName = "MinifluxSessionID"
  18. preferencesCookieName = "MinifluxPreferences"
  19. preferencesCookieLifetime = 365 * 24 * time.Hour
  20. )
  21. // authenticateWebSession binds the current browser session to the given user,
  22. // rotates its identifier and secret, and refreshes the client cookie.
  23. func authenticateWebSession(w http.ResponseWriter, r *http.Request, store *storage.Storage, user *model.User) error {
  24. session := request.WebSession(r)
  25. session.SetUser(user)
  26. oldID, secret := session.Rotate()
  27. if err := store.RotateWebSession(oldID, session); err != nil {
  28. return err
  29. }
  30. setSessionCookie(w, session, secret)
  31. return nil
  32. }
  33. // setSessionCookie writes the session cookie to the response with the
  34. // security attributes used by miniflux (HttpOnly, SameSite=Lax, Secure
  35. // when HTTPS).
  36. func setSessionCookie(w http.ResponseWriter, session *model.WebSession, secret string) {
  37. http.SetCookie(w, &http.Cookie{
  38. Name: sessionCookieName,
  39. Value: session.ID + "." + secret,
  40. Path: cookiePath(),
  41. Secure: config.Opts.HTTPS(),
  42. HttpOnly: true,
  43. Expires: time.Now().Add(config.Opts.CleanupRemoveSessionsInterval()),
  44. SameSite: http.SameSiteLaxMode,
  45. })
  46. }
  47. // setPreferencesCookie remembers the language and theme of a user who logs
  48. // out, so the login page keeps using them without keeping a session around.
  49. func setPreferencesCookie(w http.ResponseWriter, language, theme string) {
  50. http.SetCookie(w, &http.Cookie{
  51. Name: preferencesCookieName,
  52. Value: language + "." + theme,
  53. Path: cookiePath(),
  54. Secure: config.Opts.HTTPS(),
  55. HttpOnly: true,
  56. Expires: time.Now().Add(preferencesCookieLifetime),
  57. SameSite: http.SameSiteLaxMode,
  58. })
  59. }
  60. // preferencesFromCookie returns the language and theme stored by
  61. // setPreferencesCookie. It reports false when the cookie is missing or holds
  62. // an unknown language or theme.
  63. func preferencesFromCookie(r *http.Request) (language, theme string, ok bool) {
  64. cookie, err := r.Cookie(preferencesCookieName)
  65. if err != nil {
  66. return "", "", false
  67. }
  68. language, theme, ok = strings.Cut(cookie.Value, ".")
  69. if !ok {
  70. return "", "", false
  71. }
  72. if _, found := locale.AvailableLanguages[language]; !found {
  73. return "", "", false
  74. }
  75. if _, found := model.Themes()[theme]; !found {
  76. return "", "", false
  77. }
  78. return language, theme, true
  79. }
  80. func cookiePath() string {
  81. if path := config.Opts.BasePath(); path != "" {
  82. return path
  83. }
  84. return "/"
  85. }
  86. func getOAuth2Manager(ctx context.Context) *oauth2.Manager {
  87. return oauth2.NewManager(
  88. ctx,
  89. config.Opts.OAuth2Provider(),
  90. config.Opts.OAuth2ClientID(),
  91. config.Opts.OAuth2ClientSecret(),
  92. config.Opts.OAuth2RedirectURL(),
  93. config.Opts.OAuth2OIDCDiscoveryEndpoint(),
  94. )
  95. }