public <E extends Entity> void loadList(List<EntityData<E>> entities, LoadingOption... options) { // TODO improve perf by doing only one query for (EntityData<E> entityData : entities) { load(entityData, options); } }
private boolean isTenantEmptyOfUser() { return !accountsService.hasUsers(); }
@Override public boolean checkLength(String password) { return password.length() >= accountsSettings.getPasswordRequirements().getMinimalLength(); }
private void create(User user, Role initialRole, Map<String, Object> additionalContext) throws InvalidEntityException, EntityAlreadyExistsException, PasswordDoesNotMeetRequirementsException { AccountsSettings settings = getSettings(); Boolean validationIsRequired = settings.getUserValidation().getValue(); if (user.getPassword().length() < settings.getPasswordRequirements().getMinimalLength()) { throw new PasswordDoesNotMeetRequirementsException("Provided password does not meet requirements"); } if (validationIsRequired) { if (context.getTenant() != null) { throw new RuntimeException("Validation is not supported for local users"); } user.setActive(false); user.setValidationKey(generateSecret()); } else { user.setActive(true); } user.setPassword(this.passwordManager.hashPassword(user.getPassword())); final User createdUser = this.userStore.get().create(user, initialRole); if (validationIsRequired) { sendValidationMail(createdUser, settings, additionalContext); } }
@Override public void changePassword(User user, String currentPassword, String newPassword) throws WrongPasswordException, PasswordDoesNotMeetRequirementsException { Preconditions.checkNotNull(user); Preconditions.checkNotNull(user.getId()); User stored = this.userStore.get().findById(user.getId()); if (!this.passwordManager.verifyPassword(currentPassword, stored.getPassword())) { throw new WrongPasswordException("Refusing to change password : given current password is incorrect"); } if (!passwordStrengthChecker.checkLength(newPassword)) { throw new PasswordDoesNotMeetRequirementsException( "Provided password does not meet requirements : length too short"); } if (!passwordStrengthChecker.checkEntropy(newPassword)) { throw new PasswordDoesNotMeetRequirementsException( "Provided password does not meet requirements : not enough bits of entropy"); } this.userStore.get().updatePassword(user, passwordManager.hashPassword(newPassword)); }
@Override public User validateAccount(String validationKey) throws NoSuchValidationKeyException, IncompatibleConnectedUserException, UserAlreadyValidatedException { User user = this.userStore.get().findByValidationKey(validationKey); if (user == null) { throw new NoSuchValidationKeyException("Validation key does not exist"); } if (context.getUser() != null && !context.getUser().getId().equals(user.getId())) { throw new IncompatibleConnectedUserException("Refusing to validate user not matching connected user"); } if (user.isActive()) { throw new UserAlreadyValidatedException("User already validated"); } user.setActive(true); try { this.userStore.get().update(user); } catch (InvalidEntityException | EntityDoesNotExistException e) { this.logger.error("Failed to validate user", e); throw new RuntimeException(e); } return user; }
@Override public void resetPassword(String resetKey, String password) throws NoSuchPasswordResetKeyException, PasswordDoesNotMeetRequirementsException { Preconditions.checkNotNull(resetKey); Preconditions.checkNotNull(password); User user = this.userStore.get().findUserByPasswordResetRequest(resetKey); if (user == null) { throw new NoSuchPasswordResetKeyException(); } AccountsSettings settings = getSettings(); if (password.length() < settings.getPasswordRequirements().getMinimalLength()) { throw new PasswordDoesNotMeetRequirementsException("Provided password does not meet requirements"); } this.userStore.get().updatePassword(user, passwordManager.hashPassword(password)); this.userStore.get().deletePasswordResetRequest(resetKey); }
@Override public void createPasswordResetRequest(String emailOrUsername) throws UserNotFoundException { Preconditions.checkNotNull(emailOrUsername); User user = this.userStore.get().findUserByEmailOrUserName(emailOrUsername); if (user == null) { throw new UserNotFoundException("No user found with this email or usernmae"); } AccountsSettings settings = getSettings(); final String secret = generateSecret(); this.userStore.get().createPasswordResetRequest(user, secret); sendPasswordResetMail(user, secret, settings); }
private void sendPasswordResetMail(User user, final String secret, AccountsSettings settings) { MailTemplate mailTemplate = new MailTemplate().template("password-reset").to(user.getEmail()) .from(generalSettings.getNotificationsEmail()); try { Map<String, Object> context = Maps.newHashMap(); for (UserDataSupplier supplier : userDataSuppliers.values()) { supplier.supply(user, context); } String passwordResetUriLink; if (!Strings.isNullOrEmpty(settings.getUserPasswordResetUriTemplate().getValue())) { passwordResetUriLink = settings.getUserPasswordResetUriTemplate().getValue(); } else { passwordResetUriLink = urlHelper.getContextWebURL("/login/reset-password/${resetKey}").toString(); } SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(); groovy.text.Template uriTemplate = templateEngine.createTemplate(passwordResetUriLink); context.put("resetLink", uriTemplate.make(new HashMap() { { put("resetKey", secret); } }).toString()); context.put("siteName", siteSettings.getName()); mailTemplateService.sendTemplateMail(mailTemplate, context); } catch (MailException | ClassNotFoundException | IOException e) { logger.error("Failed to send validation email", e); } }
tenant = this.accountsService.findTenant(this.configuration.getDefaultTenantSlug()); if (tenant == null) { tenant = this.accountsService.createDefaultTenant(); String tmp = StringUtils.substringAfter(path, "/tenant/"); String slug = StringUtils.substringBefore(tmp, "/"); tenant = this.accountsService.findTenant(slug); tenant = this.accountsService.findTenantByDefaultHost(host); if (tenant == null) { tenant = this.accountsService.findTenant(this.extractSlugFromHost(host)); if (tenant == null) { return null;
@Override public boolean checkEntropy(String password) { if (!accountsSettings.getPasswordRequirements().getMinimalEntropyBits().isPresent()) { return true; } return meter.getStrength(password).getEntropy() >= accountsSettings.getPasswordRequirements().getMinimalEntropyBits().get(); } }
private void sendValidationMail(final User createdUser, AccountsSettings settings, Map<String, Object> additionalContext) { MailTemplate mailTemplate = new MailTemplate().template("account-validation").to(createdUser.getEmail()) .from(generalSettings.getNotificationsEmail()); try { Map<String, Object> context = Maps.newHashMap(); context.putAll(additionalContext); String validationUriTemplate; if (!Strings.isNullOrEmpty(settings.getUserValidationUriTemplate().getValue())) { validationUriTemplate = settings.getUserValidationUriTemplate().getValue(); } else { validationUriTemplate = urlHelper.getContextWebURL("/account/validation/${validationKey}").toString(); } SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(); groovy.text.Template uriTemplate = templateEngine.createTemplate(validationUriTemplate); context.put("validationLink", uriTemplate.make(new HashMap() { { put("validationKey", createdUser.getValidationKey()); } }).toString()); context.put("siteName", siteSettings.getName()); mailTemplateService.sendTemplateMail(mailTemplate, context); } catch (MailException | ClassNotFoundException | IOException e) { logger.error("Failed to send validation email", e); } }
@Override public NewCookie[] getCookies(String username, String password, boolean remember) throws EncryptionException { AccountsSettings accountsSettings = configurationService.getSettings(AccountsSettings.class); int ageWhenRemember = 60 * (context.getRequest().isApiRequest() ? accountsSettings.getApiSessionDuration().getValue() : accountsSettings.getWebSessionDuration().getValue()); NewCookie newUserCookie = new NewCookie("username", crypter.encrypt(username), "/", null, null, remember ? ageWhenRemember : -1, false); NewCookie newPassCookie = new NewCookie("password", crypter.encrypt(password), "/", null, null, remember ? ageWhenRemember : -1, false); return new NewCookie[]{ newUserCookie, newPassCookie }; } }
@Override public void initialize(Bootstrap<C> bootstrap) { this.addModule(new AccountsModule()); bootstrap.getObjectMapper().registerModule(new NIOModule()); bootstrap.getObjectMapper().registerModule(new MayocatJodaModule()); bootstrap.getObjectMapper().registerModule(new MayocatLocaleBCP47LanguageTagModule()); bootstrap.getObjectMapper().registerModule(new TimeZoneModule()); bootstrap.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // TODO: remove when upgrading DW to 0.8 bootstrap.getObjectMapper().registerModule(new PermissiveFuzzyEnumModule()); }