Search in sources :

Example 1 with Error

use of com.nexblocks.authguard.api.dto.entities.Error in project AuthGuard by AuthGuard.

the class AccountsRoute method create.

public void create(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateAccountRequestDTO request = accountRequestBodyHandler.getValidated(context);
    if (!canPerform(context, request)) {
        context.status(403).json(new Error("", "An auth client violated its restrictions in the request"));
        return;
    }
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final Optional<AccountDTO> createdAccount = Optional.of(restMapper.toBO(request)).map(accountBO -> accountsService.create(accountBO, requestContext)).map(restMapper::toDTO);
    if (createdAccount.isPresent()) {
        context.status(201).json(createdAccount.get());
    } else {
        context.status(400).json(new Error("400", "Failed to create account"));
    }
}
Also used : Inject(com.google.inject.Inject) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CredentialsService(com.nexblocks.authguard.service.CredentialsService) com.nexblocks.authguard.api.dto.requests(com.nexblocks.authguard.api.dto.requests) AccountsService(com.nexblocks.authguard.service.AccountsService) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) ActorDomainVerifier(com.nexblocks.authguard.rest.access.ActorDomainVerifier) AccountsApi(com.nexblocks.authguard.api.routes.AccountsApi) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) AccountLocksService(com.nexblocks.authguard.service.AccountLocksService) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO) AppDTO(com.nexblocks.authguard.api.dto.entities.AppDTO) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AuthGuardRoles(com.nexblocks.authguard.api.access.AuthGuardRoles) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BodyHandler(com.nexblocks.authguard.rest.util.BodyHandler) Error(com.nexblocks.authguard.api.dto.entities.Error) AccountLockDTO(com.nexblocks.authguard.api.dto.entities.AccountLockDTO) Optional(java.util.Optional) Error(com.nexblocks.authguard.api.dto.entities.Error) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Example 2 with Error

use of com.nexblocks.authguard.api.dto.entities.Error in project AuthGuard by AuthGuard.

the class AccountsRoute method createComplete.

@Override
public void createComplete(final Context context) {
    final String idempotentKey = IdempotencyHeader.getKeyOrFail(context);
    final CreateCompleteAccountRequestDTO request = completeAccountRequestBodyHandler.getValidated(context);
    if (!canPerform(context, request.getAccount())) {
        context.status(403).json(new Error("", "An auth client violated its restrictions in the request"));
        return;
    }
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(idempotentKey).source(context.ip()).build();
    final AccountBO accountBO = restMapper.toBO(request.getAccount());
    final CredentialsBO credentialsBO = restMapper.toBO(request.getCredentials());
    final List<UserIdentifierBO> identifiers = credentialsBO.getIdentifiers().stream().map(identifier -> identifier.withDomain(credentialsBO.getDomain())).collect(Collectors.toList());
    String accountId;
    String credentialsId;
    try {
        accountId = accountsService.create(accountBO, requestContext).getId();
    } catch (final CompletionException e) {
        if (e.getCause() instanceof IdempotencyException) {
            accountId = ((IdempotencyException) e.getCause()).getIdempotentRecord().getEntityId();
        } else {
            throw e;
        }
    }
    try {
        final CredentialsBO withAdditionalInfo = CredentialsBO.builder().from(credentialsBO).identifiers(identifiers).accountId(accountId).build();
        credentialsId = credentialsService.create(withAdditionalInfo, requestContext).getId();
    } catch (final CompletionException e) {
        if (e.getCause() instanceof IdempotencyException) {
            credentialsId = ((IdempotencyException) e.getCause()).getIdempotentRecord().getEntityId();
        } else {
            throw e;
        }
    }
    final CreateCompleteAccountResponseDTO response = CreateCompleteAccountResponseDTO.builder().accountId(accountId).credentialsId(credentialsId).build();
    context.status(201).json(response);
}
Also used : Inject(com.google.inject.Inject) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CredentialsService(com.nexblocks.authguard.service.CredentialsService) com.nexblocks.authguard.api.dto.requests(com.nexblocks.authguard.api.dto.requests) AccountsService(com.nexblocks.authguard.service.AccountsService) RestMapper(com.nexblocks.authguard.rest.mappers.RestMapper) Context(io.javalin.http.Context) ActorDomainVerifier(com.nexblocks.authguard.rest.access.ActorDomainVerifier) AccountsApi(com.nexblocks.authguard.api.routes.AccountsApi) IdempotencyHeader(com.nexblocks.authguard.rest.util.IdempotencyHeader) ApplicationsService(com.nexblocks.authguard.service.ApplicationsService) AccountLocksService(com.nexblocks.authguard.service.AccountLocksService) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO) AppDTO(com.nexblocks.authguard.api.dto.entities.AppDTO) ErrorCode(com.nexblocks.authguard.service.exceptions.codes.ErrorCode) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) com.nexblocks.authguard.service.model(com.nexblocks.authguard.service.model) AuthGuardRoles(com.nexblocks.authguard.api.access.AuthGuardRoles) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BodyHandler(com.nexblocks.authguard.rest.util.BodyHandler) Error(com.nexblocks.authguard.api.dto.entities.Error) AccountLockDTO(com.nexblocks.authguard.api.dto.entities.AccountLockDTO) Optional(java.util.Optional) CompletionException(java.util.concurrent.CompletionException) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 3 with Error

