-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_authentication.go
43 lines (34 loc) 路 1.23 KB
/
server_authentication.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
package web
import "net/http"
type Authenticator interface {
Routes() Routes
RequireAuthentication(http.HandlerFunc) http.HandlerFunc
IsAuthenticated(w http.ResponseWriter, r *http.Request) bool
Authenticate(w http.ResponseWriter, r *http.Request)
Deauthenticate(w http.ResponseWriter, r *http.Request)
ReadSessionValue(w http.ResponseWriter, r *http.Request, key string) (interface{}, bool)
}
type exampleAuthenticator struct{}
func (a *exampleAuthenticator) RequireAuthentication(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !a.IsAuthenticated(w, r) {
http.NotFound(w, r)
return
}
h(w, r)
}
}
func (a *exampleAuthenticator) IsAuthenticated(w http.ResponseWriter, r *http.Request) bool {
return false
}
func (a *exampleAuthenticator) ReadSessionValue(w http.ResponseWriter, r *http.Request, key string) (interface{}, bool) {
return nil, false
}
func (a *exampleAuthenticator) Authenticate(w http.ResponseWriter, r *http.Request) {}
func (a *exampleAuthenticator) Deauthenticate(w http.ResponseWriter, r *http.Request) {}
func (a *exampleAuthenticator) Routes() Routes {
return Routes{
"/auth/example/login": a.Authenticate,
"/auth/example/logout": a.Deauthenticate,
}
}