Merge pull request 'fix: huertos admin permissions; improve: abstract auth guard' (#2) from hotfix/huertos-admin-permissions into main
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<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.
|
* Maneja extracción de JWT y verificación básica.
|
||||||
* Los microservicios solo implementan getUserEntity y hasPermission.
|
* 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> {
|
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 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);
|
||||||
|
|
||||||
public Handler<RoutingContext> check(R... allowedRoles) {
|
public Handler<RoutingContext> check(R... allowedRoles) {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
String token = extractToken(ctx);
|
String token = extractToken(ctx);
|
||||||
if (token == null || !JWTManager.getInstance().isValid(token)) {
|
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) {
|
for (R allowed : allowedRoles) {
|
||||||
if (role == allowed) return true;
|
if (role == allowed) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractToken(RoutingContext ctx) {
|
protected String extractToken(RoutingContext ctx) {
|
||||||
String authHeader = ctx.request().getHeader("Authorization");
|
String authHeader = ctx.request().getHeader("Authorization");
|
||||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||||
return authHeader.substring(7);
|
return authHeader.substring(7);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>miarma-ecosystem</artifactId>
|
<artifactId>miarma-ecosystem</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>bootstrap</artifactId>
|
<artifactId>bootstrap</artifactId>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>core</artifactId>
|
<artifactId>core</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>huertos</artifactId>
|
<artifactId>huertos</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,19 @@ package net.miarma.api.microservices.huertos.routing.middlewares;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import net.miarma.api.backlib.Constants.HuertosUserRole;
|
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.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.entities.MemberEntity;
|
||||||
import net.miarma.api.microservices.huertos.services.MemberService;
|
import net.miarma.api.microservices.huertos.services.MemberService;
|
||||||
|
|
||||||
public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUserRole> {
|
public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUserRole> {
|
||||||
private final MemberService memberService;
|
|
||||||
|
private final MemberService memberService;
|
||||||
|
|
||||||
public HuertosAuthGuard(MemberService memberService) {
|
public HuertosAuthGuard(MemberService memberService) {
|
||||||
this.memberService = memberService;
|
this.memberService = memberService;
|
||||||
@@ -22,7 +27,7 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void getUserEntity(int userId, RoutingContext ctx, Consumer<MemberEntity> callback) {
|
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());
|
if (ar.succeeded()) callback.accept(ar.result());
|
||||||
else callback.accept(null);
|
else callback.accept(null);
|
||||||
});
|
});
|
||||||
@@ -32,4 +37,39 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
|||||||
protected boolean hasPermission(MemberEntity user, HuertosUserRole role) {
|
protected boolean hasPermission(MemberEntity user, HuertosUserRole role) {
|
||||||
return user.getRole() == HuertosUserRole.ADMIN;
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>huertosdecine</artifactId>
|
<artifactId>huertosdecine</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>miarmacraft</artifactId>
|
<artifactId>miarmacraft</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>mpaste</artifactId>
|
<artifactId>mpaste</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>23</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>backlib</artifactId>
|
<artifactId>backlib</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>miarma-ecosystem</artifactId>
|
<artifactId>miarma-ecosystem</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
|
|||||||
Reference in New Issue
Block a user