Search in sources :

Example 36 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.

the class UserModelTest method testUserModelNullRoles.

@Test
public void testUserModelNullRoles() {
    String expectedUserName = "expectedUser";
    String expectedPassword = "expectedPassword";
    String expectedEmail = "expectedEmail";
    Set<String> roleNames = null;
    Set<UserRoleModel> expectedRoles = null;
    UserModel userModel = UserModel.newUser(expectedUserName, expectedPassword, expectedEmail, AuthenticationType.DATABASE, expectedRoles, true);
    assertEquals(expectedUserName, userModel.getName());
    assertEquals(expectedPassword, userModel.getPassword());
    assertEquals(expectedEmail, userModel.getEmailAddress());
    assertNull(userModel.getRoles());
    assertFalse(userModel.hasRole(DefaultUserRole.ALERT_ADMIN.name()));
    assertFalse(userModel.hasRole("UNKNOWN_ROLE"));
    assertFalse(userModel.isExpired());
    assertFalse(userModel.isLocked());
    assertFalse(userModel.isPasswordExpired());
    assertTrue(userModel.isEnabled());
    assertFalse(userModel.isExternal());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 37 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.

the class UserModelTest method testUserModelEmptyRoles.

@Test
public void testUserModelEmptyRoles() {
    String expectedUserName = "expectedUser";
    String expectedPassword = "expectedPassword";
    String expectedEmail = "expectedEmail";
    Set<UserRoleModel> expectedRoles = new LinkedHashSet<>();
    UserModel userModel = UserModel.newUser(expectedUserName, expectedPassword, expectedEmail, AuthenticationType.DATABASE, expectedRoles, true);
    assertEquals(expectedUserName, userModel.getName());
    assertEquals(expectedPassword, userModel.getPassword());
    assertEquals(expectedEmail, userModel.getEmailAddress());
    assertTrue(userModel.getRoles().isEmpty());
    assertFalse(userModel.hasRole(DefaultUserRole.ALERT_ADMIN.name()));
    assertFalse(userModel.hasRole("UNKNOWN_ROLE"));
    assertFalse(userModel.isExpired());
    assertFalse(userModel.isLocked());
    assertFalse(userModel.isPasswordExpired());
    assertTrue(userModel.isEnabled());
    assertFalse(userModel.isExternal());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 38 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel 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 39 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.

the class ConfigurationOverridesStartupComponentTest method testInitializeResetPasswordDifferentUsername.

@Test
public void testInitializeResetPasswordDifferentUsername() throws AlertException {
    Environment environment = Mockito.mock(Environment.class);
    Mockito.when(environment.getProperty(ConfigurationOverridesStartupComponent.ENV_VAR_ADMIN_USER_PASSWORD_RESET)).thenReturn("true");
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
    String newUsername = "UpdatedAdmin";
    // Update the sysadmin username and password
    Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    UserModel sysadmin = sysadminOptional.get();
    assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    UserModel updatedSysadmin = changeUserNameAndPassword(sysadmin, newUsername, UPDATED_PASSWORD);
    userAccessor.updateUser(updatedSysadmin, false);
    // Run the initialize method
    configurationOverridesStartupComponent.initialize();
    // Verify the sysadmin password is the default password
    sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    sysadmin = sysadminOptional.get();
    assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    HttpSession session = Mockito.mock(HttpSession.class);
    Mockito.when(servletRequest.getSession()).thenReturn(session);
    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
    // Try to login with the updated password
    LoginConfig updatedLoginConfig = new LoginConfig(newUsername, UPDATED_PASSWORD);
    ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
    assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
    // Try to login with the default password
    LoginConfig defaultLoginConfig = new LoginConfig(newUsername, DEFAULT_PASSWORD);
    actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
    assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) HttpSession(javax.servlet.http.HttpSession) LoginConfig(com.synopsys.integration.alert.component.authentication.web.LoginConfig) Environment(org.springframework.core.env.Environment) HttpServletResponse(javax.servlet.http.HttpServletResponse) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 40 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel 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)

Aggregations

UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)42 Test (org.junit.jupiter.api.Test)32 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)17 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)12 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)8 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)7 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)6 UserManagementDescriptorKey (com.synopsys.integration.alert.component.users.UserManagementDescriptorKey)6 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)5 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)4 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)4 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 DefaultUserRole (com.synopsys.integration.alert.common.enumeration.DefaultUserRole)3 UserAccessor (com.synopsys.integration.alert.common.persistence.accessor.UserAccessor)3 LoginConfig (com.synopsys.integration.alert.component.authentication.web.LoginConfig)3