-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathroute.go
49 lines (41 loc) · 1.26 KB
/
route.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
package admin
import (
"errors"
"net/http"
"github.com/factly/kavach-server/action/admin/application"
"github.com/factly/kavach-server/action/admin/organisation"
"github.com/factly/kavach-server/action/admin/user"
"github.com/factly/x/loggerx"
"github.com/go-chi/chi"
"github.com/spf13/viper"
)
// Router organisation
func AdminRouter() chi.Router {
r := chi.NewRouter()
r.With(CheckMasterKey).Route("/", func(r chi.Router) {
r.Mount("/users", user.Router())
r.Mount("/organisations", organisation.Router())
r.Post("/applications/user", application.AddUser)
r.Get("/applications/{application_id}", application.ListOrgs)
r.Post("/afterRegistration", afterRegistration)
})
return r
}
// CheckMasterKey check X-User in header
func CheckMasterKey(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestMasterKey := r.Header.Get("X-KAVACH-MASTER-KEY")
masterKey := viper.GetString("master_key")
if masterKey == "" {
loggerx.Error(errors.New("master key is not set"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if requestMasterKey != masterKey {
loggerx.Error(errors.New("invalid master key"))
w.WriteHeader(http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}