Skip to content

Commit fa7436e

Browse files
authored
Merge pull request #164 from zbauman3/zane/cert-key-pair-example
Add examples using cert/key pair
2 parents 9d4615b + 877f7bd commit fa7436e

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

examples/native_networking/minimqtt_adafruitio_native_networking.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
### Code ###
3838

39+
3940
# Define callback methods which are called when events occur
4041
# pylint: disable=unused-argument, redefined-outer-name
4142
def connected(client, userdata, flags, rc):
@@ -59,6 +60,16 @@ def message(client, topic, message):
5960

6061
# Create a socket pool
6162
pool = socketpool.SocketPool(wifi.radio)
63+
ssl_context = ssl.create_default_context()
64+
65+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
66+
# ssl context by uncommenting the lines below and adding the following keys to the "secrets"
67+
# dictionary in your secrets.py file:
68+
# "device_cert_path" - Path to the Device Certificate
69+
# "device_key_path" - Path to the RSA Private Key
70+
# ssl_context.load_cert_chain(
71+
# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"]
72+
# )
6273

6374
# Set up a MiniMQTT Client
6475
mqtt_client = MQTT.MQTT(
@@ -67,7 +78,7 @@ def message(client, topic, message):
6778
username=secrets["aio_username"],
6879
password=secrets["aio_key"],
6980
socket_pool=pool,
70-
ssl_context=ssl.create_default_context(),
81+
ssl_context=ssl_context,
7182
)
7283

7384
# Setup the callback methods above

examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# Setup a feed named `testfeed` for publishing.
3333
default_topic = secrets["aio_username"] + "/feeds/testfeed"
3434

35+
3536
### Code ###
3637
# Define callback methods which are called when events occur
3738
# pylint: disable=unused-argument, redefined-outer-name
@@ -59,6 +60,16 @@ def message(client, topic, message):
5960

6061
# Create a socket pool
6162
pool = socketpool.SocketPool(wifi.radio)
63+
ssl_context = ssl.create_default_context()
64+
65+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
66+
# ssl context by uncommenting the lines below and adding the following keys to the "secrets"
67+
# dictionary in your secrets.py file:
68+
# "device_cert_path" - Path to the Device Certificate
69+
# "device_key_path" - Path to the RSA Private Key
70+
# ssl_context.load_cert_chain(
71+
# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"]
72+
# )
6273

6374
# Set up a MiniMQTT Client
6475
mqtt_client = MQTT.MQTT(
@@ -67,7 +78,7 @@ def message(client, topic, message):
6778
username=secrets["aio_username"],
6879
password=secrets["aio_key"],
6980
socket_pool=pool,
70-
ssl_context=ssl.create_default_context(),
81+
ssl_context=ssl_context,
7182
)
7283

7384
# Setup the callback methods above

examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
### Code ###
3131

32+
3233
# Define callback methods which are called when events occur
3334
# pylint: disable=unused-argument, redefined-outer-name
3435
def connected(client, userdata, flags, rc):
@@ -66,6 +67,16 @@ def on_message(client, topic, message):
6667

6768
# Create a socket pool
6869
pool = socketpool.SocketPool(wifi.radio)
70+
ssl_context = ssl.create_default_context()
71+
72+
# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
73+
# ssl context by uncommenting the lines below and adding the following keys to the "secrets"
74+
# dictionary in your secrets.py file:
75+
# "device_cert_path" - Path to the Device Certificate
76+
# "device_key_path" - Path to the RSA Private Key
77+
# ssl_context.load_cert_chain(
78+
# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"]
79+
# )
6980

7081
# Set up a MiniMQTT Client
7182
client = MQTT.MQTT(
@@ -74,7 +85,7 @@ def on_message(client, topic, message):
7485
username=secrets["aio_username"],
7586
password=secrets["aio_key"],
7687
socket_pool=pool,
77-
ssl_context=ssl.create_default_context(),
88+
ssl_context=ssl_context,
7889
)
7990

8091
# Setup the callback methods above

0 commit comments

Comments
 (0)