@@ -18,8 +18,9 @@ import (
18
18
// CustomRouter wraps gorilla mux with database, redis and renderer
19
19
type CustomRouter struct {
20
20
// Router *mux.Router
21
- Mux * bone.Mux
22
- Store * datastore.Datastore
21
+ Mux * bone.Mux
22
+ Store * datastore.Datastore
23
+ AuthHandler func (store * datastore.Datastore , fn http.HandlerFunc , authMethod string ) http.HandlerFunc
23
24
}
24
25
25
26
func New (store * datastore.Datastore ) * CustomRouter {
@@ -31,6 +32,20 @@ func New(store *datastore.Datastore) *CustomRouter {
31
32
// r.Handle("/attachments/", http.StripPrefix("/attachments/", http.FileServer(http.Dir(store.Settings.AttachmentsFolder))))
32
33
customRouter .Mux = r
33
34
customRouter .Store = store
35
+ customRouter .AuthHandler = authenticate
36
+ return customRouter
37
+ }
38
+
39
+ func CustomAuth (store * datastore.Datastore , authFn func (store * datastore.Datastore , fn http.HandlerFunc , authMethod string ) http.HandlerFunc ) * CustomRouter {
40
+ customRouter := & CustomRouter {}
41
+ r := bone .New ()
42
+ r .CaseSensitive = false
43
+ // r.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public/"))))
44
+ // r.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets/"))))
45
+ // r.Handle("/attachments/", http.StripPrefix("/attachments/", http.FileServer(http.Dir(store.Settings.AttachmentsFolder))))
46
+ customRouter .Mux = r
47
+ customRouter .Store = store
48
+ customRouter .AuthHandler = authFn
34
49
return customRouter
35
50
}
36
51
@@ -42,63 +57,63 @@ func (customRouter *CustomRouter) Application(route string, path string) *bone.R
42
57
43
58
func (customRouter * CustomRouter ) GET (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
44
59
//route = strings.ToLower(route)
45
- return customRouter .Mux .GetFunc (route , CustomHandler ("GET" , customRouter .Store , routeFunc , securityType ))
60
+ return customRouter .Mux .GetFunc (route , customRouter . customHandler ("GET" , customRouter .Store , routeFunc , securityType ))
46
61
}
47
62
48
63
// POST - Post handler
49
64
func (customRouter * CustomRouter ) POST (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
50
65
//route = strings.ToLower(route)
51
- return customRouter .Mux .PostFunc (route , CustomHandler ("POST" , customRouter .Store , routeFunc , securityType ))
66
+ return customRouter .Mux .PostFunc (route , customRouter . customHandler ("POST" , customRouter .Store , routeFunc , securityType ))
52
67
}
53
68
54
69
// PST - Post handler with pst for tidier lines
55
70
func (customRouter * CustomRouter ) PST (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
56
71
//route = strings.ToLower(route)
57
- return customRouter .Mux .PostFunc (route , CustomHandler ("POST" , customRouter .Store , routeFunc , securityType ))
72
+ return customRouter .Mux .PostFunc (route , customRouter . customHandler ("POST" , customRouter .Store , routeFunc , securityType ))
58
73
}
59
74
60
75
// PUT - Put handler
61
76
func (customRouter * CustomRouter ) PUT (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
62
77
//route = strings.ToLower(route)
63
- return customRouter .Mux .PutFunc (route , CustomHandler ("PUT" , customRouter .Store , routeFunc , securityType ))
78
+ return customRouter .Mux .PutFunc (route , customRouter . customHandler ("PUT" , customRouter .Store , routeFunc , securityType ))
64
79
}
65
80
66
81
// PATCH - Patch handler
67
82
func (customRouter * CustomRouter ) PATCH (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
68
83
//route = strings.ToLower(route)
69
- return customRouter .Mux .PatchFunc (route , CustomHandler ("PATCH" , customRouter .Store , routeFunc , securityType ))
84
+ return customRouter .Mux .PatchFunc (route , customRouter . customHandler ("PATCH" , customRouter .Store , routeFunc , securityType ))
70
85
}
71
86
72
87
// OPTIONS - Options handler
73
88
func (customRouter * CustomRouter ) OPTIONS (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
74
89
//route = strings.ToLower(route)
75
- return customRouter .Mux .OptionsFunc (route , CustomHandler ("OPTIONS" , customRouter .Store , routeFunc , securityType ))
90
+ return customRouter .Mux .OptionsFunc (route , customRouter . customHandler ("OPTIONS" , customRouter .Store , routeFunc , securityType ))
76
91
}
77
92
78
93
// DELETE - Delete handler
79
94
func (customRouter * CustomRouter ) DELETE (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
80
95
//route = strings.ToLower(route)
81
- return customRouter .Mux .DeleteFunc (route , CustomHandler ("DELETE" , customRouter .Store , routeFunc , securityType ))
96
+ return customRouter .Mux .DeleteFunc (route , customRouter . customHandler ("DELETE" , customRouter .Store , routeFunc , securityType ))
82
97
}
83
98
84
99
// DEL - Delete handler
85
100
func (customRouter * CustomRouter ) DEL (route string , routeFunc CustomHandlerFunc , securityType string ) * bone.Route {
86
101
//route = strings.ToLower(route)
87
- return customRouter .Mux .DeleteFunc (route , CustomHandler ("DELETE" , customRouter .Store , routeFunc , securityType ))
102
+ return customRouter .Mux .DeleteFunc (route , customRouter . customHandler ("DELETE" , customRouter .Store , routeFunc , securityType ))
88
103
}
89
104
90
- func CustomHandler (reqType string , store * datastore.Datastore , fn CustomHandlerFunc , authMethod string ) http.HandlerFunc {
91
- return authenticate (store , func (w http.ResponseWriter , req * http.Request ) {
105
+ func ( customRouter * CustomRouter ) customHandler (reqType string , store * datastore.Datastore , fn CustomHandlerFunc , authMethod string ) http.HandlerFunc {
106
+ return customRouter . AuthHandler (store , func (w http.ResponseWriter , req * http.Request ) {
92
107
fn (flow .New (w , req , store ))
93
108
}, authMethod )
94
109
}
95
110
96
- // DefaultHandler wraps default http functions in auth it does not pass the data store to them
97
- func DefaultHandler (store * datastore.Datastore , fn http.HandlerFunc , authMethod string ) http.HandlerFunc {
98
- return authenticate (store , func (w http.ResponseWriter , req * http.Request ) {
99
- fn (w , req )
100
- }, authMethod )
101
- }
111
+ // // DefaultHandler wraps default http functions in auth it does not pass the data store to them
112
+ // func DefaultHandler(store *datastore.Datastore, fn http.HandlerFunc, authMethod string) http.HandlerFunc {
113
+ // return authenticate(store, func(w http.ResponseWriter, req *http.Request) {
114
+ // fn(w, req)
115
+ // }, authMethod)
116
+ // }
102
117
103
118
func authenticate (store * datastore.Datastore , fn http.HandlerFunc , authMethod string ) http.HandlerFunc {
104
119
return func (w http.ResponseWriter , req * http.Request ) {
@@ -170,11 +185,6 @@ func authenticate(store *datastore.Datastore, fn http.HandlerFunc, authMethod st
170
185
fn (w , req )
171
186
return
172
187
}
173
- // else if err != nil { // we dont care about the error
174
- // // WE ONLY check the error after the above because if we aren't authenticating then we will get an error
175
- // view.JSON(w, http.StatusForbidden, err.Error())
176
- // return
177
- // }
178
188
179
189
// if we have reached this point then the user doesn't have access
180
190
if authMethod == security .Disallow {
0 commit comments