2018-04-27 23:38:46 -04:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-11-20 00:10:04 -05: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 00:51:50 -04:00
|
|
|
package middleware // import "miniflux.app/middleware"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-29 19:35:04 -04:00
|
|
|
"errors"
|
2017-11-23 01:22:33 -05:00
|
|
|
"net/http"
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
// AppSession handles application session middleware.
|
|
|
|
func (m *Middleware) AppSession(next http.Handler) http.Handler {
|
2017-11-20 00:10:04 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-16 21:07:53 -05:00
|
|
|
var err error
|
2018-04-29 19:35:04 -04:00
|
|
|
session := m.getAppSessionValueFromCookie(r)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if session == nil {
|
2018-04-29 19:35:04 -04:00
|
|
|
logger.Debug("[Middleware:AppSession] Session not found")
|
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
session, err = m.store.CreateSession()
|
2017-12-16 21:07:53 -05:00
|
|
|
if err != nil {
|
2018-04-29 19:35:04 -04:00
|
|
|
logger.Error("[Middleware:AppSession] %v", err)
|
|
|
|
html.ServerError(w, err)
|
2017-12-16 21:07:53 -05:00
|
|
|
return
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2017-12-16 21:07:53 -05:00
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, m.cfg.IsHTTPS, m.cfg.BasePath()))
|
2017-11-20 00:10:04 -05:00
|
|
|
} else {
|
2018-04-29 19:35:04 -04:00
|
|
|
logger.Debug("[Middleware:AppSession] %s", session)
|
2017-12-16 21:07:53 -05:00
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2017-12-16 21:07:53 -05: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-29 19:35:04 -04:00
|
|
|
logger.Error(`[Middleware:AppSession] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
|
|
|
|
html.BadRequest(w, errors.New("invalid or missing CSRF"))
|
2017-12-16 21:07:53 -05:00
|
|
|
return
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-16 21:07:53 -05:00
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, SessionIDContextKey, session.ID)
|
|
|
|
ctx = context.WithValue(ctx, CSRFContextKey, session.Data.CSRF)
|
|
|
|
ctx = context.WithValue(ctx, OAuth2StateContextKey, session.Data.OAuth2State)
|
|
|
|
ctx = context.WithValue(ctx, FlashMessageContextKey, session.Data.FlashMessage)
|
|
|
|
ctx = context.WithValue(ctx, FlashErrorMessageContextKey, session.Data.FlashErrorMessage)
|
2018-01-19 00:53:31 -05:00
|
|
|
ctx = context.WithValue(ctx, UserLanguageContextKey, session.Data.Language)
|
2018-07-19 01:30:05 -04:00
|
|
|
ctx = context.WithValue(ctx, UserThemeContextKey, session.Data.Theme)
|
2018-05-20 18:29:14 -04:00
|
|
|
ctx = context.WithValue(ctx, PocketRequestTokenContextKey, session.Data.PocketRequestToken)
|
2017-12-16 21:07:53 -05:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
func (m *Middleware) getAppSessionValueFromCookie(r *http.Request) *model.Session {
|
|
|
|
cookieValue := request.Cookie(r, cookie.CookieSessionID)
|
|
|
|
if cookieValue == "" {
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
session, err := m.store.Session(cookieValue)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2018-04-29 19:35:04 -04:00
|
|
|
logger.Error("[Middleware:AppSession] %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|