-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
77 lines (60 loc) · 2.13 KB
/
auth.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
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 goal
import "net/http"
// Registerer register a new user to system
type Registerer interface {
Register(http.ResponseWriter, *http.Request) (int, interface{}, error)
}
// Loginer authenticates user into the system
type Loginer interface {
Login(http.ResponseWriter, *http.Request) (int, interface{}, error)
}
// Logouter clear sessions and log user out
type Logouter interface {
Logout(http.ResponseWriter, *http.Request) (int, interface{}, error)
}
func (api *API) registerHandler(resource interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, request *http.Request) {
var handler simpleResponse
if resource, ok := resource.(Registerer); ok {
handler = resource.Register
}
renderJSON(rw, request, handler)
}
}
func (api *API) loginHandler(resource interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, request *http.Request) {
var handler simpleResponse
if resource, ok := resource.(Loginer); ok {
handler = resource.Login
}
renderJSON(rw, request, handler)
}
}
func (api *API) logoutHandler(resource interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, request *http.Request) {
var handler simpleResponse
if resource, ok := resource.(Logouter); ok {
handler = resource.Logout
}
renderJSON(rw, request, handler)
}
}
// AddRegisterPath let user to register into a system
func (api *API) AddRegisterPath(resource interface{}, path string) {
api.Mux().Handle(path, api.registerHandler(resource))
}
// AddLoginPath let user login to system
func (api *API) AddLoginPath(resource interface{}, path string) {
api.Mux().Handle(path, api.loginHandler(resource))
}
// AddLogoutPath let user logout from the system
func (api *API) AddLogoutPath(resource interface{}, path string) {
api.Mux().Handle(path, api.logoutHandler(resource))
}
// AddDefaultAuthPaths route request to the model which implement
// authentications
func (api *API) AddDefaultAuthPaths(resource interface{}) {
api.Mux().Handle("/auth/register", api.registerHandler(resource))
api.Mux().Handle("/auth/login", api.loginHandler(resource))
api.Mux().Handle("/auth/logout", api.logoutHandler(resource))
}