@@ -84,20 +84,25 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
8484 _connection = &connection;
8585 _brokerAddress = brokerAddress;
8686
87+ ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE;
88+ #if defined (BOARD_HAS_SECRET_KEY)
89+ /* If board supports and sketch is configured for username and password login */
90+ if (_password.length ()) {
91+ authMode = ArduinoIoTAuthenticationMode::PASSWORD;
92+ }
93+ #endif
94+
8795 /* Setup broker TLS client */
88- _brokerClient.begin (connection);
96+ _brokerClient.begin (connection, authMode );
8997
9098#if OTA_ENABLED
9199 /* Setup OTA TLS client */
92100 _otaClient.begin (connection);
93101#endif
94102
95- #if defined (BOARD_HAS_SECRET_KEY)
96- /* If board is not configured for username and password login */
97- if (!_password.length ())
103+ /* If board is configured for certificate authentication and mTLS */
104+ if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE)
98105 {
99- #endif
100-
101106#if defined(BOARD_HAS_SECURE_ELEMENT)
102107 if (!_selement.begin ())
103108 {
@@ -130,14 +135,11 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
130135 #endif
131136 _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? mqttPort () : brokerPort;
132137#endif
133-
134- #if defined(BOARD_HAS_SECRET_KEY)
135138 }
136139 else
137140 {
138141 _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
139142 }
140- #endif
141143
142144 /* Setup TimeService */
143145 _time_service.begin (_connection);
0 commit comments