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));
}
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());
}
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);
}
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));
}
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));
}
Aggregations