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