miniflux/server/middleware/session.go

83 lines
2.5 KiB
Go
Raw Normal View History

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-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/model"
2017-12-17 03:07:53 +01:00
"github.com/miniflux/miniflux/server/cookie"
2017-12-13 06:48:13 +01:00
"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-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-17 03:07:53 +01:00
func (t *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
session := t.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-17 03:07:53 +01:00
session, err = t.store.CreateSession()
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
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, r.URL.Scheme == "https"))
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)
next.ServeHTTP(w, r.WithContext(ctx))
})
2017-11-20 06:10:04 +01:00
}
2017-12-17 03:07:53 +01:00
func (t *SessionMiddleware) getSessionValueFromCookie(r *http.Request) *model.Session {
sessionCookie, err := r.Cookie(cookie.CookieSessionID)
2017-11-20 06:10:04 +01:00
if err == http.ErrNoCookie {
return nil
}
2017-12-17 03:07:53 +01:00
session, err := t.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-17 03:07:53 +01:00
func NewSessionMiddleware(s *storage.Storage) *SessionMiddleware {
return &SessionMiddleware{store: s}
2017-11-20 06:10:04 +01:00
}