This commit is contained in:
@@ -21,6 +21,7 @@ type (
|
||||
Server *http.Server
|
||||
Service *data.Service
|
||||
documentRoot string
|
||||
creds map[string]string
|
||||
}
|
||||
)
|
||||
|
||||
@@ -32,6 +33,7 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
||||
s := &HTTPServer{
|
||||
Service: srv,
|
||||
documentRoot: documentRoot,
|
||||
creds: creds,
|
||||
}
|
||||
router := chi.NewRouter()
|
||||
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
|
||||
@@ -46,7 +48,7 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
||||
router.Use(middleware.Compress(5, "application/gzip"))
|
||||
router.Use(middleware.Heartbeat("/heartbeat"))
|
||||
router.Route("/api", func(routerAPI chi.Router) {
|
||||
routerAPI.Use(BasicAuth("cloudsave", creds))
|
||||
routerAPI.Use(s.BasicAuth("cloudsave"))
|
||||
routerAPI.Route("/v1", func(r chi.Router) {
|
||||
// Get information about the server
|
||||
r.Get("/version", s.Information)
|
||||
@@ -78,6 +80,10 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *HTTPServer) SetCredentials(creds map[string]string) {
|
||||
s.creds = creds
|
||||
}
|
||||
|
||||
func (s HTTPServer) all(w http.ResponseWriter, r *http.Request) {
|
||||
datastore, err := s.Service.AllGames()
|
||||
if err != nil {
|
||||
|
||||
@@ -20,7 +20,7 @@ func recoverMiddleware(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// BasicAuth implements a simple middleware handler for adding basic http auth to a route.
|
||||
func BasicAuth(realm string, creds map[string]string) func(next http.Handler) http.Handler {
|
||||
func (s *HTTPServer) BasicAuth(realm string) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
@@ -29,7 +29,7 @@ func BasicAuth(realm string, creds map[string]string) func(next http.Handler) ht
|
||||
return
|
||||
}
|
||||
|
||||
credPass := creds[user]
|
||||
credPass := s.creds[user]
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(credPass), []byte(pass)); err != nil {
|
||||
basicAuthFailed(w, r, realm)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user