{% extends "layout.html" %}

{% block title %}{% trans %}Configure OAuth Client{% endtrans %}{% endblock %}

{% block content %}

    <h2>{% trans %}Choose a configuration method{% endtrans %}</h2>

    <p>
        {% trans display_name=g.server_config.display_name %}The {{ display_name }} configuration has been successfully loaded.
            Now register your client at the {{ display_name }}.
            Select one of the following options:{% endtrans %}
    </p>

    {% if g.server_config and g.server_config.specs and g.server_config.specs.oauth_2_dynamic_client_registration %}
        <article>
            <header>
                <h3>{% trans %}Dynamic registration{% endtrans %}</h3>
            </header>

            <p>{% trans %}This server supports{% endtrans %} <a href="https://datatracker.ietf.org/doc/html/rfc7591">{% trans %}dynamic client registration{% endtrans %}</a>. {% trans %}Provide an Initial Access Token if required, then click the button to automatically register this application.{% endtrans %}</p>

            <form method="POST" action="{{ url_for('oauth.client_dynamic_registration') }}">
                {{ dynamic_registration_form.hidden_tag() }}

                <label for="{{ dynamic_registration_form.initial_access_token.id }}">
                    {{ dynamic_registration_form.initial_access_token.label }}
                    {% if dynamic_registration_form.initial_access_token.description %}
                        <small>{{ dynamic_registration_form.initial_access_token.description }}</small>
                    {% endif %}
                </label>

                {% if dynamic_registration_form.initial_access_token.errors %}
                    {% for error in dynamic_registration_form.initial_access_token.errors %}
                        <small class="error">{{ error }}</small>
                    {% endfor %}
                {% endif %}

                <div role="group">
                    {{ dynamic_registration_form.initial_access_token(class="form-control") }}
                    {{ dynamic_registration_form.submit(class="primary") }}
                </div>
            </form>

            <details>
                <summary role="button" class="outline">{% trans %}What happens next?{% endtrans %}</summary>
                <ul>
                    <li>
                        {% trans display_name=g.server_config.display_name %}A new OAuth client will be created automatically at the {{ display_name }},
                            with the correct configuration,{% endtrans %}
                    </li>
                    <li>{% trans %}Client ID and Secret will be generated by the server,{% endtrans %}</li>
                    <li>{% trans %}Configuration will be stored in your session.{% endtrans %}</li>
                </ul>
            </details>
        </article>
    {% endif %}

    <article>
        <header>
            <h3>{% trans %}Manual configuration{% endtrans %}</h3>
        </header>

        <p>{% trans display_name=g.server_config.display_name %}Manually register the client at the {{ display_name }},
            then enter the client credentials below.{% endtrans %}</p>

        <details>
            <summary role="button" class="outline">{% trans display_name=g.server_config.display_name %}{{ display_name }} configuration{% endtrans %}</summary>
            <p>{% trans display_name=g.server_config.display_name %}When creating your OAuth client at the {{ display_name }}, configure these URIs:{% endtrans %}</p>
            <p><strong>{% trans %}Redirect URIs{% endtrans %} (<code>redirect_uris</code>):</strong></p>
            <ul>
                <li><code>{{ url_for('oauth.authorize_callback', _external=True) }}</code></li>
            </ul>
            <p><strong>{% trans %}Post-Logout Redirect URIs{% endtrans %} (<code>post_logout_redirect_uris</code>):</strong></p>
            <ul>
                <li><code>{{ url_for('oauth.logout_callback', _external=True) }}</code></li>
            </ul>
        </details>

        <form method="POST" action="{{ url_for('routes.configure_client') }}">
            {{ client_form.hidden_tag() }}

            <label for="{{ client_form.client_id.id }}">
                {{ client_form.client_id.label }}
                {{ client_form.client_id(class="form-control") }}
                {% if client_form.client_id.description %}
                    <small>{{ client_form.client_id.description }}</small>
                {% endif %}
            </label>

            {% if client_form.client_id.errors %}
                {% for error in client_form.client_id.errors %}
                    <small class="error">{{ error }}</small>
                {% endfor %}
            {% endif %}

            <label for="{{ client_form.client_secret.id }}">
                {{ client_form.client_secret.label }}
                {{ client_form.client_secret(class="form-control") }}
                {% if client_form.client_secret.description %}
                    <small>{{ client_form.client_secret.description }}</small>
                {% endif %}
            </label>

            {% if client_form.client_secret.errors %}
                {% for error in client_form.client_secret.errors %}
                    <small class="error">{{ error }}</small>
                {% endfor %}
            {% endif %}

            {{ client_form.submit() }}
        </form>
    </article>

{% endblock %}
