2018-12-06 15:18:05 +01:00
|
|
|
package gorilla
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/securecookie"
|
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 13:11:23 +02:00
|
|
|
"gitlab.com/wpetit/goweb/service/session"
|
2018-12-06 15:18:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// SessionService is an implementation of service.Session
|
|
|
|
// based on the github.com/gorilla/sessions
|
|
|
|
type SessionService struct {
|
2019-05-13 09:09:28 +02:00
|
|
|
sessionName string
|
|
|
|
store sessions.Store
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a Session associated with the given HTTP request
|
|
|
|
func (s *SessionService) Get(w http.ResponseWriter, r *http.Request) (session.Session, error) {
|
|
|
|
sess, err := s.store.Get(r, s.sessionName)
|
|
|
|
if err != nil {
|
|
|
|
multiErr, ok := err.(securecookie.MultiError)
|
|
|
|
if !ok || multiErr.Error() != securecookie.ErrMacInvalid.Error() {
|
|
|
|
return nil, errors.Wrap(err, "error while retrieving the session from the request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NewSession(sess), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSessionService returns a new SessionService backed
|
|
|
|
// by the given Store
|
2019-05-13 09:09:28 +02:00
|
|
|
func NewSessionService(sessionName string, store sessions.Store) *SessionService {
|
2018-12-06 15:18:05 +01:00
|
|
|
return &SessionService{
|
2019-05-13 09:09:28 +02:00
|
|
|
sessionName: sessionName,
|
|
|
|
store: store,
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
}
|