fix: huertos admin permissions; improve: abstract auth guard
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
|
||||
@@ -14,14 +14,14 @@ import net.miarma.api.backlib.util.JsonUtil;
|
||||
* Maneja extracción de JWT y verificación básica.
|
||||
* Los microservicios solo implementan getUserEntity y hasPermission.
|
||||
*/
|
||||
@SuppressWarnings("unchecked") // arreglar el warning de heap pollution de los arrays de genéricos
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
|
||||
|
||||
protected abstract R parseRole(String roleStr);
|
||||
protected abstract R parseRole(String roleStr);
|
||||
protected abstract void getUserEntity(int userId, RoutingContext ctx, Consumer<U> callback);
|
||||
protected abstract boolean hasPermission(U user, R role);
|
||||
|
||||
public Handler<RoutingContext> check(R... allowedRoles) {
|
||||
|
||||
public Handler<RoutingContext> check(R... allowedRoles) {
|
||||
return ctx -> {
|
||||
String token = extractToken(ctx);
|
||||
if (token == null || !JWTManager.getInstance().isValid(token)) {
|
||||
@@ -59,14 +59,14 @@ public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
|
||||
};
|
||||
}
|
||||
|
||||
private boolean isRoleAllowed(R role, R... allowedRoles) {
|
||||
protected boolean isRoleAllowed(R role, R... allowedRoles) {
|
||||
for (R allowed : allowedRoles) {
|
||||
if (role == allowed) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String extractToken(RoutingContext ctx) {
|
||||
protected String extractToken(RoutingContext ctx) {
|
||||
String authHeader = ctx.request().getHeader("Authorization");
|
||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||
return authHeader.substring(7);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>miarma-ecosystem</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bootstrap</artifactId>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
@@ -20,7 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>huertos</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
@@ -20,7 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -2,14 +2,19 @@ package net.miarma.api.microservices.huertos.routing.middlewares;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.miarma.api.backlib.Constants.HuertosUserRole;
|
||||
import net.miarma.api.backlib.http.ApiStatus;
|
||||
import net.miarma.api.backlib.middlewares.AbstractAuthGuard;
|
||||
import net.miarma.api.backlib.security.JWTManager;
|
||||
import net.miarma.api.backlib.util.JsonUtil;
|
||||
import net.miarma.api.microservices.huertos.entities.MemberEntity;
|
||||
import net.miarma.api.microservices.huertos.services.MemberService;
|
||||
|
||||
public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUserRole> {
|
||||
private final MemberService memberService;
|
||||
|
||||
private final MemberService memberService;
|
||||
|
||||
public HuertosAuthGuard(MemberService memberService) {
|
||||
this.memberService = memberService;
|
||||
@@ -22,7 +27,7 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
||||
|
||||
@Override
|
||||
protected void getUserEntity(int userId, RoutingContext ctx, Consumer<MemberEntity> callback) {
|
||||
memberService.getById(userId).onComplete(ar -> {
|
||||
memberService.getById(userId).onComplete(ar -> {
|
||||
if (ar.succeeded()) callback.accept(ar.result());
|
||||
else callback.accept(null);
|
||||
});
|
||||
@@ -32,4 +37,39 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
||||
protected boolean hasPermission(MemberEntity user, HuertosUserRole role) {
|
||||
return user.getRole() == HuertosUserRole.ADMIN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Handler<RoutingContext> check(HuertosUserRole... allowedRoles) {
|
||||
return ctx -> {
|
||||
String token = extractToken(ctx);
|
||||
if (token == null || !JWTManager.getInstance().isValid(token)) {
|
||||
JsonUtil.sendJson(ctx, ApiStatus.UNAUTHORIZED, "Invalid or missing token");
|
||||
return;
|
||||
}
|
||||
|
||||
int userId = JWTManager.getInstance().extractUserId(token);
|
||||
|
||||
getUserEntity(userId, ctx, member -> {
|
||||
if (member == null) {
|
||||
JsonUtil.sendJson(ctx, ApiStatus.UNAUTHORIZED, "User not found");
|
||||
return;
|
||||
}
|
||||
|
||||
HuertosUserRole role = HuertosUserRole.USER;
|
||||
if (member.getRole() != null) {
|
||||
role = member.getRole();
|
||||
}
|
||||
|
||||
ctx.put("userId", userId);
|
||||
ctx.put("role", role);
|
||||
ctx.put("userEntity", member);
|
||||
|
||||
if (allowedRoles.length == 0 || isRoleAllowed(role, allowedRoles)) {
|
||||
ctx.next();
|
||||
} else {
|
||||
JsonUtil.sendJson(ctx, ApiStatus.FORBIDDEN, "Forbidden");
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>huertosdecine</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
@@ -20,7 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>miarmacraft</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
@@ -20,7 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>mpaste</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
@@ -20,7 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user