using Microsoft.AspNetCore.Mvc; using Server.WatchBody.Web.Services; namespace Server.WatchBody.Web.Controllers.Admin; /// /// База admin-контроллеров (spec 01 §3): Bearer-токен обязателен, /// без/невалидный → 401 {"error":"unauthorized"}; атлетские /// X-Api-Key в этом контуре не действуют (🛡1 — middleware их сюда /// не пускает). /// public abstract class AdminControllerBase(AdminAuthService auth) : Controller { protected readonly AdminAuthService Auth = auth; /// 401, если токен не прошёл; иначе false. protected bool Denied() { var header = Request.Headers.Authorization.FirstOrDefault(); var token = header?.StartsWith("Bearer ", StringComparison.Ordinal) == true ? header["Bearer ".Length..] : null; return !Auth.Validate(token); } protected ActionResult UnauthorizedJson() => ErrorJson(401, "unauthorized"); protected ActionResult ErrorJson(int code, string message) { Response.StatusCode = code; return new JsonResult(new { error = message }); } }