// ServeHTTP as per the negroni.Handler interface
func (m *loggedInMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	// Initialise the session service
	sessionService := session.NewService(theService.cnf, r, w)

	// Attempt to start the session
	if err := sessionService.StartSession(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	context.Set(r, sessionServiceKey, sessionService)

	// Try to get a user session
	userSession, err := sessionService.GetUserSession()
	if err != nil {
		redirectWithQueryString("/web/login", r.URL.Query(), w, r)
		return
	}

	// Authenticate
	if err := authenticate(userSession); err != nil {
		redirectWithQueryString("/web/login", r.URL.Query(), w, r)
		return
	}

	// Update the user session
	sessionService.SetUserSession(userSession)

	next(w, r)
}
Esempio n. 2
0
// The SetupSuite method will be run by testify once, at the very
// start of the testing suite, before any tests are run.
func (suite *SessionTestSuite) SetupSuite() {
	suite.cnf = config.NewConfig(false, false)

	// Overwrite internal vars so we don't affect existing session
	session.StorageSessionName = "test_session"
	session.UserSessionKey = "test_user"

	// Initialise the service
	r, err := http.NewRequest("GET", "http://1.2.3.4/foo/bar", nil)
	assert.NoError(suite.T(), err, "Request setup should not get an error")
	w := httptest.NewRecorder()
	suite.service = session.NewService(suite.cnf, r, w)
}
// ServeHTTP as per the negroni.Handler interface
func (m *guestMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	// Initialise the session service
	sessionService := session.NewService(theService.cnf, r, w)

	// Attempt to start the session
	if err := sessionService.StartSession(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	context.Set(r, sessionServiceKey, sessionService)

	next(w, r)
}