2018-11-11 20:28:29 +01:00
|
|
|
// Copyright 2018 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 ui // import "miniflux.app/ui"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/http/route"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
2019-06-02 03:18:09 +02:00
|
|
|
"miniflux.app/storage"
|
2020-01-29 11:45:59 +01:00
|
|
|
"miniflux.app/ui/session"
|
2018-11-11 20:28:29 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type middleware struct {
|
|
|
|
router *mux.Router
|
2019-06-02 03:18:09 +02:00
|
|
|
store *storage.Storage
|
2018-11-11 20:28:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
func newMiddleware(router *mux.Router, store *storage.Storage) *middleware {
|
|
|
|
return &middleware{router, store}
|
2018-11-11 20:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) handleUserSession(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := m.getUserSessionFromCookie(r)
|
|
|
|
|
|
|
|
if session == nil {
|
|
|
|
if m.isPublicRoute(r) {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
} else {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Debug("[UI:UserSession] Session not found, redirect to login page")
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(m.router, "login"))
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Debug("[UI:UserSession] %s", session)
|
2018-11-11 20:28:29 +01:00
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, request.UserIDContextKey, session.UserID)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
|
|
|
|
ctx = context.WithValue(ctx, request.UserSessionTokenContextKey, session.Token)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) handleAppSession(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
session := m.getAppSessionValueFromCookie(r)
|
|
|
|
|
|
|
|
if session == nil {
|
2019-06-02 03:18:09 +02:00
|
|
|
if request.IsAuthenticated(r) {
|
2019-01-22 05:21:05 +01:00
|
|
|
userID := request.UserID(r)
|
|
|
|
logger.Debug("[UI:AppSession] Cookie expired but user #%d is logged: creating a new session", userID)
|
|
|
|
session, err = m.store.CreateAppSessionWithUserPrefs(userID)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Debug("[UI:AppSession] Session not found, creating a new one")
|
|
|
|
session, err = m.store.CreateAppSession()
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2018-11-11 20:28:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieAppSessionID, session.ID, config.Opts.HTTPS, config.Opts.BasePath()))
|
2018-11-11 20:28:29 +01:00
|
|
|
} else {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Debug("[UI:AppSession] %s", session)
|
2018-11-11 20:28:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 06:13:00 +02:00
|
|
|
if r.Method == http.MethodPost {
|
2018-11-11 20:28:29 +01:00
|
|
|
formValue := r.FormValue("csrf")
|
|
|
|
headerValue := r.Header.Get("X-Csrf-Token")
|
|
|
|
|
|
|
|
if session.Data.CSRF != formValue && session.Data.CSRF != headerValue {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Error(`[UI:AppSession] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
|
2020-08-09 22:16:57 +02:00
|
|
|
|
|
|
|
if mux.CurrentRoute(r).GetName() == "checkLogin" {
|
|
|
|
html.Redirect(w, r, route.Path(m.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
html.BadRequest(w, r, errors.New("Invalid or missing CSRF"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, request.SessionIDContextKey, session.ID)
|
|
|
|
ctx = context.WithValue(ctx, request.CSRFContextKey, session.Data.CSRF)
|
|
|
|
ctx = context.WithValue(ctx, request.OAuth2StateContextKey, session.Data.OAuth2State)
|
|
|
|
ctx = context.WithValue(ctx, request.FlashMessageContextKey, session.Data.FlashMessage)
|
|
|
|
ctx = context.WithValue(ctx, request.FlashErrorMessageContextKey, session.Data.FlashErrorMessage)
|
|
|
|
ctx = context.WithValue(ctx, request.UserLanguageContextKey, session.Data.Language)
|
|
|
|
ctx = context.WithValue(ctx, request.UserThemeContextKey, session.Data.Theme)
|
|
|
|
ctx = context.WithValue(ctx, request.PocketRequestTokenContextKey, session.Data.PocketRequestToken)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) getAppSessionValueFromCookie(r *http.Request) *model.Session {
|
2019-01-22 05:26:46 +01:00
|
|
|
cookieValue := request.CookieValue(r, cookie.CookieAppSessionID)
|
2018-11-11 20:28:29 +01:00
|
|
|
if cookieValue == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
session, err := m.store.AppSession(cookieValue)
|
2018-11-11 20:28:29 +01:00
|
|
|
if err != nil {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Error("[UI:AppSession] %v", err)
|
2018-11-11 20:28:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) isPublicRoute(r *http.Request) bool {
|
|
|
|
route := mux.CurrentRoute(r)
|
|
|
|
switch route.GetName() {
|
|
|
|
case "login",
|
|
|
|
"checkLogin",
|
|
|
|
"stylesheet",
|
|
|
|
"javascript",
|
|
|
|
"oauth2Redirect",
|
|
|
|
"oauth2Callback",
|
|
|
|
"appIcon",
|
|
|
|
"favicon",
|
|
|
|
"webManifest",
|
|
|
|
"robots",
|
2020-03-23 02:48:14 +01:00
|
|
|
"sharedEntry",
|
2018-11-11 20:28:29 +01:00
|
|
|
"healthcheck":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) getUserSessionFromCookie(r *http.Request) *model.UserSession {
|
|
|
|
cookieValue := request.CookieValue(r, cookie.CookieUserSessionID)
|
|
|
|
if cookieValue == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := m.store.UserSessionByToken(cookieValue)
|
|
|
|
if err != nil {
|
2018-11-11 20:40:40 +01:00
|
|
|
logger.Error("[UI:UserSession] %v", err)
|
2018-11-11 20:28:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
2020-01-29 11:45:59 +01:00
|
|
|
|
|
|
|
func (m *middleware) handleAuthProxy(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if request.IsAuthenticated(r) || config.Opts.AuthProxyHeader() == "" {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
username := r.Header.Get(config.Opts.AuthProxyHeader())
|
|
|
|
if username == "" {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := session.New(m.store, request.SessionID(r))
|
|
|
|
clientIP := request.ClientIP(r)
|
|
|
|
|
|
|
|
logger.Info("[AuthProxy] Successful auth for %s", username)
|
|
|
|
|
|
|
|
user, err := m.store.UserByUsername(username)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
if !config.Opts.IsAuthProxyUserCreationAllowed() {
|
|
|
|
html.Forbidden(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user = model.NewUser()
|
|
|
|
user.Username = username
|
|
|
|
user.IsAdmin = false
|
|
|
|
|
|
|
|
if err := m.store.CreateUser(user); err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionToken, _, err := m.store.CreateUserSession(user.Username, r.UserAgent(), clientIP)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("[AuthProxy] username=%s just logged in", user.Username)
|
|
|
|
|
|
|
|
m.store.SetLastLogin(user.ID)
|
|
|
|
sess.SetLanguage(user.Language)
|
|
|
|
sess.SetTheme(user.Theme)
|
|
|
|
|
|
|
|
http.SetCookie(w, cookie.New(
|
|
|
|
cookie.CookieUserSessionID,
|
|
|
|
sessionToken,
|
|
|
|
config.Opts.HTTPS,
|
|
|
|
config.Opts.BasePath(),
|
|
|
|
))
|
|
|
|
|
|
|
|
html.Redirect(w, r, route.Path(m.router, "unread"))
|
|
|
|
})
|
|
|
|
}
|