Search in sources :

Example 1 with ServiceNotFoundException

use of com.nexblocks.authguard.service.exceptions.ServiceNotFoundException in project AuthGuard by AuthGuard.

the class AccountsServiceImpl method grantPermissions.

@Override
public AccountBO grantPermissions(final String accountId, final List<PermissionBO> permissions) {
    final AccountBO account = getById(accountId).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "No account with ID " + accountId + " was found"));
    final List<PermissionBO> verifiedPermissions = permissionsService.validate(permissions, account.getDomain());
    if (verifiedPermissions.size() != permissions.size()) {
        final List<PermissionBO> difference = permissions.stream().filter(permission -> !verifiedPermissions.contains(permission)).collect(Collectors.toList());
        throw new ServiceException(ErrorCode.PERMISSION_DOES_NOT_EXIST, "The following permissions are not valid" + difference);
    }
    final List<PermissionBO> combinedPermissions = Stream.concat(account.getPermissions().stream(), verifiedPermissions.stream()).distinct().collect(Collectors.toList());
    final AccountBO updated = account.withPermissions(combinedPermissions);
    accountsRepository.update(serviceMapper.toDO(updated));
    return updated;
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) RolesService(com.nexblocks.authguard.service.RolesService) java.util(java.util) MessageBus(com.nexblocks.authguard.emb.MessageBus) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) Inject(com.google.inject.Inject) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AccountPreProcessor(com.nexblocks.authguard.service.util.AccountPreProcessor) PermissionsService(com.nexblocks.authguard.service.PermissionsService) Collectors(java.util.stream.Collectors) AccountUpdateMerger(com.nexblocks.authguard.service.util.AccountUpdateMerger) Messages(com.nexblocks.authguard.emb.Messages) AccountConfig(com.nexblocks.authguard.service.config.AccountConfig) Stream(java.util.stream.Stream) AccountsRepository(com.nexblocks.authguard.dal.persistence.AccountsRepository) AccountDO(com.nexblocks.authguard.dal.model.AccountDO) AccountsService(com.nexblocks.authguard.service.AccountsService) ValueComparator(com.nexblocks.authguard.service.util.ValueComparator) Named(com.google.inject.name.Named) ConfigContext(com.nexblocks.authguard.config.ConfigContext) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Example 2 with ServiceNotFoundException

use of com.nexblocks.authguard.service.exceptions.ServiceNotFoundException in project AuthGuard by AuthGuard.

the class AccountsServiceImpl method grantRoles.

@Override
public AccountBO grantRoles(final String accountId, final List<String> roles) {
    final AccountBO account = accountsRepository.getById(accountId).join().map(serviceMapper::toBO).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "No account with ID " + accountId + " was found"));
    verifyRolesOrFail(roles, account.getDomain());
    final List<String> combinedRoles = Stream.concat(account.getRoles().stream(), roles.stream()).distinct().collect(Collectors.toList());
    final AccountBO updated = account.withRoles(combinedRoles);
    return accountsRepository.update(serviceMapper.toDO(updated)).join().map(serviceMapper::toBO).orElseThrow(IllegalStateException::new);
}
Also used : ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Example 3 with ServiceNotFoundException

use of com.nexblocks.authguard.service.exceptions.ServiceNotFoundException in project AuthGuard by AuthGuard.

the class CredentialsServiceImpl method resetPasswordByToken.

@Override
public Optional<CredentialsBO> resetPasswordByToken(final String token, final String plainPassword) {
    final AccountTokenDO accountToken = accountTokensRepository.getByToken(token).join().orElseThrow(() -> new ServiceNotFoundException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "AccountDO token " + token + " does not exist"));
    if (accountToken.getExpiresAt().isBefore(OffsetDateTime.now())) {
        throw new ServiceException(ErrorCode.EXPIRED_TOKEN, "Token " + token + " has expired");
    }
    final String credentialsId = Optional.ofNullable(accountToken.getAdditionalInformation()).map(m -> m.get("credentialsId")).orElseThrow(() -> new ServiceException(ErrorCode.INVALID_TOKEN, "Reset token was not mapped to any credentials"));
    return updatePassword(credentialsId, plainPassword);
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) MessageBus(com.nexblocks.authguard.emb.MessageBus) CryptographicRandom(com.nexblocks.authguard.service.random.CryptographicRandom) Inject(com.google.inject.Inject) CredentialsRepository(com.nexblocks.authguard.dal.persistence.CredentialsRepository) CredentialsDO(com.nexblocks.authguard.dal.model.CredentialsDO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage) CredentialsService(com.nexblocks.authguard.service.CredentialsService) ArrayList(java.util.ArrayList) AccountsService(com.nexblocks.authguard.service.AccountsService) Duration(java.time.Duration) ServiceConflictException(com.nexblocks.authguard.service.exceptions.ServiceConflictException) com.nexblocks.authguard.basic.passwords(com.nexblocks.authguard.basic.passwords) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) Collectors(java.util.stream.Collectors) Messages(com.nexblocks.authguard.emb.Messages) CredentialsAuditRepository(com.nexblocks.authguard.dal.persistence.CredentialsAuditRepository) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Optional(java.util.Optional) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ID(com.nexblocks.authguard.service.util.ID) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Example 4 with ServiceNotFoundException

