2017-11-20 00:10:04 -05: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 01:22:33 -05:00
|
|
|
"net/http"
|
|
|
|
|
2017-12-28 23:34:18 -05:00
|
|
|
"github.com/miniflux/miniflux/config"
|
2018-01-03 01:04:48 -05:00
|
|
|
"github.com/miniflux/miniflux/http/cookie"
|
2017-12-15 21:55:57 -05:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 00:48:13 -05:00
|
|
|
"github.com/miniflux/miniflux/model"
|
|
|
|
"github.com/miniflux/miniflux/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// SessionMiddleware represents a session middleware.
|
2017-11-20 00:10:04 -05:00
|
|
|
type SessionMiddleware struct {
|
2017-12-28 23:34:18 -05:00
|
|
|
cfg *config.Config
|
2017-12-16 21:07:53 -05:00
|
|
|
store *storage.Storage
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Handler execute the middleware.
|
2017-12-28 23:34:18 -05:00
|
|
|
func (s *SessionMiddleware) Handler(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
|
2017-12-28 23:34:18 -05:00
|
|
|
session := s.getSessionValueFromCookie(r)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if session == nil {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Debug("[Middleware:Session] Session not found")
|
2017-12-28 23:34:18 -05:00
|
|
|
session, err = s.store.CreateSession()
|
2017-12-16 21:07:53 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[Middleware:Session] %v", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2017-12-16 21:07:53 -05:00
|
|
|
|
2017-12-28 23:34:18 -05:00
|
|
|
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, s.cfg.IsHTTPS))
|
2017-11-20 00:10:04 -05:00
|
|
|
} else {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Debug("[Middleware:Session] %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 {
|
|
|
|
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 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)
|
2017-12-16 21:07:53 -05:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-28 23:34:18 -05:00
|
|
|
func (s *SessionMiddleware) getSessionValueFromCookie(r *http.Request) *model.Session {
|
2017-12-16 21:07:53 -05:00
|
|
|
sessionCookie, err := r.Cookie(cookie.CookieSessionID)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err == http.ErrNoCookie {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 23:34:18 -05:00
|
|
|
session, err := s.store.Session(sessionCookie.Value)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2017-12-16 21:07:53 -05:00
|
|
|
logger.Error("[Middleware:Session] %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// NewSessionMiddleware returns a new SessionMiddleware.
|
2017-12-28 23:34:18 -05:00
|
|
|
func NewSessionMiddleware(cfg *config.Config, store *storage.Storage) *SessionMiddleware {
|
|
|
|
return &SessionMiddleware{cfg, store}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|