From 493268c62b8f57e5702577ae073e1ae245160eda Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 6 Jun 2022 01:15:59 +0530 Subject: [PATCH] feat: def model for auth grant storage --- Makefile | 2 +- dashboard/settings.py | 2 + oauth/__init__.py | 0 oauth/admin.py | 20 +++++++ oauth/apps.py | 6 +++ oauth/migrations/0001_initial.py | 54 +++++++++++++++++++ ...0002_alter_authorizationgrant_code_text.py | 27 ++++++++++ ...0003_alter_authorizationgrant_code_text.py | 27 ++++++++++ ...0004_alter_authorizationgrant_code_text.py | 27 ++++++++++ ...0005_alter_authorizationgrant_code_text.py | 27 ++++++++++ oauth/migrations/__init__.py | 0 oauth/models.py | 37 +++++++++++++ oauth/tests.py | 3 ++ oauth/urls.py | 24 +++++++++ oauth/views.py | 32 +++++++++++ 15 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 oauth/__init__.py create mode 100644 oauth/admin.py create mode 100644 oauth/apps.py create mode 100644 oauth/migrations/0001_initial.py create mode 100644 oauth/migrations/0002_alter_authorizationgrant_code_text.py create mode 100644 oauth/migrations/0003_alter_authorizationgrant_code_text.py create mode 100644 oauth/migrations/0004_alter_authorizationgrant_code_text.py create mode 100644 oauth/migrations/0005_alter_authorizationgrant_code_text.py create mode 100644 oauth/migrations/__init__.py create mode 100644 oauth/models.py create mode 100644 oauth/tests.py create mode 100644 oauth/urls.py create mode 100644 oauth/views.py diff --git a/Makefile b/Makefile index b692c4e..44464d2 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ help: ## Prints help for targets with comments lint: ## Run linter @./venv/bin/black ./dashboard/* - @./venv/bin/black ./users/* + @./venv/bin/black ./oauth/* migrate: ## Run migrations $(call run_migrations) diff --git a/dashboard/settings.py b/dashboard/settings.py index e04e64e..f1462a4 100644 --- a/dashboard/settings.py +++ b/dashboard/settings.py @@ -41,6 +41,8 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "oauth", + "oauth.integrations", ] MIDDLEWARE = [ diff --git a/oauth/__init__.py b/oauth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oauth/admin.py b/oauth/admin.py new file mode 100644 index 0000000..1657a7d --- /dev/null +++ b/oauth/admin.py @@ -0,0 +1,20 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.contrib import admin + +from .models import AuthorizationGrant + +admin.site.register(AuthorizationGrant) diff --git a/oauth/apps.py b/oauth/apps.py new file mode 100644 index 0000000..701cb69 --- /dev/null +++ b/oauth/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class OauthConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "oauth" diff --git a/oauth/migrations/0001_initial.py b/oauth/migrations/0001_initial.py new file mode 100644 index 0000000..f603c88 --- /dev/null +++ b/oauth/migrations/0001_initial.py @@ -0,0 +1,54 @@ +# Generated by Django 4.0.3 on 2022-06-05 19:37 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("integrations", "0001_initial"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="AuthorizationGrant", + fields=[ + ( + "code_text", + models.CharField( + blank=True, + default="GGw2HiQ1PaR9qXDkShgLHzx1zoi50tZD", + editable=False, + max_length=32, + primary_key=True, + serialize=False, + unique=True, + verbose_name="Authorization Code", + ), + ), + ( + "issued_date", + models.DateTimeField(auto_now_add=True, verbose_name="date issued"), + ), + ( + "authorized_by", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ( + "issued_to", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="integrations.oauthintegration", + ), + ), + ], + ), + ] diff --git a/oauth/migrations/0002_alter_authorizationgrant_code_text.py b/oauth/migrations/0002_alter_authorizationgrant_code_text.py new file mode 100644 index 0000000..43da132 --- /dev/null +++ b/oauth/migrations/0002_alter_authorizationgrant_code_text.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-06-05 19:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("oauth", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="authorizationgrant", + name="code_text", + field=models.CharField( + blank=True, + default="3lxQluG1v0yp73bNKYey0TEoDW3eXQxH", + editable=False, + max_length=32, + primary_key=True, + serialize=False, + unique=True, + verbose_name="Authorization Code", + ), + ), + ] diff --git a/oauth/migrations/0003_alter_authorizationgrant_code_text.py b/oauth/migrations/0003_alter_authorizationgrant_code_text.py new file mode 100644 index 0000000..43e2855 --- /dev/null +++ b/oauth/migrations/0003_alter_authorizationgrant_code_text.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-06-05 19:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("oauth", "0002_alter_authorizationgrant_code_text"), + ] + + operations = [ + migrations.AlterField( + model_name="authorizationgrant", + name="code_text", + field=models.CharField( + blank=True, + default="6Ciye3K1OWfDDy7BJJ50S5NE1Rev5fKA", + editable=False, + max_length=32, + primary_key=True, + serialize=False, + unique=True, + verbose_name="Authorization Code", + ), + ), + ] diff --git a/oauth/migrations/0004_alter_authorizationgrant_code_text.py b/oauth/migrations/0004_alter_authorizationgrant_code_text.py new file mode 100644 index 0000000..73e3b2a --- /dev/null +++ b/oauth/migrations/0004_alter_authorizationgrant_code_text.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-06-05 19:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("oauth", "0003_alter_authorizationgrant_code_text"), + ] + + operations = [ + migrations.AlterField( + model_name="authorizationgrant", + name="code_text", + field=models.CharField( + blank=True, + default="RfvqA3tKdTDqwaSyicR3vQaUygseBanY", + editable=False, + max_length=32, + primary_key=True, + serialize=False, + unique=True, + verbose_name="Authorization Code", + ), + ), + ] diff --git a/oauth/migrations/0005_alter_authorizationgrant_code_text.py b/oauth/migrations/0005_alter_authorizationgrant_code_text.py new file mode 100644 index 0000000..515bf37 --- /dev/null +++ b/oauth/migrations/0005_alter_authorizationgrant_code_text.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-06-05 19:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("oauth", "0004_alter_authorizationgrant_code_text"), + ] + + operations = [ + migrations.AlterField( + model_name="authorizationgrant", + name="code_text", + field=models.CharField( + blank=True, + default="V1BWmLAqQfhYW9xmCWVRucWQNYE8Lnfp", + editable=False, + max_length=32, + primary_key=True, + serialize=False, + unique=True, + verbose_name="Authorization Code", + ), + ), + ] diff --git a/oauth/migrations/__init__.py b/oauth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oauth/models.py b/oauth/models.py new file mode 100644 index 0000000..2c219fe --- /dev/null +++ b/oauth/models.py @@ -0,0 +1,37 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from django.contrib.auth.models import User +from django.db import models +from django.utils.crypto import get_random_string + +from .integrations.models import OauthIntegration + + +class AuthorizationGrant(models.Model): + authorized_by = models.ForeignKey(User, on_delete=models.CASCADE) + code_text = models.CharField( + "Authorization Code", + primary_key=True, + unique=True, + max_length=32, + default=get_random_string(32), + blank=True, + editable=False, + ) + issued_date = models.DateTimeField("date issued", auto_now_add=True, blank=True) + issued_to = models.ForeignKey(OauthIntegration, on_delete=models.CASCADE) + + def __str__(self): + return f"{self.authorized_by.username}: {self.issued_to.name_text} {self.issued_to.client_id_uuid}" diff --git a/oauth/tests.py b/oauth/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/oauth/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/oauth/urls.py b/oauth/urls.py new file mode 100644 index 0000000..d5fa070 --- /dev/null +++ b/oauth/urls.py @@ -0,0 +1,24 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from django.urls import path, include + +from . import views + +urlpatterns = [ + path("apps/", include("oauth.integrations.urls")), + path("", views.index, name="index"), +] diff --git a/oauth/views.py b/oauth/views.py new file mode 100644 index 0000000..c402ac0 --- /dev/null +++ b/oauth/views.py @@ -0,0 +1,32 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from django.shortcuts import render +from django.http import HttpResponse + +from django.contrib.auth import authenticate + +# Create your views here. +def index(request): + return HttpResponse("Foo bar") + + +def create_app(request): + return HttpResponse("create app") + + +def delete_app(request): + return HttpResponse("delete app")