X509AuthHandler extends ExecutionContextAuthHandler<MqttConnectContext> { @Override public Future<JsonObject> parseCredentials(final MqttConnectContext context) { Objects.requireNonNull(context); if (context.deviceEndpoint() == null) { throw new IllegalArgumentException("no device endpoint"); } else if (context.deviceEndpoint().isSsl()) { try { final Certificate[] path = context.deviceEndpoint().sslSession().getPeerCertificates(); final SpanContext currentSpan = context.getTracingContext(); return auth.validateClientCertificate(path, currentSpan) .map(authInfo -> authInfo.put(PROPERTY_CLIENT_IDENTIFIER, context.deviceEndpoint().clientIdentifier())); } catch (SSLPeerUnverifiedException e) { log.debug("could not retrieve client certificate from device endpoint: {}", e.getMessage()); return Future.failedFuture(UNAUTHORIZED); } } else { return Future.failedFuture(UNAUTHORIZED); } } X509AuthHandler(
            final X509Authentication clientAuth,
            final DeviceCredentialsAuthProvider<SubjectDnCredentials> authProvider);  X509AuthHandler(
            final X509Authentication clientAuth,
            final DeviceCredentialsAuthProvider<SubjectDnCredentials> authProvider,
            final PreCredentialsValidationHandler<MqttConnectContext> preCredentialsValidationHandler); @Override Future<JsonObject> parseCredentials(final MqttConnectContext context);  }
@Test public void testParseCredentialsIncludesMqttClientId(final VertxTestContext ctx) throws SSLPeerUnverifiedException { final JsonObject authInfo = new JsonObject() .put(RequestResponseApiConstants.FIELD_PAYLOAD_SUBJECT_DN, "CN=device") .put(RequestResponseApiConstants.FIELD_PAYLOAD_TENANT_ID, "tenant"); when(clientAuth.validateClientCertificate(any(Certificate[].class), (SpanContext) any())) .thenReturn(Future.succeededFuture(authInfo)); final X509Certificate clientCert = getClientCertificate("CN=device", "CN=tenant"); final SSLSession sslSession = mock(SSLSession.class); when(sslSession.getPeerCertificates()).thenReturn(new X509Certificate[] { clientCert }); final MqttEndpoint endpoint = mock(MqttEndpoint.class); when(endpoint.isSsl()).thenReturn(true); when(endpoint.sslSession()).thenReturn(sslSession); when(endpoint.clientIdentifier()).thenReturn("mqtt-device"); final MqttConnectContext context = MqttConnectContext.fromConnectPacket(endpoint, span); authHandler.parseCredentials(context) .onComplete(ctx.succeeding(info -> { ctx.verify(() -> { assertThat(info.getString(RequestResponseApiConstants.FIELD_PAYLOAD_SUBJECT_DN)).isEqualTo("CN=device"); assertThat(info.getString(RequestResponseApiConstants.FIELD_PAYLOAD_TENANT_ID)).isEqualTo("tenant"); assertThat(info.getString(X509AuthHandler.PROPERTY_CLIENT_IDENTIFIER)).isEqualTo("mqtt-device"); }); ctx.completeNow(); })); }