mirror of
https://github.com/volatiletech/authboss.git
synced 2026-06-19 23:00:27 +02:00
@@ -54,6 +54,8 @@ func (o *OAuth2) Routes() authboss.RouteTable {
|
||||
cfg.OAuth2Config.RedirectURL = authboss.Cfg.RootURL + callback
|
||||
}
|
||||
|
||||
routes["/oauth2/logout"] = logout
|
||||
|
||||
return routes
|
||||
}
|
||||
|
||||
@@ -203,3 +205,18 @@ func oauthCallback(ctx *authboss.Context, w http.ResponseWriter, r *http.Request
|
||||
http.Redirect(w, r, redirect, http.StatusFound)
|
||||
return nil
|
||||
}
|
||||
|
||||
func logout(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) error {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
ctx.SessionStorer.Del(authboss.SessionKey)
|
||||
ctx.CookieStorer.Del(authboss.CookieRemember)
|
||||
ctx.SessionStorer.Del(authboss.SessionLastAction)
|
||||
|
||||
http.Redirect(w, r, authboss.Cfg.AuthLogoutOKPath, http.StatusFound)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -264,3 +264,42 @@ func TestOAuthFailure(t *testing.T) {
|
||||
t.Error("It should record the failure.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogout(t *testing.T) {
|
||||
authboss.Cfg = authboss.NewConfig()
|
||||
authboss.Cfg.AuthLogoutOKPath = "/dashboard"
|
||||
|
||||
r, _ := http.NewRequest("GET", "/oauth2/google?", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ctx := authboss.NewContext()
|
||||
session := mocks.NewMockClientStorer(authboss.SessionKey, "asdf", authboss.SessionLastAction, "1234")
|
||||
cookies := mocks.NewMockClientStorer(authboss.CookieRemember, "qwert")
|
||||
ctx.SessionStorer = session
|
||||
ctx.CookieStorer = cookies
|
||||
|
||||
if err := logout(ctx, w, r); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if val, ok := session.Get(authboss.SessionKey); ok {
|
||||
t.Error("Unexpected session key:", val)
|
||||
}
|
||||
|
||||
if val, ok := session.Get(authboss.SessionLastAction); ok {
|
||||
t.Error("Unexpected last action:", val)
|
||||
}
|
||||
|
||||
if val, ok := cookies.Get(authboss.CookieRemember); ok {
|
||||
t.Error("Unexpected rm cookie:", val)
|
||||
}
|
||||
|
||||
if http.StatusFound != w.Code {
|
||||
t.Errorf("Expected status code %d, got %d", http.StatusFound, w.Code)
|
||||
}
|
||||
|
||||
location := w.Header().Get("Location")
|
||||
if location != authboss.Cfg.AuthLogoutOKPath {
|
||||
t.Error("Redirect wrong:", location)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user