use of org.eclipse.hono.service.management.credentials.CommonCredential 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());
}
use of org.eclipse.hono.service.management.credentials.CommonCredential in project hono by eclipse.
the class CredentialsApiTests method testGetCredentialsFailsForDisabledCredentials.
/**
* Verifies that the service fails when the credentials set is disabled.
*
* @param ctx The vert.x test context.
*/
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsFailsForDisabledCredentials(final VertxTestContext ctx) {
final String deviceId = getHelper().getRandomDeviceId(tenantId);
final String authId = UUID.randomUUID().toString();
final CommonCredential credential = getRandomHashedPasswordCredential(authId);
credential.setEnabled(false);
getHelper().registry.registerDevice(tenantId, deviceId).compose(ok -> getHelper().registry.addCredentials(tenantId, deviceId, Collections.singleton(credential))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, spanContext)).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertErrorCode(t, HttpURLConnection.HTTP_NOT_FOUND));
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.credentials.CommonCredential in project hono by eclipse.
the class CredentialsApiTests method testGetCredentialsByClientContext.
/**
* Verifies that the service returns credentials for a given type, authentication ID and matching client context.
*
* @param ctx The vert.x test context.
*/
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsByClientContext(final VertxTestContext ctx) {
final String deviceId = getHelper().getRandomDeviceId(tenantId);
final String authId = UUID.randomUUID().toString();
final CommonCredential credentials = getRandomHashedPasswordCredential(authId).putExtension("client-id", "gateway-one");
final JsonObject clientContext = new JsonObject().put("client-id", "gateway-one");
getHelper().registry.registerDevice(tenantId, deviceId).compose(httpResponse -> getHelper().registry.addCredentials(tenantId, deviceId, List.of(credentials))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, clientContext, spanContext)).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertStandardProperties(result, deviceId, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, 2);
});
ctx.completeNow();
}));
}
Aggregations