session.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/logger"
  9. "github.com/miniflux/miniflux/model"
  10. "github.com/miniflux/miniflux/server/cookie"
  11. "github.com/miniflux/miniflux/storage"
  12. )
  13. // SessionMiddleware represents a session middleware.
  14. type SessionMiddleware struct {
  15. store *storage.Storage
  16. }
  17. // Handler execute the middleware.
  18. func (t *SessionMiddleware) Handler(next http.Handler) http.Handler {
  19. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. var err error
  21. session := t.getSessionValueFromCookie(r)
  22. if session == nil {
  23. logger.Debug("[Middleware:Session] Session not found")
  24. session, err = t.store.CreateSession()
  25. if err != nil {
  26. logger.Error("[Middleware:Session] %v", err)
  27. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  28. return
  29. }
  30. http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, r.URL.Scheme == "https"))
  31. } else {
  32. logger.Debug("[Middleware:Session] %s", session)
  33. }
  34. if r.Method == "POST" {
  35. formValue := r.FormValue("csrf")
  36. headerValue := r.Header.Get("X-Csrf-Token")
  37. if session.Data.CSRF != formValue && session.Data.CSRF != headerValue {
  38. logger.Error(`[Middleware:Session] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
  39. w.WriteHeader(http.StatusBadRequest)
  40. w.Write([]byte("Invalid or missing CSRF session!"))
  41. return
  42. }
  43. }
  44. ctx := r.Context()
  45. ctx = context.WithValue(ctx, SessionIDContextKey, session.ID)
  46. ctx = context.WithValue(ctx, CSRFContextKey, session.Data.CSRF)
  47. ctx = context.WithValue(ctx, OAuth2StateContextKey, session.Data.OAuth2State)
  48. ctx = context.WithValue(ctx, FlashMessageContextKey, session.Data.FlashMessage)
  49. ctx = context.WithValue(ctx, FlashErrorMessageContextKey, session.Data.FlashErrorMessage)
  50. next.ServeHTTP(w, r.WithContext(ctx))
  51. })
  52. }
  53. func (t *SessionMiddleware) getSessionValueFromCookie(r *http.Request) *model.Session {
  54. sessionCookie, err := r.Cookie(cookie.CookieSessionID)
  55. if err == http.ErrNoCookie {
  56. return nil
  57. }
  58. session, err := t.store.Session(sessionCookie.Value)
  59. if err != nil {
  60. logger.Error("[Middleware:Session] %v", err)
  61. return nil
  62. }
  63. return session
  64. }
  65. // NewSessionMiddleware returns a new SessionMiddleware.
  66. func NewSessionMiddleware(s *storage.Storage) *SessionMiddleware {
  67. return &SessionMiddleware{store: s}
  68. }