-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext.go
44 lines (35 loc) · 1.21 KB
/
context.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
39
40
41
42
43
44
package basicauth
import (
"context"
"net/http"
)
// key is the type used for any items added to the request context.
type key uint8
const (
// userContextKey is the key for the authenticated user.
userContextKey key = iota
// logoutFuncContextKey is the key for the user logout function.
logoutFuncContextKey
)
type logoutFunc func(*http.Request) *http.Request
// GetUser returns the current authenticated User.
// If no custom user was set then it should be a type of *basicauth.SimpleUser.
func GetUser(r *http.Request) interface{} {
return r.Context().Value(userContextKey)
}
// Logout deletes the authenticated user entry from the backend.
// The client should login again on the next request.
func Logout(r *http.Request) *http.Request {
if fn, ok := r.Context().Value(logoutFuncContextKey).(logoutFunc); ok {
r = fn(r)
}
return r
}
// newContext returns a new Context with specific basicauth values.
func newContext(ctx context.Context, user interface{}, logoutFn logoutFunc) context.Context {
parent := context.WithValue(ctx, userContextKey, user)
return context.WithValue(parent, logoutFuncContextKey, logoutFn)
}
func clearContext(ctx context.Context) context.Context {
return newContext(ctx, nil, nil)
}