-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
user_updater.go
38 lines (31 loc) · 1.15 KB
/
user_updater.go
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
package middleware
import (
"net/http"
"github.com/go-pkgz/auth/token"
)
// UserUpdater defines interface adding extras or modifying UserInfo in request context
type UserUpdater interface {
Update(claims token.User) token.User
}
// UserUpdFunc type is an adapter to allow the use of ordinary functions as UserUpdater. If f is a function
// with the appropriate signature, UserUpdFunc(f) is a Handler that calls f.
type UserUpdFunc func(user token.User) token.User
// Update calls f(user)
func (f UserUpdFunc) Update(user token.User) token.User {
return f(user)
}
// UpdateUser update user info with UserUpdater if it exists in request's context. Otherwise do nothing.
// should be placed after either Auth, Trace. AdminOnly or RBAC middleware.
func (a *Authenticator) UpdateUser(upd UserUpdater) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// call update only if user info exists, otherwise do nothing
if user, err := token.GetUserInfo(r); err == nil {
r = token.SetUserInfo(r, upd.Update(user))
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
return f
}