func Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { err := services.Logout(r) w.Header().Set("Content-Type", "application/json") if err != nil { w.WriteHeader(http.StatusInternalServerError) } else { w.WriteHeader(http.StatusOK) } }
func (suite *AuthenticationServicesTestSuite) TestLogout(c *C) { user := models.User{ Username: "******", Password: "******", } authBackend := auth.InitJWTAuthenticationBackend() tokenString, err := authentication.GenerateToken(user.UUID) token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { return authBackend.PublicKey, nil }) err = services.Logout(tokenString, token) assert.Nil(t, err) }
func (suite *MiddlewaresTestSuite) TestRequireTokenAuthenticationAfterLogout(c *C) { resource := "/test/hello" requestLogout, _ := http.NewRequest("GET", resource, nil) requestLogout.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) services.Logout(requestLogout) response := httptest.NewRecorder() request, _ := http.NewRequest("GET", resource, nil) request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) server.ServeHTTP(response, request) assert.Equal(t, response.Code, http.StatusUnauthorized) }