user_session.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/server/route"
  12. "github.com/miniflux/miniflux/storage"
  13. "github.com/gorilla/mux"
  14. )
  15. // UserSessionMiddleware represents a user session middleware.
  16. type UserSessionMiddleware struct {
  17. store *storage.Storage
  18. router *mux.Router
  19. }
  20. // Handler execute the middleware.
  21. func (s *UserSessionMiddleware) Handler(next http.Handler) http.Handler {
  22. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  23. session := s.getSessionFromCookie(r)
  24. if session == nil {
  25. logger.Debug("[Middleware:UserSession] Session not found")
  26. if s.isPublicRoute(r) {
  27. next.ServeHTTP(w, r)
  28. } else {
  29. http.Redirect(w, r, route.Path(s.router, "login"), http.StatusFound)
  30. }
  31. } else {
  32. logger.Debug("[Middleware:UserSession] %s", session)
  33. ctx := r.Context()
  34. ctx = context.WithValue(ctx, UserIDContextKey, session.UserID)
  35. ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true)
  36. ctx = context.WithValue(ctx, UserSessionTokenContextKey, session.Token)
  37. next.ServeHTTP(w, r.WithContext(ctx))
  38. }
  39. })
  40. }
  41. func (s *UserSessionMiddleware) isPublicRoute(r *http.Request) bool {
  42. route := mux.CurrentRoute(r)
  43. switch route.GetName() {
  44. case "login", "checkLogin", "stylesheet", "javascript", "oauth2Redirect", "oauth2Callback", "appIcon", "favicon", "webManifest":
  45. return true
  46. default:
  47. return false
  48. }
  49. }
  50. func (s *UserSessionMiddleware) getSessionFromCookie(r *http.Request) *model.UserSession {
  51. sessionCookie, err := r.Cookie(cookie.CookieUserSessionID)
  52. if err == http.ErrNoCookie {
  53. return nil
  54. }
  55. session, err := s.store.UserSessionByToken(sessionCookie.Value)
  56. if err != nil {
  57. logger.Error("[Middleware:UserSession] %v", err)
  58. return nil
  59. }
  60. return session
  61. }
  62. // NewUserSessionMiddleware returns a new UserSessionMiddleware.
  63. func NewUserSessionMiddleware(s *storage.Storage, r *mux.Router) *UserSessionMiddleware {
  64. return &UserSessionMiddleware{store: s, router: r}
  65. }