use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class AuthenticationServerClient method verifyPlain.
/**
* Verifies username/password credentials with a remote authentication server using SASL PLAIN.
*
* @param authzid The identity to act as.
* @param authcid The username.
* @param password The password.
* @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication,
* the result contains a JWT with the authenticated user's claims.
*/
public void verifyPlain(final String authzid, final String authcid, final String password, final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {
ProtonClientOptions options = new ProtonClientOptions();
options.setReconnectAttempts(3).setReconnectInterval(50);
options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN);
factory.connect(options, authcid, password, null, null, conAttempt -> {
if (conAttempt.failed()) {
authenticationResultHandler.handle(Future.failedFuture("cannot connect to Authentication service"));
} else {
final ProtonConnection openCon = conAttempt.result();
final Future<HonoUser> userTracker = Future.future();
userTracker.setHandler(s -> {
if (s.succeeded()) {
authenticationResultHandler.handle(Future.succeededFuture(s.result()));
} else {
authenticationResultHandler.handle(Future.failedFuture(s.cause()));
}
ProtonConnection con = conAttempt.result();
if (con != null) {
LOG.debug("closing connection to Authentication service");
con.close();
}
});
vertx.setTimer(5000, tid -> {
if (!userTracker.isComplete()) {
userTracker.fail("time out reached while waiting for token from Authentication service");
}
});
getToken(openCon, userTracker);
}
});
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class AmqpServiceBaseTest method createServer.
private AmqpServiceBase<ServiceConfigProperties> createServer(final AmqpEndpoint amqpEndpoint, final Handler<ProtonConnection> onClientDisconnect) {
final AmqpServiceBase<ServiceConfigProperties> server = new AmqpServiceBase<ServiceConfigProperties>() {
@Override
protected String getServiceName() {
return "AmqpServiceBase";
}
@Override
public void setConfig(final ServiceConfigProperties configuration) {
setSpecificConfig(configuration);
}
@Override
protected void publishConnectionClosedEvent(final ProtonConnection con) {
if (onClientDisconnect != null) {
onClientDisconnect.handle(con);
}
}
};
server.setConfig(new ServiceConfigProperties());
if (amqpEndpoint != null) {
server.addEndpoint(amqpEndpoint);
}
server.init(vertx, mock(Context.class));
return server;
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class RequestResponseEndpointTest method testHandleMessageRejectsMalformedMessage.
/**
* Verifies that the endpoint rejects malformed request messages.
*/
@Test
public void testHandleMessageRejectsMalformedMessage() {
Message msg = ProtonHelper.message();
ProtonConnection con = mock(ProtonConnection.class);
ProtonDelivery delivery = mock(ProtonDelivery.class);
RequestResponseEndpoint<ServiceConfigProperties> endpoint = getEndpoint(false);
// WHEN a malformed message is received
endpoint.handleMessage(con, receiver, resource, delivery, msg);
// THEN the link is closed and the message is rejected
ArgumentCaptor<DeliveryState> deliveryState = ArgumentCaptor.forClass(DeliveryState.class);
verify(delivery).disposition(deliveryState.capture(), booleanThat(is(Boolean.TRUE)));
assertThat(deliveryState.getValue(), instanceOf(Rejected.class));
verify(receiver, never()).close();
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class RequestResponseEndpointTest method testHandleMessageProcessesAuthorizedRequests.
/**
* Verifies that the endpoint processes request messages for operations the client
* is authorized to invoke.
*/
@Test
public void testHandleMessageProcessesAuthorizedRequests() {
Message msg = ProtonHelper.message();
msg.setSubject("get");
ProtonConnection con = mock(ProtonConnection.class);
ProtonDelivery delivery = mock(ProtonDelivery.class);
AuthorizationService authService = mock(AuthorizationService.class);
when(authService.isAuthorized(any(HonoUser.class), any(ResourceIdentifier.class), anyString())).thenReturn(Future.succeededFuture(Boolean.TRUE));
Future<Void> processingTracker = Future.future();
RequestResponseEndpoint<ServiceConfigProperties> endpoint = getEndpoint(true, processingTracker);
endpoint.setAuthorizationService(authService);
// WHEN a request for an operation is received that the client is authorized to invoke
endpoint.handleMessage(con, receiver, resource, delivery, msg);
// THEN then the message gets processed
ArgumentCaptor<DeliveryState> deliveryState = ArgumentCaptor.forClass(DeliveryState.class);
verify(delivery).disposition(deliveryState.capture(), booleanThat(is(Boolean.TRUE)));
assertThat(deliveryState.getValue(), instanceOf(Accepted.class));
verify(receiver, never()).close();
verify(authService).isAuthorized(Constants.PRINCIPAL_ANONYMOUS, resource, "get");
assertTrue(processingTracker.isComplete());
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class RequestResponseEndpointTest method testHandleMessageRejectsUnauthorizedRequests.
/**
* Verifies that the endpoint rejects request messages for operations the client
* is not authorized to invoke.
*/
@Test
public void testHandleMessageRejectsUnauthorizedRequests() {
Message msg = ProtonHelper.message();
msg.setSubject("unauthorized");
ProtonConnection con = mock(ProtonConnection.class);
ProtonDelivery delivery = mock(ProtonDelivery.class);
AuthorizationService authService = mock(AuthorizationService.class);
when(authService.isAuthorized(any(HonoUser.class), any(ResourceIdentifier.class), anyString())).thenReturn(Future.succeededFuture(Boolean.FALSE));
Future<Void> processingTracker = Future.future();
RequestResponseEndpoint<ServiceConfigProperties> endpoint = getEndpoint(true, processingTracker);
endpoint.setAuthorizationService(authService);
// WHEN a request for an operation is received that the client is not authorized to invoke
endpoint.handleMessage(con, receiver, resource, delivery, msg);
// THEN the the message is rejected
ArgumentCaptor<DeliveryState> deliveryState = ArgumentCaptor.forClass(DeliveryState.class);
verify(delivery).disposition(deliveryState.capture(), booleanThat(is(Boolean.TRUE)));
assertThat(deliveryState.getValue(), instanceOf(Rejected.class));
verify(receiver, never()).close();
verify(authService).isAuthorized(Constants.PRINCIPAL_ANONYMOUS, resource, "unauthorized");
assertFalse(processingTracker.isComplete());
}
Aggregations