Libera listagem de condominios por api key
This commit is contained in:
@@ -84,6 +84,7 @@ public final class Main {
|
||||
router.post("/api/integracao/mensagens", authMiddleware.exigirApiKey(externalBridgeHandler));
|
||||
router.prefix("/api/master/", authMiddleware.exigirMaster(masterHandlers));
|
||||
router.prefix("/api/financeiro/", authMiddleware.exigirFuncionario(financeiroHandlers));
|
||||
router.get("/api/condominios", authMiddleware.exigirFuncionarioOuApiKeyLeitura(clienteHandlers));
|
||||
router.prefix("/api/", authMiddleware.exigirFuncionario(clienteHandlers));
|
||||
router.fallback(new StaticFileHandler());
|
||||
|
||||
|
||||
@@ -54,9 +54,39 @@ public final class AuthMiddleware {
|
||||
};
|
||||
}
|
||||
|
||||
public HttpHandler exigirFuncionarioOuApiKeyLeitura(HttpHandler next) {
|
||||
return exchange -> {
|
||||
var claims = bearerClaims(exchange);
|
||||
if (claims != null && "empresa".equals(claims.get("escopo"))) {
|
||||
exchange.setAttribute(ATTR_EMPRESA_ID, toLong(claims.get("empresa_id")));
|
||||
exchange.setAttribute(ATTR_FUNCIONARIO_ID, toLong(claims.get("funcionario_id")));
|
||||
exchange.setAttribute(ATTR_CARGO, String.valueOf(claims.getOrDefault("cargo", "")));
|
||||
next.handle(exchange);
|
||||
return;
|
||||
}
|
||||
if (!"GET".equals(exchange.getRequestMethod())) {
|
||||
HttpResponses.json(exchange, 401, Map.of("erro", "nao_autorizado"));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var empresaId = apiKeyService.validar(apiKeyHeader(exchange));
|
||||
if (empresaId.isEmpty()) {
|
||||
HttpResponses.json(exchange, 401, Map.of("erro", "api_key_invalida"));
|
||||
return;
|
||||
}
|
||||
exchange.setAttribute(ATTR_EMPRESA_ID, empresaId.getAsLong());
|
||||
exchange.setAttribute(ATTR_FUNCIONARIO_ID, 0L);
|
||||
exchange.setAttribute(ATTR_CARGO, "admin_empresa");
|
||||
next.handle(exchange);
|
||||
} catch (SQLException e) {
|
||||
HttpResponses.json(exchange, 500, Map.of("erro", "erro_validacao_api_key"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public HttpHandler exigirApiKey(HttpHandler next) {
|
||||
return exchange -> {
|
||||
String chave = exchange.getRequestHeaders().getFirst("X-API-Key");
|
||||
String chave = apiKeyHeader(exchange);
|
||||
try {
|
||||
var empresaId = apiKeyService.validar(chave);
|
||||
if (empresaId.isEmpty()) {
|
||||
@@ -71,6 +101,14 @@ public final class AuthMiddleware {
|
||||
};
|
||||
}
|
||||
|
||||
private String apiKeyHeader(HttpExchange exchange) {
|
||||
String chave = exchange.getRequestHeaders().getFirst("X-API-Key");
|
||||
if (chave != null && !chave.isBlank()) return chave.trim();
|
||||
String auth = exchange.getRequestHeaders().getFirst("Authorization");
|
||||
if (auth != null && auth.startsWith("Bearer ")) return auth.substring("Bearer ".length()).trim();
|
||||
return chave;
|
||||
}
|
||||
|
||||
private Map<String, Object> bearerClaims(HttpExchange exchange) throws IOException {
|
||||
String auth = exchange.getRequestHeaders().getFirst("Authorization");
|
||||
if (auth == null || !auth.startsWith("Bearer ")) {
|
||||
|
||||
Reference in New Issue
Block a user