From a9aec5148399700208ad980e961aab5079384d8f Mon Sep 17 00:00:00 2001 From: Allen <63997543+aaw3@users.noreply.github.com> Date: Sat, 23 Sep 2023 14:09:54 -0500 Subject: [PATCH] Added backend django/auth0/mongodb-atlas code --- README.md | 2 +- backend/django/Dockerfile | 2 +- backend/django/requirements.txt | 5 +- backend/django/seatstock_django/db.sqlite3 | Bin 0 -> 131072 bytes .../seatstock_django/settings.py | 12 +++- .../seatstock_django/templates/index.html | 16 +++++ .../seatstock_django/seatstock_django/urls.py | 9 ++- .../seatstock_django/views.py | 52 ++++++++++++++ docker/docker-compose.yml | 4 +- docker/nginx-restart.sh | 3 + docker/nginx-shell.sh | 3 + docker/start.sh | 3 + docker/stop.sh | 3 + docker/volumes/nginx/config/default.conf | 64 ++++++++++++++++++ 14 files changed, 172 insertions(+), 6 deletions(-) create mode 100644 backend/django/seatstock_django/seatstock_django/templates/index.html create mode 100644 backend/django/seatstock_django/seatstock_django/views.py create mode 100755 docker/nginx-restart.sh create mode 100755 docker/nginx-shell.sh create mode 100755 docker/start.sh create mode 100755 docker/stop.sh create mode 100644 docker/volumes/nginx/config/default.conf diff --git a/README.md b/README.md index f8c7372..2118700 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # hackathon-SeatStock -Hackathon project to build and design a student ticket marketplace with safe transactions, +Hackathon project to build and design a proof-of-concept student ticket marketplace with safe transactions. diff --git a/backend/django/Dockerfile b/backend/django/Dockerfile index 6ac06d6..3f6c15f 100644 --- a/backend/django/Dockerfile +++ b/backend/django/Dockerfile @@ -25,4 +25,4 @@ RUN pip install -r requirements.txt # port where the Django app runs EXPOSE 8000 # start server -CMD python seatstock_django/manage.py runserver +CMD python seatstock_django/manage.py runserver 0.0.0.0:8000 diff --git a/backend/django/requirements.txt b/backend/django/requirements.txt index d3e4ba5..b7f72e8 100644 --- a/backend/django/requirements.txt +++ b/backend/django/requirements.txt @@ -1 +1,4 @@ -django +django ~= 4.0 +authlib ~= 1.0 +python-dotenv ~= 0.19 +requests ~= 2.27 diff --git a/backend/django/seatstock_django/db.sqlite3 b/backend/django/seatstock_django/db.sqlite3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c642d53e2eb700cbe77b430de00187dc28a475fc 100644 GIT binary patch literal 131072 zcmeI5TWlNIdB-{8kQ617M^}#;TNXvHcFoFK@lMg(w5#%@R9{JGc>&z$dk^FP0HnVIj*nAhICTxsjk?MAbvwWDdzgh!G*FGr&u zkLNV~&;OdA^YleF6ZBWIeLv{yY0rx#$;XY^|8g6p`{y8Y><0l5009sH0T2KI5C8!X z009sH0TB4f6PTS2lhdj3H$3bw*r!;TEk^z}@@tV-!aoXsA$%{K3B4P7C-n27$?eaqcbDw9vXJVBl=nx2yC-fmfI>*{W^asPq3t2b+vt}a&(`AjaIzQD-C>!yc< zp=GDp*xNmlj9ey@$zF_*lN*MUM8RI6s&>TsWY;oT0P`Z|n7T`@yc>QmN%rt}fDRsY3ddF>>*m=^|^dmjG04>{RM%eXr`?YtlJt?3aV& z!))tB#_prXrm@?0uSlvud@qcW2eYjcX}br#rd6tkb-7e}Z7p*tKu*lgn=m`C)z^;H zYm&KaGIh!C^DJFlw`1fKiEpiOw})5de1%h2yoo|#ExqaUc@)xAu2t&fBCQfnYMJH( z(au__O=DBtu4+4K=~kmsa&PQ>p^(iM)@5=ie zdM<`e2>G>qF0-~eLQJWIm=K1~Ao;Zf4U{}BvM((;(G5RRE}zNd!t4({es&|bdx-qLx-7m97^$e(^C1iR^Mrup(ha=@lTIW>5_(7l@Bsl3009sH0T2KI5C8!X009Sq*qqNJEv@+PR`h$SaZ__at4Db{YNVf6 z$R?ijm-VVnFDuxrx_f$dC%Sf}TeRlcXV=|h+C^u{?#!~EKhtk>MoH6Rd4?=j@;;%; zc1P7yX6%ddXU$iq$vF9v&yeRZHzf8Ipm#uc0Yj&wfT7i1AWrXpNXuCt7woX+qV1-q z=migH;RRVJ=eK=5$vhFImp7>9vLV&XH#7ejpO$$3;|smdi8^+ny9_*SP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nPN#_(~C}NCl8Tk zZ!kVDmJB}!OLUZo#Xt5-C&fcTA78cigq~EVqSLN@plAMA9iUOXt(V#yoN4&2Q{gRIx75`dUFd2%bb@nU<~gBR6BK{`=_fTgnZ3vIZa!gUKSJ;F#Xx=dq-Ru_ zuWs6}JPhaY&Ic8m)z+oX9qXJXq==0*wL2fmOKum7+3i%RC#XBnYz5xe-+K=>bUd7A zdbXX-Tm^9g*5SGXZ|y`tDN$u^9#$EHwyG7fx>iowp5t)V`=^dv4X4keRKO@hbH=?{ zddefXF72-#ReN9M)nrD?6?5s7b&(-_JF7EiRLA9d!_~ewe-1g?d#Namw?0o)^XZv@ z@+QreSx3dirGLwM;-rwzW((O&#_CC3ixUU6&_NEIdb@VKKZ$RmD`EZrusbZ|g8&GC z00@8p2!H?xfB*=900@ABoY0w4eaAOHd&00JNY0w4eaAn@!H!2SQT--f6G2!H?xfB*=900@8p2!H?xfB*;_ zMgaH!hY>RLk}~B+H)J zS^hHt{3i{uECH{5)sS!#p@}%`U1e22NQRc + + + Auth0 Example + + +{% if session %} +