use of com.nexblocks.authguard.api.dto.entities.Error in project AuthGuard by AuthGuard.

the class AccountsRoute method getByEmail.

@Override
public void getByEmail(final Context context) {
    final String domain = context.pathParam("domain");
    final String email = context.pathParam("email");
    final Optional<AccountDTO> account = accountsService.getByEmail(email, domain).map(restMapper::toDTO);
    if (account.isPresent()) {
        context.status(200).json(account.get());
    } else {
        context.status(404).json(new Error(ErrorCode.ACCOUNT_DOES_NOT_EXIST.getCode(), "Account not found"));
    }
}
Also used : Error(com.nexblocks.authguard.api.dto.entities.Error) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO)

Example 4 with Error

use of com.nexblocks.authguard.api.dto.entities.Error in project AuthGuard by AuthGuard.

the class ApiKeysRoute method verify.

@Override
public void verify(final Context context) {
    final AuthRequestDTO authenticationRequest = authRequestBodyHandler.getValidated(context);
    final Optional<AppBO> app = apiKeysService.validateApiKey(authenticationRequest.getToken());
    if (app.isPresent()) {
        context.status(200).json(app.get());
    } else {
        context.status(404).json(new Error(ErrorCode.API_KEY_DOES_NOT_EXIST.getCode(), "API key does not exist"));
    }
}
Also used : AppBO(com.nexblocks.authguard.service.model.AppBO) AuthRequestDTO(com.nexblocks.authguard.api.dto.requests.AuthRequestDTO) Error(com.nexblocks.authguard.api.dto.entities.Error)

Example 5 with Error

use of com.nexblocks.authguard.api.dto.entities.Error in project AuthGuard by AuthGuard.

the class ApiKeysRoute method getById.

@Override
public void getById(final Context context) {
    final String apiKeyId = context.pathParam("id");
    final Optional<ApiKeyDTO> apiKey = apiKeysService.getById(apiKeyId).map(restMapper::toDTO);
    if (apiKey.isPresent()) {
        context.status(200).json(apiKey.get());
    } else {
        context.status(404).json(new Error(ErrorCode.API_KEY_DOES_NOT_EXIST.getCode(), "API key does not exist"));
    }
}
Also used : Error(com.nexblocks.authguard.api.dto.entities.Error) ApiKeyDTO(com.nexblocks.authguard.api.dto.entities.ApiKeyDTO)

Aggregations

Error (com.nexblocks.authguard.api.dto.entities.Error)31 AccountDTO (com.nexblocks.authguard.api.dto.entities.AccountDTO)9 RequestValidationError (com.nexblocks.authguard.api.dto.entities.RequestValidationError)7 Inject (com.google.inject.Inject)4 AuthRequestDTO (com.nexblocks.authguard.api.dto.requests.AuthRequestDTO)4 RestMapper (com.nexblocks.authguard.rest.mappers.RestMapper)4 BodyHandler (com.nexblocks.authguard.rest.util.BodyHandler)4 IdempotencyHeader (com.nexblocks.authguard.rest.util.IdempotencyHeader)4 Context (io.javalin.http.Context)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 AuthGuardRoles (com.nexblocks.authguard.api.access.AuthGuardRoles)3 ApiKeyDTO (com.nexblocks.authguard.api.dto.entities.ApiKeyDTO)3 AppDTO (com.nexblocks.authguard.api.dto.entities.AppDTO)3 ActorDomainVerifier (com.nexblocks.authguard.rest.access.ActorDomainVerifier)3 ApplicationsService (com.nexblocks.authguard.service.ApplicationsService)3 CredentialsService (com.nexblocks.authguard.service.CredentialsService)3 IdempotencyException (com.nexblocks.authguard.service.exceptions.IdempotencyException)3 com.nexblocks.authguard.service.model (com.nexblocks.authguard.service.model)3