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