use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class CredentialsApiAuthProvider method authenticate.
@Override
public final void authenticate(final DeviceCredentials deviceCredentials, final Handler<AsyncResult<Device>> resultHandler) {
Objects.requireNonNull(deviceCredentials);
Objects.requireNonNull(resultHandler);
Future<Device> validationResult = Future.future();
validationResult.setHandler(resultHandler);
getCredentialsForDevice(deviceCredentials).recover(t -> {
final ServiceInvocationException e = (ServiceInvocationException) t;
if (e.getErrorCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials"));
} else {
return Future.failedFuture(t);
}
}).map(credentialsOnRecord -> {
if (deviceCredentials.validate(credentialsOnRecord)) {
return new Device(deviceCredentials.getTenantId(), credentialsOnRecord.getDeviceId());
} else {
throw new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "invalid credentials");
}
}).setHandler(resultHandler);
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseCredentialsService method processAddRequest.
private Future<EventBusMessage> processAddRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final CredentialsObject payload = Optional.ofNullable(request.getJsonPayload()).map(json -> json.mapTo(CredentialsObject.class)).orElse(null);
if (tenantId == null || payload == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else if (payload.isValid()) {
final Future<CredentialsResult<JsonObject>> result = Future.future();
add(tenantId, JsonObject.mapFrom(payload), result.completer());
return result.map(res -> {
return request.getResponse(res.getStatus()).setDeviceId(payload.getDeviceId()).setCacheDirective(res.getCacheDirective());
});
} else {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseCredentialsService method processGetRequest.
private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final JsonObject payload = request.getJsonPayload();
if (tenantId == null || payload == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
final String type = getTypesafeValueForField(payload, CredentialsConstants.FIELD_TYPE);
final String authId = getTypesafeValueForField(payload, CredentialsConstants.FIELD_AUTH_ID);
final String deviceId = getTypesafeValueForField(payload, CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID);
if (type == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else if (authId != null && deviceId == null) {
log.debug("getting credentials [tenant: {}, type: {}, auth-id: {}]", tenantId, type, authId);
final Future<CredentialsResult<JsonObject>> result = Future.future();
get(tenantId, type, authId, result.completer());
return result.map(res -> {
final String deviceIdFromPayload = Optional.ofNullable(res.getPayload()).map(p -> (String) getTypesafeValueForField(p, TenantConstants.FIELD_PAYLOAD_DEVICE_ID)).orElse(null);
return request.getResponse(res.getStatus()).setDeviceId(deviceIdFromPayload).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
});
} else if (deviceId != null && authId == null) {
log.debug("getting credentials for device [tenant: {}, device-id: {}]", tenantId, deviceId);
final Future<CredentialsResult<JsonObject>> result = Future.future();
getAll(tenantId, deviceId, result.completer());
return result.map(res -> {
return request.getResponse(res.getStatus()).setDeviceId(deviceId).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
});
} else {
log.debug("get credentials request contains invalid search criteria [type: {}, device-id: {}, auth-id: {}]", type, deviceId, authId);
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
}
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseRegistrationService method processUpdateRequest.
private Future<EventBusMessage> processUpdateRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final String deviceId = request.getDeviceId();
final JsonObject payload = getRequestPayload(request.getJsonPayload());
if (tenantId == null || deviceId == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
log.debug("updating registration information for device [{}] of tenant [{}]", deviceId, tenantId);
final Future<RegistrationResult> result = Future.future();
updateDevice(tenantId, deviceId, payload, result.completer());
return result.map(res -> {
return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
});
}
}
use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.
the class BaseRegistrationService method processRegisterRequest.
private Future<EventBusMessage> processRegisterRequest(final EventBusMessage request) {
final String tenantId = request.getTenant();
final String deviceId = request.getDeviceId();
final JsonObject payload = getRequestPayload(request.getJsonPayload());
if (tenantId == null || deviceId == null) {
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
log.debug("registering device [{}] for tenant [{}]", deviceId, tenantId);
final Future<RegistrationResult> result = Future.future();
addDevice(tenantId, deviceId, payload, result.completer());
return result.map(res -> {
return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
});
}
}
Aggregations