2017-11-20 06:10:04 +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.
|
|
|
|
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-11-23 07:22:33 +01:00
|
|
|
"net/http"
|
|
|
|
|
2017-12-29 05:34:18 +01:00
|
|
|
"github.com/miniflux/miniflux/config"
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/http/cookie"
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
|
|
|
"github.com/miniflux/miniflux/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// SessionMiddleware represents a session middleware.
|
2017-11-20 06:10:04 +01:00
|
|
|
type SessionMiddleware struct {
|
2017-12-29 05:34:18 +01:00
|
|
|
cfg *config.Config
|
2017-12-17 03:07:53 +01:00
|
|
|
store *storage.Storage
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// Handler execute the middleware.
|
2017-12-29 05:34:18 +01:00
|
|
|
func (s *SessionMiddleware) Handler(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
|
2017-12-29 05:34:18 +01:00
|
|
|
session := s.getSessionValueFromCookie(r)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
if session == nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug("[Middleware:Session] Session not found")
|
2017-12-29 05:34:18 +01:00
|
|
|
session, err = s.store.CreateSession()
|
2017-12-17 03:07:53 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[Middleware:Session] %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-12-17 03:07:53 +01:00
|
|
|
|
2018-02-04 00:33:17 +01:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, s.cfg.IsHTTPS, s.cfg.BasePath()))
|
2017-11-20 06:10:04 +01:00
|
|
|
} else {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug("[Middleware:Session] %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 {
|
|
|
|
logger.Error(`[Middleware:Session] Invalid or missing CSRF token: Form="%s", Header="%s"`, formValue, headerValue)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("Invalid or missing CSRF session!"))
|
|
|
|
return
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 03:07:53 +01: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 06:53:31 +01:00
|
|
|
ctx = context.WithValue(ctx, UserLanguageContextKey, session.Data.Language)
|
2017-12-17 03:07:53 +01:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-29 05:34:18 +01:00
|
|
|
func (s *SessionMiddleware) getSessionValueFromCookie(r *http.Request) *model.Session {
|
2017-12-17 03:07:53 +01:00
|
|
|
sessionCookie, err := r.Cookie(cookie.CookieSessionID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err == http.ErrNoCookie {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-29 05:34:18 +01:00
|
|
|
session, err := s.store.Session(sessionCookie.Value)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-12-17 03:07:53 +01:00
|
|
|
logger.Error("[Middleware:Session] %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// NewSessionMiddleware returns a new SessionMiddleware.
|
2017-12-29 05:34:18 +01:00
|
|
|
func NewSessionMiddleware(cfg *config.Config, store *storage.Storage) *SessionMiddleware {
|
|
|
|
return &SessionMiddleware{cfg, store}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|