Compare commits
4 Commits
f372cbe8ab
...
2611ab62d5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2611ab62d5 | ||
|
|
748feabf24 | ||
|
|
14ea92dab9 | ||
|
|
92847e87aa |
@@ -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,13 +14,13 @@ 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, R... allowedRoles);
|
||||
|
||||
protected abstract boolean hasPermission(U user, R role);
|
||||
|
||||
public Handler<RoutingContext> check(R... allowedRoles) {
|
||||
return ctx -> {
|
||||
String token = extractToken(ctx);
|
||||
@@ -49,15 +49,21 @@ public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
|
||||
};
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
protected boolean isRoleAllowed(R role, R... allowedRoles) {
|
||||
for (R allowed : allowedRoles) {
|
||||
if (role == allowed) return true;
|
||||
=======
|
||||
protected R extractRoleFromEntity(U user) {
|
||||
try {
|
||||
return (R) user.getClass().getMethod("getRole").invoke(user);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
>>>>>>> refs/remotes/origin/dev
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
<parent>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>miarma-backend</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<artifactId>miarma-ecosystem</artifactId>
|
||||
<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,9 +20,8 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
|
||||
@@ -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,9 +20,8 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -36,6 +41,44 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
||||
if (member.getRole() == role) return true;
|
||||
}
|
||||
return false;
|
||||
<<<<<<< HEAD
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
});
|
||||
};
|
||||
=======
|
||||
>>>>>>> refs/remotes/origin/dev
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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,8 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>provided</scope>
|
||||
<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,8 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>provided</scope>
|
||||
<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,8 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>net.miarma.api</groupId>
|
||||
<artifactId>backlib</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<scope>provided</scope>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user