diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..9c50102 --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import {} }: + pkgs.mkShell { + # nativeBuildInputs is usually what you want -- tools you need to run + nativeBuildInputs = with pkgs; [ + buildPackages.python311Packages.django + buildPackages.python311Packages.requests + ]; +} \ No newline at end of file diff --git a/tfbbridge/.gitignore b/tfbbridge/.gitignore new file mode 100644 index 0000000..2b76056 --- /dev/null +++ b/tfbbridge/.gitignore @@ -0,0 +1,2 @@ +db.sqlite3 +manage.py \ No newline at end of file diff --git a/tfbbridge/bridge/.gitignore b/tfbbridge/bridge/.gitignore new file mode 100644 index 0000000..e0d1ea3 --- /dev/null +++ b/tfbbridge/bridge/.gitignore @@ -0,0 +1 @@ +migrations \ No newline at end of file diff --git a/tfbbridge/bridge/__init__.py b/tfbbridge/bridge/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tfbbridge/bridge/admin.py b/tfbbridge/bridge/admin.py new file mode 100644 index 0000000..4575273 --- /dev/null +++ b/tfbbridge/bridge/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +from .models import Organization, Group + +@admin.register(Organization) +class OrganizationAdmin(admin.ModelAdmin): + exclude = ('_sender_url', '_receiver_url') + readonly_fields = ('add_sending_group', 'add_receiving_group', ) + +admin.site.register(Group) \ No newline at end of file diff --git a/tfbbridge/bridge/apps.py b/tfbbridge/bridge/apps.py new file mode 100644 index 0000000..6c14bd1 --- /dev/null +++ b/tfbbridge/bridge/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + +class BridgeConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'bridge' diff --git a/tfbbridge/bridge/models.py b/tfbbridge/bridge/models.py new file mode 100644 index 0000000..4346fae --- /dev/null +++ b/tfbbridge/bridge/models.py @@ -0,0 +1,36 @@ +from django.db import models +import uuid + +def get_uuid(): + return uuid.uuid4().hex + +class Organization(models.Model): + name = models.CharField( + max_length=256, + ) + _sender_url = models.CharField( + max_length=256, + default = get_uuid + ) + _receiver_url = models.CharField( + max_length=256, + default = get_uuid + ) + + @property + def add_sending_group(self): + return "https://tfb.beepboop.systems/groupme/add/send/{}".format( + self._sender_url + ) + @property + def add_receiving_group(self): + return "https://tfb.beepboop.systems/groupme/add/recv/{}".format( + self._receiver_url + ) + +class Group(models.Model): + name = models.CharField(max_length=256) + group_id = models.CharField(max_length=256) + bot_id = models.CharField(max_length=256) + can_send_notices = models.BooleanField() + belongs_to = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True) \ No newline at end of file diff --git a/tfbbridge/bridge/tests.py b/tfbbridge/bridge/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/tfbbridge/bridge/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/tfbbridge/bridge/urls.py b/tfbbridge/bridge/urls.py new file mode 100644 index 0000000..a9641c2 --- /dev/null +++ b/tfbbridge/bridge/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("", views.index, name="index"), + path("oauth_callback", views.handle_oauth, name="handle_oauth"), + path("add/send/", views.add_sending_group, name="add_sending_group"), +] \ No newline at end of file diff --git a/tfbbridge/bridge/views.py b/tfbbridge/bridge/views.py new file mode 100644 index 0000000..0cbb795 --- /dev/null +++ b/tfbbridge/bridge/views.py @@ -0,0 +1,35 @@ +from django.http import HttpResponse, HttpResponseBadRequest +from django.shortcuts import redirect +from .models import Organization +import requests + +auth_link = "https://oauth.groupme.com/oauth/authorize?client_id=qAAAc8GSUlTA8C3Ypo9CMiFQwQCQnU8zPU5KGEtz3FYHDqP5" + +def index(request): + return HttpResponse("Hello, world. You're at the polls index.") + +def handle_send_requests(token, org_id): + # firstly, check what organization the org_id belongs to + matching = Organization.objects.filter(_sender_url = org_id) + if matching == []: + return HttpResponseBadRequest # this is a fake org_id + +def handle_oauth(request): + token = request.GET.get('access_token') + try: + request_type = request.session["type"] + organization_id = request.session["id"] + except KeyError: + return HttpResponseBadRequest + + if request_type == "send": + return handle_send_requests(token, organization_id) + + return HttpResponse(token) + +def add_sending_group(request, url_id): + # first part -- authenticate with the GroupMe api + request.session['type'] = "send" + request.session['id'] = url_id + + return redirect(auth_link) \ No newline at end of file diff --git a/tfbbridge/tfb/__init__.py b/tfbbridge/tfb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tfbbridge/tfb/asgi.py b/tfbbridge/tfb/asgi.py new file mode 100644 index 0000000..8cde725 --- /dev/null +++ b/tfbbridge/tfb/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for tfb project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tfb.settings') + +application = get_asgi_application() diff --git a/tfbbridge/tfb/settings.py b/tfbbridge/tfb/settings.py new file mode 100644 index 0000000..f2869a9 --- /dev/null +++ b/tfbbridge/tfb/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for tfb project. + +Generated by 'django-admin startproject' using Django 4.2.16. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-%4qs)a3%&$ykwuu5d$0%^=8u1)b=t=t+vgo!rxvrq1z9+7)w1z' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ["10.100.0.2"] +CSRF_TRUSTED_ORIGINS = ["https://tfb.beepboop.systems"] + + +# Application definition + +INSTALLED_APPS = [ + "bridge.apps.BridgeConfig", + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'tfb.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'tfb.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'groupme/static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/tfbbridge/tfb/urls.py b/tfbbridge/tfb/urls.py new file mode 100644 index 0000000..5862d57 --- /dev/null +++ b/tfbbridge/tfb/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import include, path + +urlpatterns = [ + path('groupme/', include('bridge.urls')), + path('groupme/admin/', admin.site.urls), +] diff --git a/tfbbridge/tfb/wsgi.py b/tfbbridge/tfb/wsgi.py new file mode 100644 index 0000000..6f9ab89 --- /dev/null +++ b/tfbbridge/tfb/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for tfb project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tfb.settings') + +application = get_wsgi_application()