Search in sources :

Example 1 with ID

use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.

the class IdempotencyServiceImpl method performOperation.

@Override
public <T extends Entity> CompletableFuture<T> performOperation(final Supplier<T> operation, final String idempotentKey, final String entityType) {
    return findByKeyAndEntityType(idempotentKey, entityType).thenApplyAsync(record -> {
        if (record.isPresent()) {
            throw new IdempotencyException(record.get());
        }
        return operation.get();
    }).thenApply(result -> {
        final IdempotentRecordBO record = IdempotentRecordBO.builder().id(ID.generate()).entityId(result.getId()).entityType(result.getEntityType()).idempotentKey(idempotentKey).build();
        // we don't have to wait for this to finish
        CompletableFuture.runAsync(() -> create(record));
        return result;
    });
}
Also used : ServiceMapper(com.nexblocks.authguard.service.mappers.ServiceMapper) IdempotencyService(com.nexblocks.authguard.service.IdempotencyService) Entity(com.nexblocks.authguard.service.model.Entity) Inject(com.google.inject.Inject) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) Optional(java.util.Optional) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CompletableFuture(java.util.concurrent.CompletableFuture) IdempotentRecordsRepository(com.nexblocks.authguard.dal.persistence.IdempotentRecordsRepository) Supplier(java.util.function.Supplier) ID(com.nexblocks.authguard.service.util.ID) IdempotentRecordBO(com.nexblocks.authguard.service.model.IdempotentRecordBO) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException)

Example 2 with ID

use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.

the class CredentialsServiceImpl method removeIdentifiers.

@Override
public Optional<CredentialsBO> removeIdentifiers(final String id, final List<String> identifiers) {
    final CredentialsBO existing = getByIdUnsafe(id).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "No credentials with ID " + id));
    final List<UserIdentifierBO> updatedIdentifiers = existing.getIdentifiers().stream().map(identifier -> {
        if (identifiers.contains(identifier.getIdentifier())) {
            return identifier.withActive(false);
        }
        return identifier;
    }).collect(Collectors.toList());
    final CredentialsBO updated = CredentialsBO.builder().from(existing).identifiers(updatedIdentifiers).build();
    return doUpdate(existing, updated, false);
}
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) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Example 3 with ID

use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.

the class CredentialsServiceImpl method replaceIdentifier.

@Override
public Optional<CredentialsBO> replaceIdentifier(final String id, final String oldIdentifier, final UserIdentifierBO newIdentifier) {
    final CredentialsBO credentials = getByIdUnsafe(id).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "No credentials with ID " + id));
    final boolean hasIdentifier = credentials.getIdentifiers().stream().anyMatch(identifier -> identifier.getIdentifier().equals(oldIdentifier));
    if (!hasIdentifier) {
        throw new ServiceException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "Credentials " + id + " has no identifier " + oldIdentifier);
    }
    final Set<UserIdentifierBO> newIdentifiers = credentials.getIdentifiers().stream().map(identifier -> {
        if (identifier.getIdentifier().equals(oldIdentifier)) {
            return UserIdentifierBO.builder().identifier(newIdentifier.getIdentifier()).active(newIdentifier.isActive()).type(identifier.getType()).domain(identifier.getDomain()).build();
        }
        return identifier;
    }).collect(Collectors.toSet());
    final CredentialsBO update = credentials.withIdentifiers(newIdentifiers);
    return doUpdate(credentials, update, false);
}
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) ServiceNotFoundException(com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)

Aggregations

Inject (com.google.inject.Inject)3 IdempotencyService (com.nexblocks.authguard.service.IdempotencyService)3 ServiceMapper (com.nexblocks.authguard.service.mappers.ServiceMapper)3 ID (com.nexblocks.authguard.service.util.ID)3 Optional (java.util.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 com.nexblocks.authguard.basic.passwords (com.nexblocks.authguard.basic.passwords)2 AccountTokensRepository (com.nexblocks.authguard.dal.cache.AccountTokensRepository)2 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)2 CredentialsDO (com.nexblocks.authguard.dal.model.CredentialsDO)2 CredentialsAuditRepository (com.nexblocks.authguard.dal.persistence.CredentialsAuditRepository)2 CredentialsRepository (com.nexblocks.authguard.dal.persistence.CredentialsRepository)2 MessageBus (com.nexblocks.authguard.emb.MessageBus)2 Messages (com.nexblocks.authguard.emb.Messages)2 AccountsService (com.nexblocks.authguard.service.AccountsService)2 CredentialsService (com.nexblocks.authguard.service.CredentialsService)2 ServiceConflictException (com.nexblocks.authguard.service.exceptions.ServiceConflictException)2 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)2 ServiceNotFoundException (com.nexblocks.authguard.service.exceptions.ServiceNotFoundException)2 ErrorCode (com.nexblocks.authguard.service.exceptions.codes.ErrorCode)2