aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middleware/user_session.go
blob: e9bad82dc7196873dfb34345d195e879355467eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package middleware

import (
	"context"
	"net/http"

	"github.com/miniflux/miniflux/logger"
	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/server/cookie"
	"github.com/miniflux/miniflux/server/route"
	"github.com/miniflux/miniflux/storage"

	"github.com/gorilla/mux"
)

// UserSessionMiddleware represents a user session middleware.
type UserSessionMiddleware struct {
	store  *storage.Storage
	router *mux.Router
}

// Handler execute the middleware.
func (s *UserSessionMiddleware) Handler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		session := s.getSessionFromCookie(r)

		if session == nil {
			logger.Debug("[Middleware:UserSession] Session not found")
			if s.isPublicRoute(r) {
				next.ServeHTTP(w, r)
			} else {
				http.Redirect(w, r, route.Path(s.router, "login"), http.StatusFound)
			}
		} else {
			logger.Debug("[Middleware:UserSession] %s", session)
			ctx := r.Context()
			ctx = context.WithValue(ctx, UserIDContextKey, session.UserID)
			ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true)
			ctx = context.WithValue(ctx, UserSessionTokenContextKey, session.Token)

			next.ServeHTTP(w, r.WithContext(ctx))
		}
	})
}

func (s *UserSessionMiddleware) isPublicRoute(r *http.Request) bool {
	route := mux.CurrentRoute(r)
	switch route.GetName() {
	case "login", "checkLogin", "stylesheet", "javascript", "oauth2Redirect", "oauth2Callback", "appIcon", "favicon":
		return true
	default:
		return false
	}
}

func (s *UserSessionMiddleware) getSessionFromCookie(r *http.Request) *model.UserSession {
	sessionCookie, err := r.Cookie(cookie.CookieUserSessionID)
	if err == http.ErrNoCookie {
		return nil
	}

	session, err := s.store.UserSessionByToken(sessionCookie.Value)
	if err != nil {
		logger.Error("[Middleware:UserSession] %v", err)
		return nil
	}

	return session
}

// NewUserSessionMiddleware returns a new UserSessionMiddleware.
func NewUserSessionMiddleware(s *storage.Storage, r *mux.Router) *UserSessionMiddleware {
	return &UserSessionMiddleware{store: s, router: r}
}