2018-04-28 05:38:46 +02:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-11-20 06:10:04 +01:00
|
|
|
// 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-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-30 01:35:04 +02:00
|
|
|
"errors"
|
2017-11-23 07:22:33 +01:00
|
|
|
"net/http"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
// AppSession handles application session middleware.
|
|
|
|
func (m *Middleware) AppSession(next http.Handler) http.Handler {
|
2017-11-20 06:10:04 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-17 03:07:53 +01:00
|
|
|
var err error
|
2018-04-30 01:35:04 +02:00
|
|
|
session := m.getAppSessionValueFromCookie(r)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
if session == nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
logger.Debug("[Middleware:AppSession] Session not found")
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
session, err = m.store.CreateSession()
|
2017-12-17 03:07:53 +01:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2017-12-17 03:07:53 +01:00
|
|
|
return
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-12-17 03:07:53 +01:00
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, m.cfg.IsHTTPS, m.cfg.BasePath()))
|
2017-11-20 06:10:04 +01:00
|
|
|
} else {
|
2018-04-30 01:35:04 +02:00
|
|
|
logger.Debug("[Middleware:AppSession] %s", session)
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-12-17 03:07:53 +01:00
|
|
|
if r.Method == "POST" {
|
|
|
|
formValue := r.FormValue("csrf")
|
|
|
|
headerValue := r.Header.Get("X-Csrf-Token")
|
|
|
|
|
|
|
|
if session.Data.CSRF != formValue && session.Data.CSRF != headerValue {
|
2018-04-30 01:35:04 +02:00
|
|
|
logger.Error(`[Middleware:AppSession] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
|
2018-10-08 03:42:43 +02:00
|
|
|
html.BadRequest(w, r, errors.New("Invalid or missing CSRF"))
|
2017-12-17 03:07:53 +01:00
|
|
|
return
|
|
|
|
}
|
2017-11-20 06:10:04 +01: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.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)
|
2017-12-17 03:07:53 +01:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func (m *Middleware) getAppSessionValueFromCookie(r *http.Request) *model.Session {
|
2018-09-24 06:02:26 +02:00
|
|
|
cookieValue := request.CookieValue(r, cookie.CookieSessionID)
|
2018-04-30 01:35:04 +02:00
|
|
|
if cookieValue == "" {
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
session, err := m.store.Session(cookieValue)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
logger.Error("[Middleware:AppSession] %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|