Search in sources :

Example 26 with ApplicationUser

use of org.finra.herd.model.dto.ApplicationUser in project herd by FINRAOS.

the class NamespaceSecurityAdviceTest method checkPermissionAssertNoExceptionWhenNull.

/**
 * Assert no access denied exception when parameter value is null.
 */
@Test
public void checkPermissionAssertNoExceptionWhenNull() throws Exception {
    // Mock a join point of the method call
    // mockMethod(null);
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod", String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { null });
    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser), null));
    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodSignature(org.aspectj.lang.reflect.MethodSignature) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) Method(java.lang.reflect.Method) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) JoinPoint(org.aspectj.lang.JoinPoint) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Example 27 with ApplicationUser

use of org.finra.herd.model.dto.ApplicationUser in project herd by FINRAOS.

the class HerdUserDetailsService method loadUserDetails.

@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
    ApplicationUser user = (ApplicationUser) token.getPrincipal();
    Set<GrantedAuthority> authorities = new HashSet<>();
    // Add all functional points per given collection of user roles.
    authorities.addAll(securityHelper.mapRolesToFunctions(user.getRoles()));
    // Add all function points that are not mapped to any roles in the system.
    authorities.addAll(securityHelper.getUnrestrictedFunctions());
    SecurityUserWrapper result = new SecurityUserWrapper(user.getUserId(), "N/A", true, true, true, true, authorities, user);
    LOGGER.debug("Loaded User: " + result);
    return result;
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) HashSet(java.util.HashSet)

Example 28 with ApplicationUser

use of org.finra.herd.model.dto.ApplicationUser in project herd by FINRAOS.

the class HttpHeaderAuthenticationFilter method getExistingUser.

/**
 * Gets the existing user.
 *
 * @return the existing user or null if no existing user is present.
 */
protected ApplicationUser getExistingUser() {
    ApplicationUser applicationUser = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        SecurityUserWrapper securityUserWrapper = (SecurityUserWrapper) authentication.getPrincipal();
        if (securityUserWrapper != null) {
            applicationUser = securityUserWrapper.getApplicationUser();
            LOGGER.trace("Existing Application User: " + applicationUser);
            return applicationUser;
        }
    }
    return applicationUser;
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) Authentication(org.springframework.security.core.Authentication) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper)

Example 29 with ApplicationUser

use of org.finra.herd.model.dto.ApplicationUser in project herd by FINRAOS.

the class TrustedApplicationUserBuilder method buildUser.

/**
 * Builds the application user.
 *
 * @param request the HTTP servlet request.
 * @param includeRoles If true, the user's roles will be included. Otherwise, not.
 *
 * @return the application user.
 */
protected ApplicationUser buildUser(HttpServletRequest request, boolean includeRoles) {
    ApplicationUser applicationUser = new ApplicationUser(this.getClass());
    applicationUser.setUserId(TRUSTED_USER_ID);
    applicationUser.setFirstName(TRUSTED_USER_FIRST_NAME);
    applicationUser.setLastName(TRUSTED_USER_LAST_NAME);
    applicationUser.setEmail(TRUSTED_USER_EMAIL);
    applicationUser.setSessionId(request.getSession().getId());
    applicationUser.setNamespaceAuthorizations(userNamespaceAuthorizationHelper.getAllNamespaceAuthorizations());
    if (includeRoles) {
        Set<String> roles = new HashSet<>();
        roles.add(TRUSTED_USER_ROLE);
        applicationUser.setRoles(roles);
    }
    return applicationUser;
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) HashSet(java.util.HashSet)

Example 30 with ApplicationUser

use of org.finra.herd.model.dto.ApplicationUser in project herd by FINRAOS.

the class AbstractAppTest method validateTrustedApplicationUser.

protected void validateTrustedApplicationUser() throws Exception {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    assertNotNull(authentication);
    SecurityUserWrapper user = (SecurityUserWrapper) authentication.getPrincipal();
    ApplicationUser applicationUser = user.getApplicationUser();
    assertEquals(TrustedApplicationUserBuilder.TRUSTED_USER_ID, applicationUser.getUserId());
    assertEquals(TrustedApplicationUserBuilder.TRUSTED_USER_FIRST_NAME, applicationUser.getFirstName());
    assertEquals(TrustedApplicationUserBuilder.TRUSTED_USER_LAST_NAME, applicationUser.getLastName());
    assertEquals(TrustedApplicationUserBuilder.TRUSTED_USER_EMAIL, applicationUser.getEmail());
    Set<String> roles = applicationUser.getRoles();
    assertTrue(roles.contains(TrustedApplicationUserBuilder.TRUSTED_USER_ROLE));
    assertNotNull(applicationUser.getSessionId());
    assertEquals(TrustedApplicationUserBuilder.class, applicationUser.getGeneratedByClass());
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) Authentication(org.springframework.security.core.Authentication) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper)

Aggregations

ApplicationUser (org.finra.herd.model.dto.ApplicationUser)50 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)41 Test (org.junit.Test)36 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)31 AccessDeniedException (org.springframework.security.access.AccessDeniedException)29 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)26 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)22 Method (java.lang.reflect.Method)21 JoinPoint (org.aspectj.lang.JoinPoint)21 MethodSignature (org.aspectj.lang.reflect.MethodSignature)21 ArrayList (java.util.ArrayList)6 Job (org.finra.herd.model.api.xml.Job)6 Authentication (org.springframework.security.core.Authentication)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)4 Collection (java.util.Collection)3 List (java.util.List)3 UserAuthorizations (org.finra.herd.model.api.xml.UserAuthorizations)3