From a22e6abc053966659060d2116bfe9f0dff99ed4c Mon Sep 17 00:00:00 2001 From: Mohamed El-Kalioby Date: Fri, 2 Jun 2023 11:16:01 +0300 Subject: [PATCH 1/2] Make Result JSON Serializable --- soft_webauthn.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/soft_webauthn.py b/soft_webauthn.py index 2cb9acc..071a5a9 100644 --- a/soft_webauthn.py +++ b/soft_webauthn.py @@ -5,7 +5,7 @@ import json import os -from base64 import urlsafe_b64encode +from base64 import urlsafe_b64encode, urlsafe_b64decode from struct import pack from cryptography.hazmat.backends import default_backend @@ -14,7 +14,7 @@ from fido2 import cbor from fido2.cose import ES256 from fido2.webauthn import AttestedCredentialData -from fido2.utils import sha256 +from fido2.utils import sha256, websafe_encode class SoftWebauthnDevice(): @@ -63,29 +63,29 @@ def create(self, options, origin): # generate credential response client_data = { 'type': 'webauthn.create', - 'challenge': urlsafe_b64encode(options['publicKey']['challenge']).decode('ascii').rstrip('='), + 'challenge': options['publicKey']['challenge'].decode("utf8").rstrip('='), 'origin': origin } - rp_id_hash = sha256(self.rp_id.encode('ascii')) + rp_id_hash = sha256(self.rp_id.encode("utf8")) flags = b'\x41' # attested_data + user_present sign_count = pack('>I', self.sign_count) credential_id_length = pack('>H', len(self.credential_id)) cose_key = cbor.encode(ES256.from_cryptography_key(self.private_key.public_key())) attestation_object = { 'authData': - rp_id_hash + flags + sign_count - + self.aaguid + credential_id_length + self.credential_id + cose_key, + (rp_id_hash + flags + sign_count + + self.aaguid + credential_id_length + self.credential_id + cose_key), 'fmt': 'none', 'attStmt': {} } return { - 'id': urlsafe_b64encode(self.credential_id), - 'rawId': self.credential_id, + 'id': urlsafe_b64encode(self.credential_id).decode("utf8"), + 'rawId': self.credential_id.decode("latin-1"), 'response': { - 'clientDataJSON': json.dumps(client_data).encode('utf-8'), - 'attestationObject': cbor.encode(attestation_object) + 'clientDataJSON': urlsafe_b64encode(json.dumps(client_data).encode("utf8")).decode("ascii"), + 'attestationObject': urlsafe_b64encode(cbor.encode(attestation_object)).decode("utf8") }, 'type': 'public-key' } @@ -101,9 +101,9 @@ def get(self, options, origin): # prepare signature client_data = json.dumps({ 'type': 'webauthn.get', - 'challenge': urlsafe_b64encode(options['publicKey']['challenge']).decode('ascii').rstrip('='), + 'challenge': (options['publicKey']['challenge']).decode('ascii').rstrip('='), 'origin': origin - }).encode('utf-8') + }).encode("utf8") client_data_hash = sha256(client_data) rp_id_hash = sha256(self.rp_id.encode('ascii')) @@ -115,12 +115,12 @@ def get(self, options, origin): # generate assertion return { - 'id': urlsafe_b64encode(self.credential_id), - 'rawId': self.credential_id, + 'id': urlsafe_b64encode(self.credential_id).decode("ascii"), + 'rawId': urlsafe_b64encode(self.credential_id).decode("ascii"), 'response': { - 'authenticatorData': authenticator_data, - 'clientDataJSON': client_data, - 'signature': signature, + 'authenticatorData': urlsafe_b64encode(authenticator_data).decode("ascii"), + 'clientDataJSON': urlsafe_b64encode(client_data).decode('ascii'), + 'signature': urlsafe_b64encode(signature).decode('ascii'), 'userHandle': self.user_handle }, 'type': 'public-key' From cbcbe70c731c270f227bf3b6cc9e89f72b21362e Mon Sep 17 00:00:00 2001 From: Mohamed El-Kalioby Date: Fri, 2 Jun 2023 11:18:18 +0300 Subject: [PATCH 2/2] Update soft_webauthn.py --- soft_webauthn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/soft_webauthn.py b/soft_webauthn.py index 071a5a9..3e36738 100644 --- a/soft_webauthn.py +++ b/soft_webauthn.py @@ -5,7 +5,7 @@ import json import os -from base64 import urlsafe_b64encode, urlsafe_b64decode +from base64 import urlsafe_b64encode from struct import pack from cryptography.hazmat.backends import default_backend @@ -14,7 +14,7 @@ from fido2 import cbor from fido2.cose import ES256 from fido2.webauthn import AttestedCredentialData -from fido2.utils import sha256, websafe_encode +from fido2.utils import sha256 class SoftWebauthnDevice():