add: generic JWT token extraction

refactor: change project and POMs' names accordingly
This commit is contained in:
2025-10-31 17:13:42 +01:00
parent 14ea92dab9
commit 748feabf24
17 changed files with 88 additions and 277 deletions

View File

@@ -132,33 +132,4 @@
</dependency>
</dependencies>
<build>
<finalName>BackLib</finalName>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.miarma.backlib.MainVerticle</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -30,18 +30,6 @@ public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
}
int userId = JWTManager.getInstance().extractUserId(token);
String roleStr = JWTManager.getInstance().extractRole(token);
R role;
try {
role = parseRole(roleStr);
} catch (Exception e) {
JsonUtil.sendJson(ctx, ApiStatus.UNAUTHORIZED, "Invalid role");
return;
}
ctx.put("userId", userId);
ctx.put("role", role);
getUserEntity(userId, ctx, entity -> {
if (entity == null) {
@@ -49,7 +37,9 @@ public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
return;
}
if (allowedRoles.length == 0 || isRoleAllowed(role, allowedRoles)) {
R userRole = extractRoleFromEntity(entity);
if (allowedRoles.length == 0 || hasPermission(entity, userRole, allowedRoles)) {
ctx.put("userEntity", entity);
ctx.next();
} else {
@@ -58,12 +48,11 @@ public abstract class AbstractAuthGuard<U, R extends Enum<R> & IUserRole> {
});
};
}
protected boolean isRoleAllowed(R role, R... allowedRoles) {
for (R allowed : allowedRoles) {
if (role == allowed) return true;
}
return false;
}
protected String extractToken(RoutingContext ctx) {

View File

@@ -1,14 +1,15 @@
package net.miarma.api.backlib.security;
import java.util.Date;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import net.miarma.api.backlib.ConfigManager;
import net.miarma.api.backlib.Constants;
import net.miarma.api.backlib.Constants.CoreUserRole;
import java.util.Date;
import net.miarma.api.backlib.interfaces.IUserRole;
/**
* Clase de gestión de JSON Web Tokens (JWT).
@@ -49,7 +50,7 @@ public class JWTManager {
* @param keepLoggedIn Indica si el token debe tener una duración prolongada.
* @return El token JWT generado.
*/
public String generateToken(String user_name, Integer user_id, CoreUserRole role, boolean keepLoggedIn) {
public String generateToken(String user_name, Integer user_id, IUserRole role, boolean keepLoggedIn) {
final long EXPIRATION_TIME_MS = 1000L * (keepLoggedIn ? config.getIntProperty("jwt.expiration") : config.getIntProperty("jwt.expiration.short"));
return JWT.create()
.withSubject(user_name)