Compare commits
4 Commits
f372cbe8ab
...
2611ab62d5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2611ab62d5 | ||
|
|
748feabf24 | ||
|
|
14ea92dab9 | ||
|
|
92847e87aa |
@@ -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,12 +14,12 @@ 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, R... allowedRoles);
|
protected abstract boolean hasPermission(U user, R role);
|
||||||
|
|
||||||
public Handler<RoutingContext> check(R... allowedRoles) {
|
public Handler<RoutingContext> check(R... allowedRoles) {
|
||||||
return ctx -> {
|
return 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) {
|
protected R extractRoleFromEntity(U user) {
|
||||||
try {
|
try {
|
||||||
return (R) user.getClass().getMethod("getRole").invoke(user);
|
return (R) user.getClass().getMethod("getRole").invoke(user);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
|
>>>>>>> refs/remotes/origin/dev
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>miarma-backend</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,9 +20,8 @@
|
|||||||
<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>
|
||||||
<scope>provided</scope>
|
</dependency>
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|||||||
@@ -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,9 +20,8 @@
|
|||||||
<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>
|
||||||
<scope>provided</scope>
|
</dependency>
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -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);
|
||||||
});
|
});
|
||||||
@@ -36,6 +41,44 @@ public class HuertosAuthGuard extends AbstractAuthGuard<MemberEntity, HuertosUse
|
|||||||
if (member.getRole() == role) return true;
|
if (member.getRole() == role) return true;
|
||||||
}
|
}
|
||||||
return false;
|
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>
|
<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,8 +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>
|
||||||
<scope>provided</scope>
|
|
||||||
</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,8 +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>
|
||||||
<scope>provided</scope>
|
|
||||||
</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,8 +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>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
4
pom.xml
4
pom.xml
@@ -4,8 +4,8 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>net.miarma.api</groupId>
|
<groupId>net.miarma.api</groupId>
|
||||||
<artifactId>miarma-backend</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