Mass refactor. Clang-format. Start adding tests. Use FetchContent.

This commit is contained in:
jimmy committed 2022-03-24 00:20:21 +00:00
1 parent c76ff553cc
commit 3309b19c2e
19 files changed
+310 -223

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
import requests
import json
import time
import jwt
# Get urls.
well_known = requests.get("https://auth.jpace121.net/realms/jpace121-main/.well-known/openid-configuration")
well_known = well_known.json()
auth_url = well_known['device_authorization_endpoint']
token_url = well_known['token_endpoint']
header = {"Content-Type":"application/x-www-form-urlencoded"}
data = {"client_id" : "jpace-mqtt"}
r = requests.post(auth_url, headers=header, data=data)
r_json = r.json()
print("Go to: {} to login.".format(r_json['verification_uri_complete']))
data['grant_type'] = "urn:ietf:params:oauth:grant-type:device_code"
data['device_code'] = r_json['device_code']
for index in range(1, 20):
r = requests.post(token_url, headers=header, data=data)
if 'access_token' in r.json():
break
time.sleep(r_json['interval'])
public_key = b"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlIKdtC04YbRMO0L4ID4YOWLr2AxYpQZYZ3g9BNpVm+IjDdn4H5HaYwYvOcbdjKyRdmwm+rsrIbWxCGYQCD5TtaCnq1IGwOueoprgCTDNSpTxsKQ+JuEUIhKc4rygVhX7JKIvVikfWimKVuNJBVhut/O+/N0AarasszAyinc3gjwtu2SyLBdZtIe3Krs1MIvYb786J2RhK3GfLzrXVzmKjA2/ThB9D6sS7dtZCe//37kYZzGUv5+xFkjkKwZr2aULMlmpUosFd/S2w3zsZkGRELLTvdRf5PVKeGpk40EneETJAHwiMjX6+jO/vlFQIj/Ye66ypVhCCI+NizE/hWbdawIDAQAB\n-----END PUBLIC KEY-----"
#decoded = jwt.decode(r.json()['access_token'], audience=None, options={"verify_signature": False, 'verify_aud': False})
decoded = jwt.decode(r.json()['access_token'], public_key, algorithms=['RS256'])
print('Decoded: {}'.format(decoded))
+3
View File
@@ -0,0 +1,3 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlIKdtC04YbRMO0L4ID4YOWLr2AxYpQZYZ3g9BNpVm+IjDdn4H5HaYwYvOcbdjKyRdmwm+rsrIbWxCGYQCD5TtaCnq1IGwOueoprgCTDNSpTxsKQ+JuEUIhKc4rygVhX7JKIvVikfWimKVuNJBVhut/O+/N0AarasszAyinc3gjwtu2SyLBdZtIe3Krs1MIvYb786J2RhK3GfLzrXVzmKjA2/ThB9D6sS7dtZCe//37kYZzGUv5+xFkjkKwZr2aULMlmpUosFd/S2w3zsZkGRELLTvdRf5PVKeGpk40EneETJAHwiMjX6+jO/vlFQIj/Ye66ypVhCCI+NizE/hWbdawIDAQAB
-----END PUBLIC KEY-----
+17
View File
@@ -0,0 +1,17 @@
per_listener_settings true
log_type all
listener 8082
protocol websockets
allow_anonymous false
auth_plugin /home/jimmy/Develop/mosquitto-plugin/build/libj7s-plugin.so
auth_opt_issuer https://auth.jpace121.net/realms/jpace121-main
auth_opt_public_key /home/jimmy/Develop/mosquitto-plugin/test/key.pem
listener 8081
protocol mqtt
allow_anonymous false
auth_plugin /home/jimmy/Develop/mosquitto-plugin/build/libj7s-plugin.so
auth_opt_issuer https://auth.jpace121.net/realms/jpace121-main
auth_opt_public_key /home/jimmy/Develop/mosquitto-plugin/test/key.pem
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
mosquitto_pub -u james -P test -h localhost -p 8081 -t test -m "test"
+39
View File
@@ -0,0 +1,39 @@
import requests
import json
import time
import paho.mqtt.client
import time
# Get urls.
well_known = requests.get("https://auth.jpace121.net/realms/jpace121-main/.well-known/openid-configuration").json()
auth_url = well_known['device_authorization_endpoint']
token_url = well_known['token_endpoint']
header = {"Content-Type":"application/x-www-form-urlencoded"}
data = {"client_id" : "jpace-mqtt"}
# Request login.
r = requests.post(auth_url, headers=header, data=data)
r_json = r.json()
print("Go to: {} to login.".format(r_json['verification_uri_complete']))
# Wait for token.
data['grant_type'] = "urn:ietf:params:oauth:grant-type:device_code"
data['device_code'] = r_json['device_code']
for index in range(1, 20):
r = requests.post(token_url, headers=header, data=data)
if 'access_token' in r.json():
break
time.sleep(r_json['interval'])
token = r.json()['access_token']
print(token)
client = paho.mqtt.client.Client(protocol=paho.mqtt.client.MQTTv5,
transport="tcp")
client.username_pw_set("jimmy", password=token)
client.connect("localhost", port=8081)
print("Waiting on connection.")
time.sleep(20)
+36
View File
@@ -0,0 +1,36 @@
import requests
import json
import time
import paho.mqtt.client
import time
# RESULT=`curl --cacert ./ca.pem --cert jimmy-client.pem --key jimmy-client-key.pem --data "grant_type=password&client_id=test&username=jimmy&password=1234" https://nginx-test.internal.jpace121.net:8443/realms/master/protocol/openid-connect/token`
s = requests.Session()
s.verify = "/home/jimmy/Develop/mosquitto-plugin/test/ca.pem"
s.cert = ("/home/jimmy/Develop/keycloak/jimmy-client.pem", "/home/jimmy/Develop/keycloak/jimmy-client-key.pem")
# Get urls.
print("Before get.")
well_known = s.get("https://nginx-test.internal.jpace121.net:8443/realms/jpace121-main/.well-known/openid-configuration", ).json()
print("After get.")
token_url = well_known['token_endpoint']
# Override for now, url doesn't include port...
token_url = "https://nginx-test.internal.jpace121.net:8443/realms/jpace121-main/protocol/openid-connect/token"
# RESULT=`curl --cacert ./ca.pem --cert jimmy-client.pem --key jimmy-client-key.pem --data "grant_type=password&client_id=mqtt&username=jimmy&password=1234" https://nginx-test.internal.jpace121.net:8443/realms/jpace121-main/protocol/openid-connect/token`
data = {"grant_type" : "password", "client_id" : "mqtt", "username" : "jimmy", "password": "1234"}
# Request token.
r = s.post(token_url, data=data)
token = r.json()['access_token']
client = paho.mqtt.client.Client(protocol=paho.mqtt.client.MQTTv5,
transport="tcp")
client.username_pw_set("jimmy", password=token)
client.connect("localhost", port=8081)
print("Waiting on connection.")
time.sleep(20)
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
/usr/sbin/mosquitto -v -c $PWD/mosquitto.conf