From 033507176f65cc572655d209d16053f3f2d64a1c Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 11 Mar 2025 16:34:09 +0100
Subject: [PATCH] =?UTF-8?q?feat(api):=20=E2=9C=A8=20add=20OAuth2=20support?=
 =?UTF-8?q?=20for=20DRF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add OAuth2 support for DRF. Prettify default OAuth2 templates with crispy forms.
---
 scipost_django/SciPost_v1/settings/base.py    |  3 +-
 .../oauth2_provider/application_detail.html   | 90 +++++++++++++++++++
 .../oauth2_provider/application_form.html     | 38 ++++++++
 .../templates/oauth2_provider/base.html       | 12 +++
 4 files changed, 142 insertions(+), 1 deletion(-)
 create mode 100644 scipost_django/templates/oauth2_provider/application_detail.html
 create mode 100644 scipost_django/templates/oauth2_provider/application_form.html

diff --git a/scipost_django/SciPost_v1/settings/base.py b/scipost_django/SciPost_v1/settings/base.py
index 9f8c1459f..95bd8d12a 100644
--- a/scipost_django/SciPost_v1/settings/base.py
+++ b/scipost_django/SciPost_v1/settings/base.py
@@ -178,7 +178,8 @@ OAUTH2_PROVIDER = {
 
 REST_FRAMEWORK = {
     "DEFAULT_AUTHENTICATION_CLASSES": [
-        "rest_framework.authentication.SessionAuthentication"
+        "rest_framework.authentication.SessionAuthentication",
+        "oauth2_provider.contrib.rest_framework.OAuth2Authentication",
     ],
     "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAdminUser",),
     "DEFAULT_FILTER_BACKENDS": [
diff --git a/scipost_django/templates/oauth2_provider/application_detail.html b/scipost_django/templates/oauth2_provider/application_detail.html
new file mode 100644
index 000000000..e193c3ff7
--- /dev/null
+++ b/scipost_django/templates/oauth2_provider/application_detail.html
@@ -0,0 +1,90 @@
+{% extends "oauth2_provider/base.html" %}
+
+{% load i18n %}
+
+{% block breadcrumb_items %}
+  {{ block.super }}
+  <span class="breadcrumb-item"><a href="{% url 'oauth2_provider:list' %}">Applications</a></span>
+
+  {% if object %}
+    <a href="#"
+       class="breadcrumb-item active">{{ object }}</a>
+  {% endif %}
+
+{% endblock %}
+
+
+{% block content %}
+  <div class="block-center">
+    <h3 class="block-center-heading">{{ application.name }}</h3>
+
+    <dl>
+
+      <dt>{% trans "Client id" %}</dt>
+      <dd>
+        <input class="form-control"
+               type="text"
+               value="{{ application.client_id }}"
+               readonly />
+      </dd>
+
+
+      <dt>{% trans "Client secret" %}</dt>
+      <dd>
+
+        {% if application.hash_client_secret %}
+          Client secret is hashed. If you don't remember it, you will need to generate a new application.
+        {% else %}
+          <input class="form-control"
+                 type="text"
+                 value="{{ application.client_secret }}"
+                 readonly />
+        {% endif %}
+
+      </dd>
+
+
+      <dt>{% trans "Client type" %}</dt>
+      <dd>
+        {{ application.client_type }}
+      </dd>
+
+
+      <dt>{% trans "Authorization Grant Type" %}</dt>
+      <dd>
+        {{ application.authorization_grant_type }}
+      </dd>
+
+
+      <dt>{% trans "Redirect Uris" %}</dt>
+      <dd>
+        <textarea class="form-control"
+                  readonly>{{ application.redirect_uris }}</textarea>
+      </dd>
+
+
+      <dt>{% trans "Post Logout Redirect Uris" %}</dt>
+      <dd>
+        <textarea class="form-control"
+                  readonly>{{ application.post_logout_redirect_uris }}</textarea>
+      </dd>
+
+
+      <dt>{% trans "Allowed Origins" %}</dt>
+      <dd>
+        <textarea class="form-control"
+                  readonly>{{ application.allowed_origins }}</textarea>
+      </dd>
+
+    </ul>
+
+    <div class="btn-toolbar">
+      <a class="btn"
+         href="{% url "oauth2_provider:list" %}">{% trans "Go Back" %}</a>
+      <a class="btn btn-primary"
+         href="{% url "oauth2_provider:update" application.pk %}">{% trans "Edit" %}</a>
+      <a class="btn btn-danger"
+         href="{% url "oauth2_provider:delete" application.pk %}">{% trans "Delete" %}</a>
+    </div>
+  </div>
+{% endblock content %}
diff --git a/scipost_django/templates/oauth2_provider/application_form.html b/scipost_django/templates/oauth2_provider/application_form.html
new file mode 100644
index 000000000..d87653db9
--- /dev/null
+++ b/scipost_django/templates/oauth2_provider/application_form.html
@@ -0,0 +1,38 @@
+{% extends "oauth2_provider/base.html" %}
+
+{% load crispy_forms_tags %}
+
+{% load i18n %}
+
+{% block breadcrumb_items %}
+  {{ block.super }}
+  <span class="breadcrumb-item"><a href="{% url 'oauth2_provider:list' %}">Applications</a></span>
+
+  {% if object %}
+    <span class="breadcrumb-item">{{ object }}</span>
+  {% else %}
+    <span class="breadcrumb-item">Register</span>
+  {% endif %}
+
+{% endblock %}
+
+
+{% block content %}
+  <form id="application-form" method="post" action=" 
+    {% block app-form-action-url %}{% url 'oauth2_provider:update' application.pk %}{% endblock app-form-action-url %}
+     ">
+    {% csrf_token %}
+    {% crispy form %}
+
+
+    <div class="d-flex gap-2">
+      <a class="btn" href=" 
+        {% block app-form-back-url %}{% url "oauth2_provider:detail" application.pk %}{% endblock app-form-back-url %}
+       ">{% trans "Go Back" %}</a>
+      <button type="submit"
+              form="application-form"
+              class="btn btn-primary">{% trans "Save" %}</button>
+    </div>
+
+  </form>
+{% endblock %}
diff --git a/scipost_django/templates/oauth2_provider/base.html b/scipost_django/templates/oauth2_provider/base.html
index 2b14ed4c1..81670c086 100644
--- a/scipost_django/templates/oauth2_provider/base.html
+++ b/scipost_django/templates/oauth2_provider/base.html
@@ -1 +1,13 @@
 {% extends 'scipost/base.html' %}
+
+{% block breadcrumb %}
+  <div class="breadcrumb-container">
+    <div class="container">
+      <nav class="breadcrumb hidden-sm-down">
+
+        {% block breadcrumb_items %}<span class="breadcrumb-item">OAuth</span>{% endblock %}
+
+      </nav>
+    </div>
+  </div>
+{% endblock %}
-- 
GitLab