-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathlogout.go
More file actions
77 lines (63 loc) · 1.84 KB
/
logout.go
File metadata and controls
77 lines (63 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package logout allows users to log out (from auth or oauth2 logins)
package logout
import (
"net/http"
"github.com/pkg/errors"
"github.com/volatiletech/authboss"
)
func init() {
authboss.RegisterModule("logout", &Logout{})
}
// Logout module
type Logout struct {
*authboss.Authboss
}
// Init the module
func (l *Logout) Init(ab *authboss.Authboss) error {
l.Authboss = ab
var logoutRouteMethod func(string, http.Handler)
switch l.Authboss.Config.Modules.LogoutMethod {
case "GET":
logoutRouteMethod = l.Authboss.Config.Core.Router.Get
case "POST":
logoutRouteMethod = l.Authboss.Config.Core.Router.Post
case "DELETE":
logoutRouteMethod = l.Authboss.Config.Core.Router.Delete
default:
return errors.Errorf("logout wants to register a logout route but was given an invalid method: %s", l.Authboss.Config.Modules.LogoutMethod)
}
logoutRouteMethod("/logout", l.Authboss.Core.ErrorHandler.Wrap(l.Logout))
return nil
}
// Logout the user
func (l *Logout) Logout(w http.ResponseWriter, r *http.Request) error {
logger := l.RequestLogger(r)
user, err := l.CurrentUser(r)
if err == nil && user != nil {
logger.Infof("user %s logged out", user.GetPID())
} else {
logger.Info("user (unknown) logged out")
}
var handled bool
handled, err = l.Events.FireBefore(authboss.EventLogout, w, r)
if err != nil {
return err
} else if handled {
return nil
}
authboss.DelAllSession(w, l.Config.Storage.SessionStateWhitelistKeys)
authboss.DelKnownSession(w)
authboss.DelKnownCookie(w)
handled, err = l.Authboss.Events.FireAfter(authboss.EventLogout, w, r)
if err != nil {
return err
} else if handled {
return nil
}
ro := authboss.RedirectOptions{
Code: http.StatusTemporaryRedirect,
RedirectPath: l.Authboss.Paths.LogoutOK,
Success: "You have been logged out",
}
return l.Authboss.Core.Redirector.Redirect(w, r, ro)
}