use of com.nexblocks.authguard.service.exceptions.ServiceNotFoundException in project AuthGuard by AuthGuard.

the class CredentialsServiceImpl method generateResetToken.

@Override
public PasswordResetTokenBO generateResetToken(final String identifier, final boolean returnToken, final String domain) {
    final CredentialsBO credentials = getByUsername(identifier, domain).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.CREDENTIALS_DOES_NOT_EXIST, "Unknown identifier"));
    final AccountBO account = accountsService.getById(credentials.getAccountId()).orElseThrow(() -> new ServiceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "Credentials found for the identifier but no account was associated with it. This could be the " + "result of deleting an account without deleting its credentials"));
    final OffsetDateTime now = OffsetDateTime.now();
    final AccountTokenDO accountToken = AccountTokenDO.builder().id(ID.generate()).token(cryptographicRandom.base64Url(RESET_TOKEN_SIZE)).associatedAccountId(account.getId()).additionalInformation(ImmutableMap.of("credentialsId", credentials.getId())).expiresAt(now.plus(TOKEN_LIFETIME)).build();
    accountTokensRepository.save(accountToken).join();
    messageBus.publish(CREDENTIALS_CHANNEL, Messages.resetTokenGenerated(new ResetTokenMessage(account, accountToken)));
    return PasswordResetTokenBO.builder().token(returnToken ? accountToken.getToken() : null).issuedAt(now.toEpochSecond()).expiresAt(accountToken.getExpiresAt().toEpochSecond()).build();
}
Also used : ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) OffsetDateTime(java.time.OffsetDateTime) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ResetTokenMessage(com.nexblocks.authguard.service.messaging.ResetTokenMessage)

Example 5 with ServiceNotFoundException

use of com.nexblocks.authguard.service.exceptions.ServiceNotFoundException in project AuthGuard by AuthGuard.

the class VerificationServiceImpl method verifyEmail.

@Override
public void verifyEmail(final String verificationToken) {
    final AccountTokenDO accountToken = accountTokensRepository.getByToken(verificationToken).join().orElseThrow(() -> new ServiceNotFoundException(ErrorCode.TOKEN_EXPIRED_OR_DOES_NOT_EXIST, "AccountDO token " + verificationToken + " does not exist"));
    if (accountToken.getExpiresAt().isBefore(OffsetDateTime.now())) {
        throw new ServiceException(ErrorCode.EXPIRED_TOKEN, "Token " + verificationToken + " has expired");
    }
    final String verifiedEmail = Optional.ofNullable(accountToken.getAdditionalInformation()).map(additional -> additional.get(TARGET_EMAIL_PROPERTY)).orElseThrow(() -> new ServiceException(ErrorCode.INVALID_TOKEN, "Invalid account token: no valid additional information"));
    final AccountBO account = accountsService.getById(accountToken.getAssociatedAccountId()).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "AccountDO " + accountToken.getAssociatedAccountId() + " does not exist"));
    final AccountBO updated;
    if (verifiedEmail.equals(account.getEmail().getEmail())) {
        updated = account.withEmail(account.getEmail().withVerified(true));
    } else if (verifiedEmail.equals(account.getBackupEmail().getEmail())) {
        updated = account.withBackupEmail(account.getBackupEmail().withVerified(true));
    } else {
        throw new ServiceException(ErrorCode.INVALID_TOKEN, "Account " + account.getId() + " does not contain the " + "email associated with the verification token");
    }
    accountsService.update(updated);
}
Also used : ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) OffsetDateTime(java.time.OffsetDateTime) AccountsService(com.nexblocks.authguard.service.AccountsService) AccountBO(com.nexblocks.authguard.service.model.AccountBO) Inject(com.google.inject.Inject) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) Optional(java.util.Optional) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) VerificationService(com.nexblocks.authguard.service.VerificationService) AccountBO(com.nexblocks.authguard.service.model.AccountBO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Aggregations

ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)13 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)9 Inject (com.google.inject.Inject)7 AccountsService (com.nexblocks.authguard.service.AccountsService)7 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)7 MessageBus (com.nexblocks.authguard.emb.MessageBus)6 Messages (com.nexblocks.authguard.emb.Messages)6 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)6 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)6 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)6 Collectors (java.util.stream.Collectors)6 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)5 OffsetDateTime (java.time.OffsetDateTime)5 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)4 ServiceConflictException (com.nexblocks.authguard.service.exceptions.ServiceConflictException)4 ResetTokenMessage (com.nexblocks.authguard.service.messaging.ResetTokenMessage)4 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Named (com.google.inject.name.Named)3