@@ -5,10 +5,14 @@ import (
55 "fmt"
66 "log"
77 "net/http"
8+ "os"
89
910 "github.com/appbaseio/arc/middleware"
11+ "github.com/appbaseio/arc/middleware/classify"
12+ "github.com/appbaseio/arc/middleware/validate"
1013 "github.com/appbaseio/arc/model/category"
1114 "github.com/appbaseio/arc/model/credential"
15+ "github.com/appbaseio/arc/model/index"
1216 "github.com/appbaseio/arc/model/op"
1317 "github.com/appbaseio/arc/model/permission"
1418 "github.com/appbaseio/arc/model/user"
@@ -19,6 +23,48 @@ import (
1923 "golang.org/x/crypto/bcrypt"
2024)
2125
26+ type chain struct {
27+ middleware.Fifo
28+ }
29+
30+ func (c * chain ) Wrap (h http.HandlerFunc ) http.HandlerFunc {
31+ return c .Adapt (h , list ()... )
32+ }
33+
34+ func list () []middleware.Middleware {
35+ return []middleware.Middleware {
36+ classifyCategory ,
37+ classifyIndices ,
38+ classify .Op (),
39+ BasicAuth (),
40+ validate .Operation (),
41+ validate .Category (),
42+ }
43+ }
44+
45+ func classifyIndices (h http.HandlerFunc ) http.HandlerFunc {
46+ return func (w http.ResponseWriter , req * http.Request ) {
47+ publicKeyIndex := os .Getenv (envPublicKeyEsIndex )
48+ if publicKeyIndex == "" {
49+ publicKeyIndex = defaultPublicKeyEsIndex
50+ }
51+ ctx := index .NewContext (req .Context (), []string {publicKeyIndex })
52+ req = req .WithContext (ctx )
53+ h (w , req )
54+ }
55+ }
56+
57+ func classifyCategory (h http.HandlerFunc ) http.HandlerFunc {
58+ return func (w http.ResponseWriter , req * http.Request ) {
59+ permissionCategory := category .Auth
60+
61+ ctx := category .NewContext (req .Context (), & permissionCategory )
62+ req = req .WithContext (ctx )
63+
64+ h (w , req )
65+ }
66+ }
67+
2268// BasicAuth middleware authenticates each requests against the basic auth credentials.
2369func BasicAuth () middleware.Middleware {
2470 return Instance ().basicAuth
0 commit comments