session.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "context"
  7. "net/http"
  8. "github.com/miniflux/miniflux/config"
  9. "github.com/miniflux/miniflux/logger"
  10. "github.com/miniflux/miniflux/model"
  11. "github.com/miniflux/miniflux/server/cookie"
  12. "github.com/miniflux/miniflux/storage"
  13. )
  14. // SessionMiddleware represents a session middleware.
  15. type SessionMiddleware struct {
  16. cfg *config.Config
  17. store *storage.Storage
  18. }
  19. // Handler execute the middleware.
  20. func (s *SessionMiddleware) Handler(next http.Handler) http.Handler {
  21. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  22. var err error
  23. session := s.getSessionValueFromCookie(r)
  24. if session == nil {
  25. logger.Debug("[Middleware:Session] Session not found")
  26. session, err = s.store.CreateSession()
  27. if err != nil {
  28. logger.Error("[Middleware:Session] %v", err)
  29. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  30. return
  31. }
  32. http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, s.cfg.IsHTTPS))
  33. } else {
  34. logger.Debug("[Middleware:Session] %s", session)
  35. }
  36. if r.Method == "POST" {
  37. formValue := r.FormValue("csrf")
  38. headerValue := r.Header.Get("X-Csrf-Token")
  39. if session.Data.CSRF != formValue && session.Data.CSRF != headerValue {
  40. logger.Error(`[Middleware:Session] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
  41. w.WriteHeader(http.StatusBadRequest)
  42. w.Write([]byte("Invalid or missing CSRF session!"))
  43. return
  44. }
  45. }
  46. ctx := r.Context()
  47. ctx = context.WithValue(ctx, SessionIDContextKey, session.ID)
  48. ctx = context.WithValue(ctx, CSRFContextKey, session.Data.CSRF)
  49. ctx = context.WithValue(ctx, OAuth2StateContextKey, session.Data.OAuth2State)
  50. ctx = context.WithValue(ctx, FlashMessageContextKey, session.Data.FlashMessage)
  51. ctx = context.WithValue(ctx, FlashErrorMessageContextKey, session.Data.FlashErrorMessage)
  52. next.ServeHTTP(w, r.WithContext(ctx))
  53. })
  54. }
  55. func (s *SessionMiddleware) getSessionValueFromCookie(r *http.Request) *model.Session {
  56. sessionCookie, err := r.Cookie(cookie.CookieSessionID)
  57. if err == http.ErrNoCookie {
  58. return nil
  59. }
  60. session, err := s.store.Session(sessionCookie.Value)
  61. if err != nil {
  62. logger.Error("[Middleware:Session] %v", err)
  63. return nil
  64. }
  65. return session
  66. }
  67. // NewSessionMiddleware returns a new SessionMiddleware.
  68. func NewSessionMiddleware(cfg *config.Config, store *storage.Storage) *SessionMiddleware {
  69. return &SessionMiddleware{cfg, store}
  70. }