django-mfa3

multi factor authentication for django
git clone https://git.ce9e.org/django-mfa3.git

commit
c3896f0e1343e938df0ecab89102a9620de3eff4
parent
fbbc19dcaa50b50e40e5476ed774d48b056cbe42
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-02-03 15:06
js: convert to module

Diffstat

M mfa/static/mfa/fido2.js 98 ++++++++++++++++++++++++++++++-------------------------------
M mfa/templates/mfa/auth_FIDO2.html 2 +-
M mfa/templates/mfa/create_FIDO2.html 2 +-

3 files changed, 50 insertions, 52 deletions


diff --git a/mfa/static/mfa/fido2.js b/mfa/static/mfa/fido2.js

@@ -1,56 +1,54 @@
    1    -1 (function() {
    2    -1     var encode = function(data) {
    3    -1         var buffer = CBOR.encode(data);
    4    -1         var arr = new Uint8Array(buffer);
    5    -1         return arr.reduce((s, b) => s + b.toString(16).padStart(2, '0'), '');
    6    -1     };
   -1     1 var encode = function(data) {
   -1     2     var buffer = CBOR.encode(data);
   -1     3     var arr = new Uint8Array(buffer);
   -1     4     return arr.reduce((s, b) => s + b.toString(16).padStart(2, '0'), '');
   -1     5 };
    7     6 
    8    -1     var decode = function(hex) {
    9    -1         var arr = new Uint8Array(hex.length / 2);
   10    -1         for (var i = 0; i < arr.length; i += 1) {
   11    -1             arr[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
   12    -1         }
   13    -1         return CBOR.decode(arr.buffer);
   14    -1     };
   -1     7 var decode = function(hex) {
   -1     8     var arr = new Uint8Array(hex.length / 2);
   -1     9     for (var i = 0; i < arr.length; i += 1) {
   -1    10         arr[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
   -1    11     }
   -1    12     return CBOR.decode(arr.buffer);
   -1    13 };
   15    14 
   16    -1     var initCreate = function() {
   17    -1         var form = document.querySelector('form[data-fido2-create]');
   18    -1         if (form) {
   19    -1             var options = decode(form.dataset.fido2Create);
   20    -1             form.addEventListener('submit', function(event) {
   21    -1                 event.preventDefault();
   -1    15 var initCreate = function() {
   -1    16     var form = document.querySelector('form[data-fido2-create]');
   -1    17     if (form) {
   -1    18         var options = decode(form.dataset.fido2Create);
   -1    19         form.addEventListener('submit', function(event) {
   -1    20             event.preventDefault();
   22    21 
   23    -1                 navigator.credentials.create(options).then(attestation => {
   24    -1                     this.code.value = encode({
   25    -1                         'attestationObject': new Uint8Array(attestation.response.attestationObject),
   26    -1                         'clientData': new Uint8Array(attestation.response.clientDataJSON),
   27    -1                     });
   28    -1                     form.submit();
   29    -1                 }).catch(alert);
   30    -1             });
   31    -1         }
   32    -1     };
   -1    22             navigator.credentials.create(options).then(attestation => {
   -1    23                 this.code.value = encode({
   -1    24                     'attestationObject': new Uint8Array(attestation.response.attestationObject),
   -1    25                     'clientData': new Uint8Array(attestation.response.clientDataJSON),
   -1    26                 });
   -1    27                 form.submit();
   -1    28             }).catch(alert);
   -1    29         });
   -1    30     }
   -1    31 };
   33    32 
   34    -1     var initAuth = function() {
   35    -1         var form = document.querySelector('form[data-fido2-auth]');
   36    -1         if (form) {
   37    -1             var options = decode(form.dataset.fido2Auth);
   38    -1             form.addEventListener('submit', function(event) {
   39    -1                 event.preventDefault();
   -1    33 var initAuth = function() {
   -1    34     var form = document.querySelector('form[data-fido2-auth]');
   -1    35     if (form) {
   -1    36         var options = decode(form.dataset.fido2Auth);
   -1    37         form.addEventListener('submit', function(event) {
   -1    38             event.preventDefault();
   40    39 
   41    -1                 navigator.credentials.get(options).then(assertion => {
   42    -1                     this.code.value = encode({
   43    -1                         'credentialId': new Uint8Array(assertion.rawId),
   44    -1                         'authenticatorData': new Uint8Array(assertion.response.authenticatorData),
   45    -1                         'clientData': new Uint8Array(assertion.response.clientDataJSON),
   46    -1                         'signature': new Uint8Array(assertion.response.signature),
   47    -1                     });
   48    -1                     form.submit();
   49    -1                 }).catch(alert);
   50    -1             });
   51    -1         }
   52    -1     };
   -1    40             navigator.credentials.get(options).then(assertion => {
   -1    41                 this.code.value = encode({
   -1    42                     'credentialId': new Uint8Array(assertion.rawId),
   -1    43                     'authenticatorData': new Uint8Array(assertion.response.authenticatorData),
   -1    44                     'clientData': new Uint8Array(assertion.response.clientDataJSON),
   -1    45                     'signature': new Uint8Array(assertion.response.signature),
   -1    46                 });
   -1    47                 form.submit();
   -1    48             }).catch(alert);
   -1    49         });
   -1    50     }
   -1    51 };
   53    52 
   54    -1     initCreate();
   55    -1     initAuth();
   56    -1 })();
   -1    53 initCreate();
   -1    54 initAuth();

diff --git a/mfa/templates/mfa/auth_FIDO2.html b/mfa/templates/mfa/auth_FIDO2.html

@@ -15,4 +15,4 @@
   15    15 </form>
   16    16 
   17    17 <script src="{% static 'cbor-js/cbor.js' %}"></script>
   18    -1 <script src="{% static 'mfa/fido2.js' %}"></script>
   -1    18 <script src="{% static 'mfa/fido2.js' %}" type="module"></script>

diff --git a/mfa/templates/mfa/create_FIDO2.html b/mfa/templates/mfa/create_FIDO2.html

@@ -16,4 +16,4 @@
   16    16 </form>
   17    17 
   18    18 <script src="{% static 'cbor-js/cbor.js' %}"></script>
   19    -1 <script src="{% static 'mfa/fido2.js' %}"></script>
   -1    19 <script src="{% static 'mfa/fido2.js' %}" type="module"></script>