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