{% extends 'users/base.html' %}
{% from '_switch.html' import switch %}
{% from 'message_box.html' import message_box %}
{% from 'api/_messages.html' import get_enable_persistent_msg, get_disable_persistent_msg %}

{% block user_content %}
    {% if can_create_personal_tokens %}
        {% call message_box('warning') %}
            {%- set link_start -%}
                <a href="{{ url_for('oauth.user_tokens') }}">
            {%- endset -%}
            {%- set link_end -%}
                </a>
            {%- endset -%}
            <p>
                <strong>
                    {% trans -%}
                        API keys are deprecated and may be removed in a future Indico version.
                    {%- endtrans %}
                </strong>
            </p>
            <p>
                {% trans -%}
                    Please create {{ link_start }}API tokens{{ link_end }} instead.
                    They use a more standard way of authenticating (Bearer token authentication
                    instead of an HMAC signature) and you can (and should!) create one token for
                    each script/application where you need to access the Indico API. To reduce
                    the risk from a leaked token they also let you specify more fine-grained
                    scopes.
                {%- endtrans %}
            </p>
        {% endcall %}
    {% endif %}

    <div class="i-box-group vert">
        <div class="i-box">
            <div class="i-box-header">
                <div class="i-box-title">
                    {%- trans %}API Key{% endtrans -%}
                </div>
                {% if key and can_modify %}
                    <div class="i-box-buttons toolbar">
                        <div class="group">
                            <button type="button"
                                    class="ui orange small button"
                                    data-href="{{ url_for('.key_create') }}"
                                    data-method="post"
                                    data-params='{"force": "1"}'
                                    data-title="{% trans %}Create new API key{% endtrans %}"
                                    data-confirm="{% trans %}Do you really want to create a new API key?
                                    Your old key will stop working immediately!{% endtrans %}">
                                <i aria-hidden="true" class="icon plus"></i>
                                {%- trans %}Create new API key{% endtrans -%}
                            </button>
                            <button type="button"
                                    class="ui negative small button"
                                    data-href="{{ url_for('.key_delete') }}"
                                    data-method="post"
                                    data-title="{% trans %}Delete API key{% endtrans %}"
                                    data-confirm="{% trans %}Do you really want to delete your API key?{% endtrans %}">
                                <i aria-hidden="true" class="icon trash alternate"></i>
                                {%- trans %}Delete API key{% endtrans -%}
                            </button>
                        </div>
                    </div>
                {% endif %}
            </div>
            <div class="i-box-content">
                {% if not key %}
                    <p>
                        {%- trans -%}
                            You do not have an API key at the moment. If you would like to use our API,
                            please create an API key using the button below.
                        {%- endtrans -%}
                    </p>

                    <button type="button"
                            class="ui primary button"
                            data-href="{{ url_for('.key_create') }}"
                            data-method="post">
                        {%- trans %}Create API key{% endtrans -%}
                    </button>
                {% else %}
                    <dl class="i-data-list">
                        <dt>{% trans %}Token{% endtrans %}</dt>
                        <dd>
                            {{ key.token }}
                            {% if key.is_blocked %}
                                <i class="icon-warning warningText"
                                   title="{% trans %}Your API key has been blocked.
                                   Please contact an administrator for further details.{% endtrans %}">
                                </i>
                            {% endif %}
                        </dd>
                        {% if use_signatures %}
                            <dt>{% trans %}Secret{% endtrans %}</dt>
                            <dd>{{ key.secret }}</dd>
                        {% endif %}
                        <dt>{% trans %}Created{% endtrans %}</dt>
                        <dd>{{ key.created_dt | format_datetime('short') }}</dd>
                        <dt>{% trans %}Total uses{% endtrans %}</dt>
                        <dd>{{ key.use_count }}</dd>
                        {% if key.last_used_dt %}
                            <dt>{% trans %}Last used{% endtrans %}</dt>
                            <dd>{{ key.last_used_dt | format_datetime('short') }}</dd>
                        {% endif %}
                        {% if key.last_used_ip %}
                            <dt>{% trans %}Last IP{% endtrans %}</dt>
                            <dd>{{ key.last_used_ip }}</dd>
                        {% endif %}
                        {% if key.last_used_uri %}
                            <dt>{% trans %}Last request{% endtrans %}</dt>
                            <dd>
                                {{ key.last_used_uri }}
                                {% if key.last_used_auth %}
                                    <i class="icon-shield"
                                       title="{% trans %}Last request could access private data.{% endtrans %}">
                                    </i>
                                {% else %}
                                    <i class="icon-earth"
                                       title="{% trans %}Last request could only access public data.{% endtrans %}">
                                    </i>
                                {% endif %}
                            </dd>
                        {% endif %}
                        {% if allow_persistent and use_signatures %}
                            <dt>{% trans %}Persistent signatures{% endtrans %}</dt>
                            <dd>
                                {{ switch(id='toggle-persistent-signatures', checked=key.is_persistent_allowed,
                                          on_label=_('On'), off_label=_('Off'),
                                          disabled=not can_modify,
                                          data_href=url_for('.key_toggle_persistent'),
                                          data_confirm_enable=get_enable_persistent_msg(),
                                          data_confirm_disable=get_disable_persistent_msg()) }}
                            </dd>
                        {% endif %}
                    </dl>
                {% endif %}
            </div>
        </div>

        <div class="i-box">
            <div class="i-box-header">
                <div class="i-box-title">
                    {%- trans %}Token-based links{% endtrans -%}
                </div>
                <div class="i-box-buttons toolbar">
                    <div class="group">
                        <button type="button"
                                class="ui negative small button"
                                data-href="{{ url_for('core.reset_signature_tokens') }}"
                                data-method="POST"
                                data-confirm="{% trans %}Are you sure you'd like to invalidate all your
                                token-based links?{% endtrans %}">
                            <i aria-hidden="true" class="icon trash alternate"></i>
                            {%- trans %}Invalidate all links{% endtrans -%}
                        </button>
                    </div>
                </div>
            </div>
            <div class="i-box-content">
                {% trans %}
                    Token-based unique links are secret URLs which Indico uses to allow you to easily fetch data
                    from within other applications (e.g. calendar schedulers through iCalendar). They are generated
                    through a digital signature based on a secret key that is unique per Indico user. If one of your
                    links is compromised, you should click this button. All other links will be invalidated.
                {% endtrans %}
            </div>
        </div>

        {% if session.user.is_admin and (key or old_keys) %}
            <div class="i-box">
                <div class="i-box-header">
                    <div class="i-box-title">
                        {%- trans %}Administration{% endtrans -%}
                    </div>
                    {% if key %}
                        <div class="i-box-buttons toolbar">
                            <div class="group">
                                {% if key.is_blocked %}
                                    <button type="button"
                                            class="ui positive small button"
                                            data-href="{{ url_for('.key_block') }}"
                                            data-method="post">
                                        {%- trans %}Unblock API key{% endtrans -%}
                                    </button>
                                {% else %}
                                    <button type="button"
                                            class="ui negative small button"
                                            data-href="{{ url_for('.key_block') }}"
                                            data-method="post">
                                        <i aria-hidden="true" class="icon ban"></i>
                                        {%- trans %}Block API key{% endtrans -%}
                                    </button>
                                {% endif %}
                            </div>
                        </div>
                    {% endif %}
                </div>
                <div class="i-box-content">
                    <p>{% trans %}This is the list of old keys associated with this user.{% endtrans %}</p>
                    {% if old_keys %}
                        <ul class="group-list">
                            {% for old_key in old_keys -%}
                                <li>
                                    <code>{{ old_key.token }}</code>
                                    <span class="right">
                                        {%- if not old_key.use_count -%}
                                            {%- trans %}never used{% endtrans -%}
                                        {%- else -%}
                                            {%- trans n = old_key.use_count -%}
                                                used once
                                            {%- pluralize -%}
                                                used <strong>{{ n }} times</strong>
                                            {%- endtrans -%}
                                            {%- if old_key.last_used_dt -%}
                                                , {% trans date=old_key.last_used_dt|format_datetime('short') -%}
                                                    last used on {{ date }}
                                                {%- endtrans %}
                                            {%- endif -%}
                                        {%- endif -%}
                                    </span>
                                </li>
                            {% endfor %}
                        </ul>
                    {% else %}
                        <p>{% trans %}There are no old keys.{% endtrans %}</p>
                    {% endif %}
                </div>
            </div>
        {% endif %}
    </div>

    <script>
        $('#toggle-persistent-signatures').ajaxCheckbox();
    </script>
{% endblock %}
