Search in sources :

Example 1 with ServiceMapper

use of com.nexblocks.authguard.service.mappers.ServiceMapper in project AuthGuard by AuthGuard.

the class OtpVerifier method verifyAccountToken.

@Override
public Either<Exception, String> verifyAccountToken(final String token) {
    // TODO: no need to have a special format for the token, just receive the two parts in the request
    final String[] parts = token.split(":");
    if (parts.length != 2) {
        return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_AUTHORIZATION_FORMAT, "Invalid OTP token format"));
    }
    final String passwordId = parts[0];
    final String otp = parts[1];
    final Optional<OneTimePasswordBO> generatedOpt = otpRepository.getById(passwordId).thenApply(optional -> optional.map(serviceMapper::toBO)).join();
    if (generatedOpt.isPresent()) {
        final OneTimePasswordBO generated = generatedOpt.get();
        if (generated.getExpiresAt().isBefore(OffsetDateTime.now())) {
            return Either.left(new ServiceAuthorizationException(ErrorCode.EXPIRED_TOKEN, "OTP " + passwordId + " has expired", EntityType.ACCOUNT, generated.getAccountId()));
        }
        if (generated.getPassword().equals(otp)) {
            return Either.right(generated.getAccountId());
        } else {
            return Either.left(new ServiceAuthorizationException(ErrorCode.PASSWORDS_DO_NOT_MATCH, "OTP " + passwordId + " values did not match", EntityType.ACCOUNT, generated.getAccountId()));
        }
    } else {
        return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_TOKEN, "Invalid OTP ID"));
    }
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) OffsetDateTime(java.time.OffsetDateTime) AuthVerifier(com.nexblocks.authguard.service.auth.AuthVerifier) EntityType(com.nexblocks.authguard.service.model.EntityType) Either(io.vavr.control.Either) Inject(com.google.inject.Inject) Optional(java.util.Optional) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) OtpRepository(com.nexblocks.authguard.dal.cache.OtpRepository) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO)

Example 2 with ServiceMapper

use of com.nexblocks.authguard.service.mappers.ServiceMapper in project AuthGuard by AuthGuard.

the class AccountsServiceImpl method revokePermissions.

@Override
public AccountBO revokePermissions(final String accountId, final List<PermissionBO> permissions) {
    final Set<String> permissionsFullNames = permissions.stream().map(Permission::getFullName).collect(Collectors.toSet());
    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"));
    final List<PermissionBO> filteredPermissions = account.getPermissions().stream().filter(permission -> !permissionsFullNames.contains(permission.getFullName())).collect(Collectors.toList());
    final AccountBO updated = account.withPermissions(filteredPermissions);
    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) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Example 3 with ServiceMapper

use of com.nexblocks.authguard.service.mappers.ServiceMapper in project AuthGuard by AuthGuard.

the class AccountsServiceImpl method revokeRoles.

@Override
public AccountBO revokeRoles(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"));
    final List<String> filteredRoles = account.getRoles().stream().filter(role -> !roles.contains(role)).collect(Collectors.toList());
    final AccountBO updated = account.withRoles(filteredRoles);
    return accountsRepository.update(serviceMapper.toDO(updated)).join().map(serviceMapper::toBO).orElseThrow(IllegalStateException::new);
}
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) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Aggregations

Inject (com.google.inject.Inject)3 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)3 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)3 Named (com.google.inject.name.Named)2 ConfigContext (com.nexblocks.authguard.config.ConfigContext)2 AccountDO (com.nexblocks.authguard.dal.model.AccountDO)2 AccountsRepository (com.nexblocks.authguard.dal.persistence.AccountsRepository)2 MessageBus (com.nexblocks.authguard.emb.MessageBus)2 Messages (com.nexblocks.authguard.emb.Messages)2 AccountsService (com.nexblocks.authguard.service.AccountsService)2 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)2 PermissionsService (com.nexblocks.authguard.service.PermissionsService)2 RolesService (com.nexblocks.authguard.service.RolesService)2 AccountConfig (com.nexblocks.authguard.service.config.AccountConfig)2 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)2 ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)2 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)2 AccountPreProcessor (com.nexblocks.authguard.service.util.AccountPreProcessor)2 AccountUpdateMerger (com.nexblocks.authguard.service.util.AccountUpdateMerger)2 ValueComparator (com.nexblocks.authguard.service.util.ValueComparator)2