1
0
Fork 0
miniflux/middleware/app_session.go

77 lines
2.4 KiB
Go
Raw Normal View History

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.
package middleware
import (
"context"
"errors"
2017-11-23 01:22:33 -05:00
"net/http"
"github.com/miniflux/miniflux/http/cookie"
"github.com/miniflux/miniflux/http/request"
"github.com/miniflux/miniflux/http/response/html"
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"
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
session := m.getAppSessionValueFromCookie(r)
2017-11-20 00:10:04 -05:00
if session == nil {
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 {
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 {
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 {
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)
ctx = context.WithValue(ctx, UserLanguageContextKey, session.Data.Language)
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
}
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
}
session, err := m.store.Session(cookieValue)
2017-11-20 00:10:04 -05:00
if err != nil {
logger.Error("[Middleware:AppSession] %v", err)
2017-11-20 00:10:04 -05:00
return nil
}
return session
}