Skip to content

Commit

Permalink
testing scripst for smart authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbeor committed Jan 8, 2024
1 parent 9e95f46 commit ba589ed
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/test/SMART-test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"e\":\"AQAB\",\"kid\":\"kU3EOUCL5nQqLocgGehYRBMted9yBL5MCAtQJXZSJWg\",\"kty\":\"RSA\",\"n\":\"rpTcVZlAfCJdnPmfZ_9mJXIiz4E-pEQyIT_GR1nWyUwGoZ9VoI_TApcZGv02maZV3VCrOtMsULTFFYEfrPxk1HXrQTChO4b_SlR9LXP_5S6WsqcyVsmuWXUlSh06cMeKAx6aeNPNXe7FjptxqPsrr_VF3SZnyi5EqtgSRpeXvm58HU5fUjKcbUIKDG5YMQfZkk42lGoF_xUHVHv1U-heQm6UiCXKCQ9w0MObk9YezughpXGzuXVosRheMAbQoByAZdn8xIrO5y10CTOowp26Dd5rJx_cgDf8BLf_LewjTeg-P73HegpQ-0SGGiwU5yOisCzzUlg8tM8IyDy0Ud7nLxffqMqKimDaTuWPCWopMQuMSItJRDdtFImSvIogth2JsJpHgZLA8EzNj52qkAbfxoxyHPndqve71q6eYhN_f-_I2QUJAyIZPoLe76b9NPR2yLb3eXs1Mxbk--ztW8kmtDnuuxY1B7CJH-sJB1W8i_fhFXnoE8ipml2c87MdpItTg-4UaE3Gb6f6jUKf3QwGn8dRTLc6VPEctKH0dDM5FoiHuyAyqfiwtZGAQmXV9zUdGa8Shl8OOhbvWcTCg1RG42DOPUYMkNyHcG6fNzE17hkOajxjoSy4ayT-9OZ2PdrhyJJHExwC_w5Y7PkPmmCeaOuejJKPO6jrcYcKHTiC57c\"}\n",
"200\n",
"b'JWK REGISTERED'\n"
]
}
],
"source": [
"import requests\n",
"import jwt\n",
"import time\n",
"import secrets\n",
"from jwcrypto import jwk\n",
"baseUrl = \"http://localhost:8080/iis\"\n",
"tenant = \"Connectathon\"\n",
"fhirUrl = baseUrl + \"/fhir/\" + tenant\n",
"session = requests.Session()\n",
"headers={\"Content-Type\": \"application/json\"}\n",
"alg = \"RS384\"\n",
"\n",
"key = jwk.JWK.generate(kty='RSA', alg=alg, size=2048) \n",
"# Use following command to generate private key\n",
"# ssh-keygen -t rsa -b 4096 -E SHA384 -m PEM -P \"\" -f RS384.key\n",
"with open(\"RS384.key\", \"rb\") as pemfile: \n",
" key = jwk.JWK.from_pem(pemfile.read())\n",
"print(key.export_public())\n",
"register = session.post(f\"{baseUrl}/registerClient\", data=key.export_public())\n",
"print(register.status_code)\n",
"print(register.content)"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"eyJhbGciOiJSUzM4NCIsImtpZCI6ImtVM0VPVUNMNW5RcUxvY2dHZWhZUkJNdGVkOXlCTDVNQ0F0UUpYWlNKV2ciLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJjbGllbnQtaWQiLCJzdWIiOiJyZWdpc3RlcmVkLWNsaWVudC1pZCIsImF1ZCI6IiIsImV4cCI6MTcwNDczMDI1OTYzNCwianRpIjoib3pKZTNMbmxHVjJkaTA4cjRoVl9VbFVCc2lGeUpkTUhvb2NWd0d1RDdoSSJ9.UJrmEWpNX0Z4HDLB_aOx20DIzQjBJR-Y6JNL3kPL13d4wONoi66NpQcltTeo-VLEsshubz3ZbqpBkGYSdn6zjy53DbkXC7O34AQAFHwDq9KkvoV5GwxmSsG7znXBsVyjOcJ7RTAF38Ihu_715-uLz1ye5-875_Jz1U54XxaMy9w0XBMhd_LpgHp3Ymt2x_krVClpW3RX4fJsvBSYvlGd94ReNKpeQB3DjKaxYbD74LcTx1I9AnmPZI8g2NgS0bAxK90TKwkbQ64P4nAk-onEj0sYU4OOlRk351I5HkINdffIMknraHZ4XVUWTY1AZPfVMbpW84a5HkR74BUxkmdKbR4Gr3Sl7uX_Qcahs24JIcROm5JYBUhoZRD-pIsXNkiIWtS7RtljOmZU1zoufkfC9BOzflhieNb5bMR7Qb7jTb5wkhFSZNqlO7p_yVEEGWkPlYnABskkc82YJanBVUtlvGpbtg2WRCBdGS5_4HrNFKOq4_Sh8S6PoGPrYKdWboaJiUspSNqvHUONEpbxszmg0B5W-LGU0uPgi2A31NOPMevCEOxxnWOaV-XixaUZxPj4ADy3doqNFZMi7nBBl8IwSLJvkiOx9TF8Zec1XLcZY8uY3tPciw43Hm-kYVFYdZwoWpbFWp8RnMtvGfpLARJt-o7GGd_x2Oa-cku6-KTBKG4\n",
"{'iss': 'client-id', 'sub': 'registered-client-id', 'aud': '', 'exp': 1704730259634, 'jti': 'ozJe3LnlGV2di08r4hV_UlUBsiFyJdMHoocVwGuD7hI'}\n",
"<Response [200]>\n"
]
}
],
"source": [
"jwt_claims = {\"iss\": \"client-id\",\"sub\": \"registered-client-id\", \"aud\": \"\", \"exp\": int(time.time()*1000) + 30000, \"jti\": secrets.token_urlsafe()}\n",
"jwt_headers = {\"alg\":alg, \"kid\":key.get(\"kid\"), \"typ\":\"JWT\"}\n",
"encoded_jwt = jwt.encode(payload=jwt_claims, key=private, algorithm=alg, headers=jwt_headers)\n",
"print(encoded_jwt)\n",
"decode = jwt.decode(encoded_jwt, public, algorithms=[alg])\n",
"print(decode)\n",
"login = session.post(f\"{baseUrl}/token\", params={\"client_assertion_type\": \"urn:ietf:params:oauth:client-assertion-type:jwt-bearer\", \"client_assertion\": encoded_jwt})\n",
"print(login)\n",
"token = login.text"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [500]>\n"
]
}
],
"source": [
"result = session.get(f\"{fhirUrl}/Patient\", headers={\"Authorization\": f\"Bearer {token}\"})\n",
"print(result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions src/test/generateKey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-keygen -t rsa -b 4096 -E SHA384 -m PEM -P "" -f RS384.key;

0 comments on commit ba589ed

Please sign in to comment.