2017-12-17 03:07:53 +01:00
|
|
|
// 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.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package middleware // import "miniflux.app/middleware"
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
2018-10-08 03:42:43 +02:00
|
|
|
"miniflux.app/http/response/html"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/route"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
// UserSession handles the user session middleware.
|
|
|
|
func (m *Middleware) UserSession(next http.Handler) http.Handler {
|
2017-12-17 03:07:53 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-04-30 01:35:04 +02:00
|
|
|
session := m.getUserSessionFromCookie(r)
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
if session == nil {
|
|
|
|
logger.Debug("[Middleware:UserSession] Session not found")
|
2018-04-28 05:38:46 +02:00
|
|
|
if m.isPublicRoute(r) {
|
2017-12-17 03:07:53 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
} else {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.Redirect(w, r, route.Path(m.router, "login"))
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Debug("[Middleware:UserSession] %s", session)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2017-12-17 03:07:53 +01:00
|
|
|
ctx := r.Context()
|
2018-09-03 23:26:40 +02:00
|
|
|
ctx = context.WithValue(ctx, request.UserIDContextKey, session.UserID)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
|
|
|
|
ctx = context.WithValue(ctx, request.UserSessionTokenContextKey, session.Token)
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
func (m *Middleware) isPublicRoute(r *http.Request) bool {
|
2017-12-17 03:07:53 +01:00
|
|
|
route := mux.CurrentRoute(r)
|
|
|
|
switch route.GetName() {
|
2018-01-19 06:53:31 +01:00
|
|
|
case "login",
|
|
|
|
"checkLogin",
|
|
|
|
"stylesheet",
|
|
|
|
"javascript",
|
|
|
|
"oauth2Redirect",
|
|
|
|
"oauth2Callback",
|
|
|
|
"appIcon",
|
|
|
|
"favicon",
|
|
|
|
"webManifest":
|
2017-12-17 03:07:53 +01:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func (m *Middleware) getUserSessionFromCookie(r *http.Request) *model.UserSession {
|
2018-09-24 06:02:26 +02:00
|
|
|
cookieValue := request.CookieValue(r, cookie.CookieUserSessionID)
|
2018-04-30 01:35:04 +02:00
|
|
|
if cookieValue == "" {
|
2017-12-17 03:07:53 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
session, err := m.store.UserSessionByToken(cookieValue)
|
2017-12-17 03:07:53 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[Middleware:UserSession] %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|