From 8d5386be38db25a2a41c3bf6c876adee21ca26cc Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 19 Sep 2014 08:26:02 +0000
Subject: [PATCH] #396 fix - Fixed more SonarQube issues
---
securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java | 156 +++++++++++++++++++++++++--------------------------
1 files changed, 76 insertions(+), 80 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java b/securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java
index 3f5c11c..3e68f4c 100644
--- a/securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java
+++ b/securis/src/main/java/net/curisit/securis/security/SecurityInterceptor.java
@@ -34,98 +34,94 @@
// @PreMatching
@Priority(Priorities.AUTHENTICATION)
public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter {
- private static final Logger LOG = LogManager.getLogger(SecurityInterceptor.class);
+ private static final Logger LOG = LogManager.getLogger(SecurityInterceptor.class);
- @Inject
- private TokenHelper tokenHelper;
+ @Inject
+ private TokenHelper tokenHelper;
- @Context
- private HttpServletRequest servletRequest;
+ @Context
+ private HttpServletRequest servletRequest;
- @Inject
- CacheTTL cache;
+ @Inject
+ CacheTTL cache;
- @Context
- Dispatcher dispatcher;
+ @Context
+ Dispatcher dispatcher;
- @Inject
- com.google.inject.Provider<EntityManager> emProvider;
+ @Inject
+ com.google.inject.Provider<EntityManager> emProvider;
- public void filter(ContainerRequestContext containerRequestContext) throws IOException {
- ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) containerRequestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
- Method method = methodInvoker.getMethod();
+ public void filter(ContainerRequestContext containerRequestContext) throws IOException {
+ ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) containerRequestContext
+ .getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
+ Method method = methodInvoker.getMethod();
- if (!method.isAnnotationPresent(Securable.class))
- return;
- String token = servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM);
- if (token == null || !tokenHelper.isTokenValid(token)) {
- LOG.info("Access denied to '{}', Token not valid.", servletRequest.getPathInfo());
- containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
- } else {
- Securable sec = method.getAnnotation(Securable.class);
+ if (!method.isAnnotationPresent(Securable.class)) {
+ return;
+ }
+ String token = servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM);
+ if (token == null || !tokenHelper.isTokenValid(token)) {
+ LOG.info("Access denied to '{}', Token not valid.", servletRequest.getPathInfo());
+ containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
+ } else {
+ Securable sec = method.getAnnotation(Securable.class);
- // If roles == 0 we only need to validate the token
- String username = tokenHelper.extractUserFromToken(token);
- int userRoles = getUserRoles(username);
- // if (sec.roles() != 0) {
- // if ((sec.roles() & userRoles) == 0) {
- // LOG.info("User {} has no necessary role to access url: {}", username, servletRequest.getPathInfo());
- // containerRequestContext.abortWith(Response.status(Status.UNAUTHORIZED).build());
- // }
- // }
- Set<Integer> orgs = getUserOrganizations(username);
+ // If roles == 0 we only need to validate the token
+ String username = tokenHelper.extractUserFromToken(token);
+ int userRoles = getUserRoles(username);
+ Set<Integer> orgs = getUserOrganizations(username);
- BasicSecurityContext scw = new BasicSecurityContext(username, userRoles, servletRequest.isSecure());
- scw.setOrganizationsIds(orgs);
- containerRequestContext.setSecurityContext(scw);
- // Next line provide injection in resource methods
- ResteasyProviderFactory.pushContext(BasicSecurityContext.class, scw);
- LOG.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs);
- }
- }
+ BasicSecurityContext scw = new BasicSecurityContext(username, userRoles, servletRequest.isSecure());
+ scw.setOrganizationsIds(orgs);
+ containerRequestContext.setSecurityContext(scw);
+ // Next line provide injection in resource methods
+ ResteasyProviderFactory.pushContext(BasicSecurityContext.class, scw);
+ LOG.debug("Added custom SecurityContext for user {}, orgs: {}", username, orgs);
+ }
+ }
- private Set<Integer> getUserOrganizations(String username) {
- @SuppressWarnings("unchecked")
- Set<Integer> userOrgs = cache.get("orgs_" + username, Set.class);
- if (userOrgs == null) {
- // Theorically this shouldn't be never null, but just in case...
- EntityManager em = emProvider.get();
- User user = em.find(User.class, username);
- if (user != null) {
- userOrgs = user.getAllOrgsIds();
- // We store user orgs in cache only for one hour
- cache.set("orgs_" + username, userOrgs, 3600);
- }
- }
+ private Set<Integer> getUserOrganizations(String username) {
+ @SuppressWarnings("unchecked")
+ Set<Integer> userOrgs = cache.get("orgs_" + username, Set.class);
+ if (userOrgs == null) {
+ // Theorically this shouldn't be never null, but just in case...
+ EntityManager em = emProvider.get();
+ User user = em.find(User.class, username);
+ if (user != null) {
+ userOrgs = user.getAllOrgsIds();
+ // We store user orgs in cache only for one hour
+ cache.set("orgs_" + username, userOrgs, 3600);
+ }
+ }
- return userOrgs;
- }
+ return userOrgs;
+ }
- private int getUserRoles(String username) {
- if (username == null)
- return 0;
- Integer userRoles = cache.get("roles_" + username, Integer.class);
- if (userRoles == null) {
- EntityManager em = emProvider.get();
- User user = em.find(User.class, username);
- if (user != null) {
- userRoles = 0;
- List<Integer> roles = user.getRoles();
- for (Integer rol : roles) {
- userRoles += rol;
- }
- // We store user roles in cache only for one hour
- cache.set("roles_" + username, userRoles, 3600);
- cache.set("orgs_" + username, user.getOrgsIds(), 3600);
- }
- }
- return userRoles == null ? 0 : userRoles.intValue();
- }
+ private int getUserRoles(String username) {
+ if (username == null) {
+ return 0;
+ }
+ Integer userRoles = cache.get("roles_" + username, Integer.class);
+ if (userRoles == null) {
+ EntityManager em = emProvider.get();
+ User user = em.find(User.class, username);
+ if (user != null) {
+ userRoles = 0;
+ List<Integer> roles = user.getRoles();
+ for (Integer rol : roles) {
+ userRoles += rol;
+ }
+ // We store user roles in cache only for one hour
+ cache.set("roles_" + username, userRoles, 3600);
+ cache.set("orgs_" + username, user.getOrgsIds(), 3600);
+ }
+ }
+ return userRoles == null ? 0 : userRoles.intValue();
+ }
- // @Override
- public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method) throws Failure, WebApplicationException {
- // TODO Auto-generated method stub
- return null;
- }
+ // @Override
+ public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method) throws Failure, WebApplicationException {
+ return null;
+ }
}
--
Gitblit v1.3.2