Search in sources :

Example 1 with UserPrincipal

use of com.synopsys.integration.alert.common.security.UserPrincipal in project hub-alert by blackducksoftware.

the class UserDetailsServiceTest method testEmptyRoleArray.

@Test
public void testEmptyRoleArray() {
    SAMLCredential credential = Mockito.mock(SAMLCredential.class);
    String[] roles = new String[0];
    NameID nameId = Mockito.mock(NameID.class);
    Mockito.when(nameId.getValue()).thenReturn(USER_NAME);
    Mockito.when(credential.getNameID()).thenReturn(nameId);
    Mockito.when(credential.getAttributeAsString("Name")).thenReturn(USER_NAME);
    Mockito.when(credential.getAttributeAsString("Email")).thenReturn(EMAIL);
    Mockito.when(credential.getAttributeAsStringArray("AlertRoles")).thenReturn(roles);
    UserDetailsService userDetailsService = new UserDetailsService(authoritiesPopulator);
    Object result = userDetailsService.loadUserBySAML(credential);
    assertNotNull(result);
    assertTrue(UserPrincipal.class.isInstance(result));
    UserPrincipal principal = (UserPrincipal) result;
    assertEquals(USER_NAME, principal.getUsername());
    assertTrue(StringUtils.isBlank(principal.getPassword()));
    assertEquals(VALID_DB_ROLES.length, principal.getAuthorities().size());
    List<String> expectedRoles = Arrays.asList(VALID_DB_ROLES);
    List<String> actualRoles = extractRoleNamesFromPrincipal(principal);
    assertTrue(expectedRoles.containsAll(actualRoles));
}
Also used : SAMLCredential(org.springframework.security.saml.SAMLCredential) NameID(org.opensaml.saml2.core.NameID) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal) Test(org.junit.jupiter.api.Test)

Example 2 with UserPrincipal

use of com.synopsys.integration.alert.common.security.UserPrincipal in project hub-alert by blackducksoftware.

the class AuthenticationTestUtils method createAuthentication.

public Authentication createAuthentication(Long id, String username, Set<UserRoleModel> roles) {
    UserModel userModel = UserModel.existingUser(id, username, "", "", AuthenticationType.DATABASE, roles, true);
    UserPrincipal userPrincipal = new UserPrincipal(userModel);
    return new UsernamePasswordAuthenticationToken(userPrincipal, null, userPrincipal.getAuthorities());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal)

Example 3 with UserPrincipal

use of com.synopsys.integration.alert.common.security.UserPrincipal in project hub-alert by blackducksoftware.

the class UserDetailsService method loadUserBySAML.

@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
    String userName = credential.getNameID().getValue();
    String emailAddress = StringUtils.contains(userName, "@") ? userName : null;
    String[] alertRoles = credential.getAttributeAsStringArray(authoritiesPopulator.getSAMLRoleAttributeName("AlertRoles"));
    Set<String> existingRoles = Set.of();
    if (alertRoles != null) {
        existingRoles = Arrays.stream(alertRoles).collect(Collectors.toSet());
    }
    Set<String> roleNames = authoritiesPopulator.addAdditionalRoleNames(userName, existingRoles, false);
    Set<UserRoleModel> roles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
    UserModel userModel = UserModel.newUser(userName, "", emailAddress, AuthenticationType.SAML, roles, true);
    return new UserPrincipal(userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal)

Example 4 with UserPrincipal

use of com.synopsys.integration.alert.common.security.UserPrincipal in project hub-alert by blackducksoftware.

the class UserDetailsServiceTest method testValidCredential.

@Test
public void testValidCredential() {
    SAMLCredential credential = Mockito.mock(SAMLCredential.class);
    NameID nameId = Mockito.mock(NameID.class);
    Mockito.when(nameId.getValue()).thenReturn(USER_NAME);
    Mockito.when(credential.getNameID()).thenReturn(nameId);
    Mockito.when(credential.getAttributeAsString("Name")).thenReturn(USER_NAME);
    Mockito.when(credential.getAttributeAsString("Email")).thenReturn(EMAIL);
    Mockito.when(credential.getAttributeAsStringArray("AlertRoles")).thenReturn(VALID_ROLES);
    UserDetailsService userDetailsService = new UserDetailsService(authoritiesPopulator);
    Object result = userDetailsService.loadUserBySAML(credential);
    assertNotNull(result);
    assertTrue(UserPrincipal.class.isInstance(result));
    UserPrincipal principal = (UserPrincipal) result;
    assertEquals(USER_NAME, principal.getUsername());
    assertTrue(StringUtils.isBlank(principal.getPassword()));
    assertEquals(VALID_ROLES.length + VALID_DB_ROLES.length, principal.getAuthorities().size());
    List<String> expectedRoles = new ArrayList<>();
    expectedRoles.addAll(Arrays.asList(VALID_ROLES));
    expectedRoles.addAll(Arrays.asList(VALID_DB_ROLES));
    List<String> actualRoles = extractRoleNamesFromPrincipal(principal);
    assertTrue(expectedRoles.containsAll(actualRoles));
}
Also used : SAMLCredential(org.springframework.security.saml.SAMLCredential) NameID(org.opensaml.saml2.core.NameID) ArrayList(java.util.ArrayList) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal) Test(org.junit.jupiter.api.Test)

Example 5 with UserPrincipal

use of com.synopsys.integration.alert.common.security.UserPrincipal in project hub-alert by blackducksoftware.

the class UserDetailsServiceTest method testNullRoleArray.

@Test
public void testNullRoleArray() {
    SAMLCredential credential = Mockito.mock(SAMLCredential.class);
    NameID nameId = Mockito.mock(NameID.class);
    Mockito.when(nameId.getValue()).thenReturn(USER_NAME);
    Mockito.when(credential.getNameID()).thenReturn(nameId);
    Mockito.when(credential.getAttributeAsString("Name")).thenReturn(USER_NAME);
    Mockito.when(credential.getAttributeAsString("Email")).thenReturn(EMAIL);
    Mockito.when(credential.getAttributeAsStringArray("AlertRoles")).thenReturn(null);
    UserDetailsService userDetailsService = new UserDetailsService(authoritiesPopulator);
    Object result = userDetailsService.loadUserBySAML(credential);
    assertNotNull(result);
    assertTrue(UserPrincipal.class.isInstance(result));
    UserPrincipal principal = (UserPrincipal) result;
    assertEquals(USER_NAME, principal.getUsername());
    assertTrue(StringUtils.isBlank(principal.getPassword()));
    assertEquals(VALID_DB_ROLES.length, principal.getAuthorities().size());
    List<String> expectedRoles = Arrays.asList(VALID_DB_ROLES);
    List<String> actualRoles = extractRoleNamesFromPrincipal(principal);
    assertTrue(expectedRoles.containsAll(actualRoles));
}
Also used : SAMLCredential(org.springframework.security.saml.SAMLCredential) NameID(org.opensaml.saml2.core.NameID) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal) Test(org.junit.jupiter.api.Test)

Aggregations

UserPrincipal (com.synopsys.integration.alert.common.security.UserPrincipal)5 Test (org.junit.jupiter.api.Test)3 NameID (org.opensaml.saml2.core.NameID)3 SAMLCredential (org.springframework.security.saml.SAMLCredential)3 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)2 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)1 ArrayList (java.util.ArrayList)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1