{% extends 'users/_base_otp.html' %}
{% load static %}
{% load i18n %}

{% block small_title %}
    {% trans 'Authenticate' %}
{% endblock %}

{% block content %}
    <form id="verify-form" class="" role="form" method="post" action="">
        {% csrf_token %}
        <div class="form-input">
            <input id="password" type="password" class="" placeholder="{% trans 'Password' %}" required="" autofocus="autofocus">
            <input id="password-hidden" type="text" style="display:none" name="{{ form.password.html_name }}">
        </div>
        <button type="submit" class="next" onclick="doVerify();return false;">{% trans 'Next' %}</button>
        {% if 'password' in form.errors %}
            <p class="red-fonts">{{ form.password.errors.as_text }}</p>
        {% endif %}
    </form>
    <script type="text/javascript" src="/static/js/plugins/jsencrypt/jsencrypt.min.js"></script>
    <script>
        function encryptLoginPassword(password, rsaPublicKey) {
            var jsencrypt = new JSEncrypt(); //加密对象
            jsencrypt.setPublicKey(rsaPublicKey); // 设置密钥
            return jsencrypt.encrypt(password); //加密
        }
        function doVerify() {
            //公钥加密
            var rsaPublicKey = "{{ rsa_public_key }}"
            var password = $('#password').val(); //明文密码
            var passwordEncrypted = encryptLoginPassword(password, rsaPublicKey)
            $('#password-hidden').val(passwordEncrypted); //返回给密码输入input
            $('#verify-form').submit();//post提交
        }

        $(document).ready(function () {
        })
    </script>
{% endblock %}