Welcome {{session.userinfo.name}}!

+

Logout

+
{{pretty}}
+{% else %} +

Welcome Guest

+

Login

+{% endif %} + + diff --git a/backend/django/seatstock_django/seatstock_django/urls.py b/backend/django/seatstock_django/seatstock_django/urls.py index 3b3e8b1..348cd12 100644 --- a/backend/django/seatstock_django/seatstock_django/urls.py +++ b/backend/django/seatstock_django/seatstock_django/urls.py @@ -17,6 +17,13 @@ Including another URLconf from django.contrib import admin from django.urls import path +from . import views + urlpatterns = [ - path('admin/', admin.site.urls), + #path('admin/', admin.site.urls), + path("", views.index, name="index"), + path("login", views.login, name="login"), + path("logout", views.logout, name="logout"), + path("callback", views.callback, name="callback"), + ] diff --git a/backend/django/seatstock_django/seatstock_django/views.py b/backend/django/seatstock_django/seatstock_django/views.py new file mode 100644 index 0000000..f48cf77 --- /dev/null +++ b/backend/django/seatstock_django/seatstock_django/views.py @@ -0,0 +1,52 @@ +import json +from authlib.integrations.django_client import OAuth +from django.conf import settings +from django.shortcuts import redirect, render, redirect +from django.urls import reverse +from urllib.parse import quote_plus, urlencode + +oauth = OAuth() + +oauth.register( + "auth0", + client_id=settings.AUTH0_CLIENT_ID, + client_secret=settings.AUTH0_CLIENT_SECRET, + client_kwargs={ + "scope": "openid profile email", + }, + server_metadata_url=f"https://{settings.AUTH0_DOMAIN}/.well-known/openid-configuration", +) + +def login(request): + return oauth.auth0.authorize_redirect( + request, request.build_absolute_uri(reverse("callback")) + ) + +def callback(request): + token = oauth.auth0.authorize_access_token(request) + request.session["user"] = token + return redirect(request.build_absolute_uri(reverse("index"))) + +def logout(request): + request.session.clear() + + return redirect( + f"https://{settings.AUTH0_DOMAIN}/v2/logout?" + + urlencode( + { + "returnTo": request.build_absolute_uri(reverse("index")), + "client_id": settings.AUTH0_CLIENT_ID, + }, + quote_via=quote_plus, + ), + ) + +def index(request): + return render( + request, + "index.html", + context={ + "session": request.session.get("user"), + "pretty": json.dumps(request.session.get("user"), indent=4), + }, + ) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 7006974..e10c5bc 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,16 +2,18 @@ version: "3.3" services: nginx: + container_name: seatstock-nginx image: nginx:latest restart: unless-stopped ports: - "8080:80" volumes: - ../frontend:/usr/share/nginx/html - - ./volumes/config:/usr/share/nginx/html + - ./volumes/nginx/config/default.conf:/etc/nginx/conf.d/default.conf:ro docker-django: image: docker-django + container_name: seatstock-django restart: unless-stopped ports: - "8000:8000" diff --git a/docker/nginx-restart.sh b/docker/nginx-restart.sh new file mode 100755 index 0000000..f1bc55b --- /dev/null +++ b/docker/nginx-restart.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker exec -it seatstock-nginx /etc/init.d/nginx restart diff --git a/docker/nginx-shell.sh b/docker/nginx-shell.sh new file mode 100755 index 0000000..b05889c --- /dev/null +++ b/docker/nginx-shell.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker exec -it seatstock-nginx /bin/bash diff --git a/docker/start.sh b/docker/start.sh new file mode 100755 index 0000000..217a5fe --- /dev/null +++ b/docker/start.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker-compose up -d diff --git a/docker/stop.sh b/docker/stop.sh new file mode 100755 index 0000000..f163d8d --- /dev/null +++ b/docker/stop.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker-compose down diff --git a/docker/volumes/nginx/config/default.conf b/docker/volumes/nginx/config/default.conf new file mode 100644 index 0000000..452337c --- /dev/null +++ b/docker/volumes/nginx/config/default.conf @@ -0,0 +1,64 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + #access_log /var/log/nginx/host.access.log main; + + + location /ngxapi/transaction { + proxy_pass http://localhost:8000/transaction; + } + + location /ngxapi/search { + proxy_pass http://localhost:8000/search; + } + + location /ngxapi/query_games { + proxy_pass http://localhost:8000/query_games; + } + + location /ngxapi/account { + proxy_pass http://localhost:8000/account; + } + + + + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +}