user_session.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/http/cookie"
  9. "github.com/miniflux/miniflux/http/route"
  10. "github.com/miniflux/miniflux/logger"
  11. "github.com/miniflux/miniflux/model"
  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",
  45. "checkLogin",
  46. "stylesheet",
  47. "javascript",
  48. "oauth2Redirect",
  49. "oauth2Callback",
  50. "appIcon",
  51. "favicon",
  52. "webManifest":
  53. return true
  54. default:
  55. return false
  56. }
  57. }
  58. func (s *UserSessionMiddleware) getSessionFromCookie(r *http.Request) *model.UserSession {
  59. sessionCookie, err := r.Cookie(cookie.CookieUserSessionID)
  60. if err == http.ErrNoCookie {
  61. return nil
  62. }
  63. session, err := s.store.UserSessionByToken(sessionCookie.Value)
  64. if err != nil {
  65. logger.Error("[Middleware:UserSession] %v", err)
  66. return nil
  67. }
  68. return session
  69. }
  70. // NewUserSessionMiddleware returns a new UserSessionMiddleware.
  71. func NewUserSessionMiddleware(s *storage.Storage, r *mux.Router) *UserSessionMiddleware {
  72. return &UserSessionMiddleware{store: s, router: r}
  73. }