package org.tiatus.server.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tiatus.auth.TiatusSecurityContext;
import org.tiatus.auth.UserPrincipal;
import org.tiatus.entity.Role;
import org.tiatus.entity.User;
import org.tiatus.entity.UserRole;

import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by johnreynolds on 26/08/2016.
 */
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationFilter.class);

    @Context
    HttpServletRequest servletRequest;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        LOG.debug("in filter");

        // are we already logged in the session
        HttpSession session = servletRequest.getSession();
        if (session != null && session.getAttribute("principal") == null) {
            LOG.debug("Creating new user principal");
            // do we have the auth details - in post
            String scheme = requestContext.getUriInfo().getRequestUri().getScheme();
            UserPrincipal user = getUser(null, null);
            requestContext.getRequest();
            requestContext.setSecurityContext(new TiatusSecurityContext(user, scheme));
        }
    }

    private UserPrincipal getUser(String userName, String password) {
        UserPrincipal principal = new UserPrincipal();
        User user = new User();
        user.setFirstName("John");
        user.setLastName("Reynolds");
        user.setUserName("admin");
        Role role = new Role();
        role.setRole("ADMIN");
        UserRole userRole = new UserRole();
        userRole.setRole(role);
        Set<UserRole> roles = new HashSet<>();
        roles.add(userRole);
        user.setRoles(roles);
        principal.setUser(user);
        return principal;
    }
}

@@ -1248,12 +1248,144 @@
 null
- && 
+)%7B%0A            String scheme = requestContext.getUriInfo().getRequestUri().getScheme();%0A            UserPrincipal user;%0A            if (
 sess
@@ -1421,24 +1421,28 @@
  == null) %7B%0A
+    
             
@@ -1495,16 +1495,20 @@
         
+    
 // do we
@@ -1556,81 +1556,64 @@
     
-String scheme = requestContext.getUriInfo().getRequestUri().getScheme();%0A
+    user = getUser(null, null);%0A            %7D else %7B%0A   
     
@@ -1612,32 +1612,41 @@
 %0A               
+ user = (
 UserPrincipal us
@@ -1646,77 +1646,57 @@
 ipal
- user = getUser(null, null);%0A            requestContext.getRequest();
+)session.getAttribute(%22principal%22);%0A            %7D
 %0A   

Save a potential db call be pulling principal form the session