2024-12-12 13:27:07 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-12-15 06:36:33 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2024-12-12 13:27:07 +00:00
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors"
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateAuthGuard(cfg *config.Config) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Получаем значение из заголовка
|
|
|
|
token := r.Header.Get("Authorization")
|
|
|
|
if token != "Bearer "+cfg.Token {
|
|
|
|
render.Render(w, r, errors.ErrUnauthorized())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|