2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-11-11 14:28:29 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package ui // import "miniflux.app/v2/internal/ui"
|
2018-11-11 14:28:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2018-11-11 14:28:29 -05:00
|
|
|
"net/http"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/http/cookie"
|
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/http/response/html"
|
|
|
|
"miniflux.app/v2/internal/http/route"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
|
|
|
"miniflux.app/v2/internal/ui/session"
|
2018-11-11 14:28:29 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type middleware struct {
|
|
|
|
router *mux.Router
|
2019-06-01 21:18:09 -04:00
|
|
|
store *storage.Storage
|
2018-11-11 14:28:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
func newMiddleware(router *mux.Router, store *storage.Storage) *middleware {
|
|
|
|
return &middleware{router, store}
|
2018-11-11 14:28:29 -05: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 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Redirecting to login page because no user session has been found",
|
|
|
|
slog.Any("url", r.RequestURI),
|
|
|
|
)
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(m.router, "login"))
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("User session found",
|
|
|
|
slog.Any("url", r.RequestURI),
|
|
|
|
slog.Int64("user_id", session.UserID),
|
|
|
|
slog.Int64("user_session_id", session.ID),
|
|
|
|
)
|
2018-11-11 14:28:29 -05: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-01 21:18:09 -04:00
|
|
|
if request.IsAuthenticated(r) {
|
2019-01-21 23:21:05 -05:00
|
|
|
userID := request.UserID(r)
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Cookie expired but user is logged: creating a new app session",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
)
|
2019-01-21 23:21:05 -05:00
|
|
|
session, err = m.store.CreateAppSessionWithUserPrefs(userID)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("App session not found, creating a new one")
|
2019-01-21 23:21:05 -05:00
|
|
|
session, err = m.store.CreateAppSession()
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2018-11-11 14:28:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieAppSessionID, session.ID, config.Opts.HTTPS, config.Opts.BasePath()))
|
2018-11-11 14:28:29 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:13:00 -04:00
|
|
|
if r.Method == http.MethodPost {
|
2018-11-11 14:28:29 -05:00
|
|
|
formValue := r.FormValue("csrf")
|
|
|
|
headerValue := r.Header.Get("X-Csrf-Token")
|
|
|
|
|
|
|
|
if session.Data.CSRF != formValue && session.Data.CSRF != headerValue {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("Invalid or missing CSRF token",
|
|
|
|
slog.Any("url", r.RequestURI),
|
|
|
|
slog.String("form_csrf", formValue),
|
|
|
|
slog.String("header_csrf", headerValue),
|
|
|
|
)
|
2020-08-09 16:16:57 -04:00
|
|
|
|
|
|
|
if mux.CurrentRoute(r).GetName() == "checkLogin" {
|
|
|
|
html.Redirect(w, r, route.Path(m.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:35:10 -04:00
|
|
|
html.BadRequest(w, r, errors.New("invalid or missing CSRF"))
|
2018-11-11 14:28:29 -05:00
|
|
|
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)
|
2023-09-03 00:35:10 -04:00
|
|
|
ctx = context.WithValue(ctx, request.OAuth2CodeVerifierContextKey, session.Data.OAuth2CodeVerifier)
|
2018-11-11 14:28:29 -05:00
|
|
|
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)
|
2023-10-20 18:12:02 -04:00
|
|
|
ctx = context.WithValue(ctx, request.LastForceRefreshContextKey, session.Data.LastForceRefresh)
|
2023-11-05 12:57:35 -05:00
|
|
|
ctx = context.WithValue(ctx, request.WebAuthnDataContextKey, session.Data.WebAuthnSessionData)
|
2018-11-11 14:28:29 -05:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) getAppSessionValueFromCookie(r *http.Request) *model.Session {
|
2019-01-21 23:26:46 -05:00
|
|
|
cookieValue := request.CookieValue(r, cookie.CookieAppSessionID)
|
2018-11-11 14:28:29 -05:00
|
|
|
if cookieValue == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 23:21:05 -05:00
|
|
|
session, err := m.store.AppSession(cookieValue)
|
2018-11-11 14:28:29 -05:00
|
|
|
if err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Unable to fetch app session from the database; another session will be created",
|
|
|
|
slog.Any("cookie_value", cookieValue),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-11-11 14:28:29 -05: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-22 21:48:14 -04:00
|
|
|
"sharedEntry",
|
2021-03-07 18:25:34 -05:00
|
|
|
"healthcheck",
|
2022-10-15 02:17:17 -04:00
|
|
|
"offline",
|
2023-11-05 12:57:35 -05:00
|
|
|
"proxy",
|
|
|
|
"webauthnLoginBegin",
|
|
|
|
"webauthnLoginFinish":
|
2018-11-11 14:28:29 -05:00
|
|
|
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 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Unable to fetch user session from the database",
|
|
|
|
slog.Any("cookie_value", cookieValue),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-11-11 14:28:29 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
2020-01-29 05:45:59 -05: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
|
|
|
|
}
|
|
|
|
|
|
|
|
clientIP := request.ClientIP(r)
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("[AuthProxy] Received authenticated requested",
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", username),
|
|
|
|
)
|
2020-01-29 05:45:59 -05:00
|
|
|
|
|
|
|
user, err := m.store.UserByUsername(username)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
if !config.Opts.IsAuthProxyUserCreationAllowed() {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("[AuthProxy] User doesn't exist and user creation is not allowed",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", username),
|
|
|
|
)
|
2020-01-29 05:45:59 -05:00
|
|
|
html.Forbidden(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
if user, err = m.store.CreateUser(&model.UserCreationRequest{Username: username}); err != nil {
|
2020-01-29 05:45:59 -05:00
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:28:34 -05:00
|
|
|
sessionToken, _, err := m.store.CreateUserSessionFromUsername(user.Username, r.UserAgent(), clientIP)
|
2020-01-29 05:45:59 -05:00
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Info("[AuthProxy] User authenticated successfully",
|
|
|
|
slog.Bool("authentication_successful", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("username", user.Username),
|
|
|
|
)
|
2020-01-29 05:45:59 -05:00
|
|
|
|
|
|
|
m.store.SetLastLogin(user.ID)
|
2021-01-04 00:20:21 -05:00
|
|
|
|
|
|
|
sess := session.New(m.store, request.SessionID(r))
|
2020-01-29 05:45:59 -05:00
|
|
|
sess.SetLanguage(user.Language)
|
|
|
|
sess.SetTheme(user.Theme)
|
|
|
|
|
|
|
|
http.SetCookie(w, cookie.New(
|
|
|
|
cookie.CookieUserSessionID,
|
|
|
|
sessionToken,
|
|
|
|
config.Opts.HTTPS,
|
|
|
|
config.Opts.BasePath(),
|
|
|
|
))
|
|
|
|
|
2023-11-07 14:29:19 -05:00
|
|
|
html.Redirect(w, r, route.Path(m.router, user.DefaultHomePage))
|
2020-01-29 05:45:59 -05:00
|
|
|
})
|
|
|
|
}
|