pennersr / django-allauth

Integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication.
https://allauth.org
MIT License
9.41k stars 3.01k forks source link

Move JS inline snippets into JS files. #4063

Closed d9pouces closed 5 days ago

d9pouces commented 3 weeks ago

In some template, you can find inline JS snippets. However, such inline scripts can be forbidden by strong CSP. Could we imagine to export these snippets to conventional .JS static files?

templates/mfa/webauthn/snippets/login_script.html:

    allauth.webauthn.forms.loginForm({
        ids: {
            login: "passkey_login",
            credential: "mfa_credential",
        }
    })

templates/mfa/webauthn/reauthenticate.html:

       allauth.webauthn.forms.authenticateForm({
        ids: {
        authenticate: "mfa_webauthn_reauthenticate",
        credential: "{{ form.credential.auto_id }}"
        },
        data: JSON.parse(document.getElementById('js_data').textContent)
        })

templates/mfa/webauthn/add_form.html:

        allauth.webauthn.forms.addForm({
            ids: {
                add: "mfa_webauthn_add",
                passwordless: "{{ form.passwordless.auto_id }}",
                credential: "{{ form.credential.auto_id }}"
            },
            data: JSON.parse(document.getElementById('js_data').textContent)
        })

templates/mfa/authenticate.html:

        allauth.webauthn.forms.authenticateForm({
        ids: {
        authenticate: "mfa_webauthn_authenticate",
        credential: "{{ webauthn_form.credential.auto_id }}"
        },
        data: JSON.parse(document.getElementById('js_data').textContent)
        })

templates/account/email.html:

(function() {
  var message = "{% trans 'Do you really want to remove the selected email address?' %}";
  var actions = document.getElementsByName('action_remove');
  if (actions.length) {
    actions[0].addEventListener("click", function(e) {
      if (! confirm(message)) {
        e.preventDefault();
      }
    });
  }
})();
pennersr commented 3 weeks ago

Yes, that's possible, though it will need the use of json_script to hand over data to the static scripts.

pennersr commented 5 days ago

Moved to https://codeberg.org/allauth/django-allauth/issues/4063