Search in sources :

Example 1 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class BaseCredentialsService method processUpdateRequest.

private Future<EventBusMessage> processUpdateRequest(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();
        update(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));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsResult(org.eclipse.hono.util.CredentialsResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class AbstractDeviceCredentialsTest method testValidateSucceedsIfAnyValidSecretMatches.

/**
 * Verifies that credentials validation succeeds if at least one of the secrets on record is
 * valid and matches the credentials.
 */
@Test
public void testValidateSucceedsIfAnyValidSecretMatches() {
    final AbstractDeviceCredentials creds = getDeviceCredentials("type", "identity", true);
    final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", true).addSecret(CredentialsObject.emptySecret(Instant.now().minusSeconds(120), Instant.now().plusSeconds(120)));
    assertTrue(creds.validate(credentialsOnRecord));
}
Also used : CredentialsObject(org.eclipse.hono.util.CredentialsObject) Test(org.junit.Test)

Example 3 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class AbstractDeviceCredentialsTest method testValidateFailsIfNoSecretsAreValidAnymore.

/**
 * Verifies that credentials validation fails if none of the secrets on record are
 * valid any more.
 */
@Test
public void testValidateFailsIfNoSecretsAreValidAnymore() {
    final AbstractDeviceCredentials creds = getDeviceCredentials("type", "identity", true);
    final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", true).addSecret(CredentialsObject.emptySecret(null, Instant.now().minusSeconds(120)));
    assertFalse(creds.validate(credentialsOnRecord));
}
Also used : CredentialsObject(org.eclipse.hono.util.CredentialsObject) Test(org.junit.Test)

Example 4 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class AbstractDeviceCredentialsTest method testValidateFailsIfNoSecretsAreValidYet.

/**
 * Verifies that credentials validation fails if none of the secrets on record are
 * valid yet.
 */
@Test
public void testValidateFailsIfNoSecretsAreValidYet() {
    final AbstractDeviceCredentials creds = getDeviceCredentials("type", "identity", true);
    final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", true).addSecret(CredentialsObject.emptySecret(Instant.now().plusSeconds(120), null));
    assertFalse(creds.validate(credentialsOnRecord));
}
Also used : CredentialsObject(org.eclipse.hono.util.CredentialsObject) Test(org.junit.Test)

Example 5 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class BaseCredentialsServiceTest method testGetFailsForMissingType.

/**
 * Verifies that the base service fails a request for getting credentials
 * with a 400 error code if the type is missing.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetFailsForMissingType(final TestContext ctx) {
    // GIVEN a request for getting credentials that does not specify a type
    final CredentialsObject malformedPayload = new CredentialsObject().setAuthId("bumlux").addSecret(CredentialsObject.emptySecret(null, null));
    final EventBusMessage request = createRequestForPayload(CredentialsConstants.CredentialsAction.get, JsonObject.mapFrom(malformedPayload));
    // WHEN processing the request
    service.processRequest(request).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the response contains a 400 error code
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) BeforeClass(org.junit.BeforeClass) CredentialsResult(org.eclipse.hono.util.CredentialsResult) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) EventBusMessage(org.eclipse.hono.util.EventBusMessage) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Aggregations

CredentialsObject (org.eclipse.hono.util.CredentialsObject)20 JsonObject (io.vertx.core.json.JsonObject)16 HttpURLConnection (java.net.HttpURLConnection)14 Future (io.vertx.core.Future)13 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)12 Handler (io.vertx.core.Handler)10 ClientErrorException (org.eclipse.hono.client.ClientErrorException)10 CredentialsResult (org.eclipse.hono.util.CredentialsResult)10 MessageHelper (org.eclipse.hono.util.MessageHelper)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 Span (io.opentracing.Span)6 Tracer (io.opentracing.Tracer)6 Vertx (io.vertx.core.Vertx)6 Timeout (io.vertx.junit5.Timeout)6 VertxTestContext (io.vertx.junit5.VertxTestContext)6 Instant (java.time.Instant)6 AnnotatedCacheKey (org.eclipse.hono.client.util.AnnotatedCacheKey)6 VertxExtension (io.vertx.junit5.VertxExtension)5 Constants (org.eclipse.hono.util.Constants)5 Cache (com.github.benmanes.caffeine.cache.Cache)4