garm/auth/init_required.go
Gabriel Adrian Samfira 0883fcd5cd Add some basic auth
2022-04-28 16:13:20 +00:00

34 lines
876 B
Go

package auth
import (
"encoding/json"
"net/http"
"runner-manager/apiserver/params"
"runner-manager/database/common"
)
// NewjwtMiddleware returns a populated jwtMiddleware
func NewInitRequiredMiddleware(store common.Store) (Middleware, error) {
return &initRequired{
store: store,
}, nil
}
type initRequired struct {
store common.Store
}
// Middleware implements the middleware interface
func (i *initRequired) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctrlInfo, err := i.store.ControllerInfo()
if err != nil || ctrlInfo.ControllerID.String() == "" {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(params.InitializationRequired)
return
}
ctx := r.Context()
next.ServeHTTP(w, r.WithContext(ctx))
})
}