1 Commits

Author SHA1 Message Date
Jose
d3f4330fa8 add: generic JWT token extraction
refactor: change project and POMs' names accordingly
2025-10-31 17:13:42 +01:00
11 changed files with 34 additions and 78 deletions

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>

View File

@@ -14,12 +14,12 @@ 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")
@SuppressWarnings("unchecked") // arreglar el warning de heap pollution de los arrays de genéricos
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);
protected abstract boolean hasPermission(U user, R role, R... allowedRoles);
public Handler<RoutingContext> check(R... allowedRoles) {
return ctx -> {
@@ -49,21 +49,15 @@ 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
}
}
protected String extractToken(RoutingContext ctx) {
private String extractToken(RoutingContext ctx) {
String authHeader = ctx.request().getHeader("Authorization");
if (authHeader != null && authHeader.startsWith("Bearer ")) {
return authHeader.substring(7);

View File

@@ -5,8 +5,8 @@
<parent>
<groupId>net.miarma.api</groupId>
<artifactId>miarma-ecosystem</artifactId>
<version>1.2.1</version>
<artifactId>miarma-backend</artifactId>
<version>1.2.0</version>
</parent>
<artifactId>bootstrap</artifactId>

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>core</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
@@ -20,8 +20,9 @@
<dependency>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
</dependency>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>huertos</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
@@ -20,8 +20,9 @@
<dependency>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
</dependency>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -2,19 +2,14 @@ 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;
@@ -27,7 +22,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);
});
@@ -41,44 +36,6 @@ 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
}
}

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>huertosdecine</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
@@ -20,7 +20,8 @@
<dependency>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>miarmacraft</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
@@ -20,7 +20,8 @@
<dependency>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@@ -19,9 +19,9 @@ public class MMCDataRouter {
router.route().handler(BodyHandler.create());
router.get(MMCEndpoints.MODS).handler(hModData::getAll);
router.get(MMCEndpoints.MOD).handler(hModData::getById);
router.post(MMCEndpoints.MODS).handler(BodyHandler.create().setBodyLimit(100 * 1024 * 1024)).handler(authGuard.check(MMCUserRole.ADMIN)).handler(hModData::create);
router.get(MMCEndpoints.MODS).handler(authGuard.check()).handler(hModData::getAll);
router.get(MMCEndpoints.MOD).handler(authGuard.check()).handler(hModData::getById);
router.post(MMCEndpoints.MODS).handler(BodyHandler.create().setBodyLimit(100 * 1024 * 1024)).handler(authGuard.check()).handler(hModData::create);
router.put(MMCEndpoints.MOD).handler(authGuard.check(MMCUserRole.ADMIN)).handler(hModData::update);
router.delete(MMCEndpoints.MOD).handler(authGuard.check(MMCUserRole.ADMIN)).handler(hModData::delete);

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>mpaste</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
@@ -20,7 +20,8 @@
<dependency>
<groupId>net.miarma.api</groupId>
<artifactId>backlib</artifactId>
<version>1.2.1</version>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@@ -4,8 +4,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.miarma.api</groupId>
<artifactId>miarma-ecosystem</artifactId>
<version>1.2.1</version>
<artifactId>miarma-backend</artifactId>
<version>1.2.0</version>
<packaging>pom</packaging>
<modules>