Librairie d’aide à la création d’applications web pour Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
1.0 KiB

package gorilla
import (
"net/http"
"github.com/gorilla/securecookie"
"forge.cadoles.com/wpetit/goweb/service/session"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
)
// SessionService is an implementation of service.Session
// based on the github.com/gorilla/sessions
type SessionService struct {
sessionName string
store sessions.Store
}
// 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
func NewSessionService(sessionName string, store sessions.Store) *SessionService {
return &SessionService{
sessionName: sessionName,
store: store,
}
}