use of org.eclipse.hono.deviceregistry.service.credentials.CredentialKey in project hono by eclipse.
the class CredentialsServiceImpl method processGet.
@Override
protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
return this.store.findCredentials(key, span.context()).map(r -> {
if (r.isEmpty()) {
return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
}
final var result = r.get();
final var secrets = result.getCredentials().stream().map(JsonObject::mapFrom).filter(filter(key.getType(), key.getAuthId())).filter(credential -> DeviceRegistryUtils.matchesWithClientContext(credential, clientContext)).flatMap(c -> c.getJsonArray(CredentialsConstants.FIELD_SECRETS).stream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)).filter(CredentialsServiceImpl::filterSecrets).collect(Collectors.toList());
if (secrets.isEmpty()) {
// nothing was left after filtering ... not found
return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
}
final var payload = new JsonObject().put(Constants.JSON_FIELD_DEVICE_ID, result.getDeviceId()).put(CredentialsConstants.FIELD_TYPE, key.getType()).put(CredentialsConstants.FIELD_AUTH_ID, key.getAuthId()).put(CredentialsConstants.FIELD_SECRETS, new JsonArray(secrets));
return CredentialsResult.from(HttpURLConnection.HTTP_OK, payload, getCacheDirective(key.getType(), config.getCredentialsTtl().toSeconds()));
});
}
use of org.eclipse.hono.deviceregistry.service.credentials.CredentialKey in project hono by eclipse.
the class TableAdapterStore method findCredentials.
/**
* Find credentials for a device.
*
* @param key The credentials key to look for.
* @param spanContext The span context.
*
* @return A future tracking the outcome of the operation.
*/
public Future<Optional<CredentialsReadResult>> findCredentials(final CredentialKey key, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "find credentials", getClass().getSimpleName()).withTag(TracingHelper.TAG_AUTH_ID, key.getAuthId()).withTag(TracingHelper.TAG_CREDENTIALS_TYPE, key.getType()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).start();
final var expanded = this.findCredentialsStatement.expand(params -> {
params.put("tenant_id", key.getTenantId());
params.put("type", key.getType());
params.put("auth_id", key.getAuthId());
});
log.debug("findCredentials - statement: {}", expanded);
return expanded.trace(this.tracer, span.context()).query(this.client).<Optional<CredentialsReadResult>>flatMap(r -> {
final var entries = r.getRows(true);
span.log(Map.of("event", "read result", "rows", entries.size()));
final Set<String> deviceIds = entries.stream().map(o -> o.getString("device_id")).filter(Objects::nonNull).collect(Collectors.toSet());
final int num = deviceIds.size();
if (num <= 0) {
return Future.succeededFuture(Optional.empty());
} else if (num > 1) {
TracingHelper.logError(span, "Found multiple entries for a single device");
return Future.failedFuture(new IllegalStateException("Found multiple entries for a single device"));
}
// we know now that we have exactly one entry
final String deviceId = deviceIds.iterator().next();
final List<CommonCredential> credentials = entries.stream().map(o -> o.getString("data")).map(s -> Json.decodeValue(s, CommonCredential.class)).collect(Collectors.toList());
return Future.succeededFuture(Optional.of(new CredentialsReadResult(deviceId, credentials, Optional.empty())));
}).onComplete(x -> span.finish());
}
Aggregations