diff --git a/example.env b/example.env new file mode 100644 index 0000000..c0e63b3 --- /dev/null +++ b/example.env @@ -0,0 +1,6 @@ +SPOTIFY_ENCODED_ID= +OIDC_RP_CLIENT_ID= +OIDC_RP_CLIENT_SECRET= +OIDC_OP_AUTHORIZATION_ENDPOINT=https://auth.yourwebsite.com/application/o/authorize/ +OIDC_OP_TOKEN_ENDPOINT=https://auth.yourwebsite.com/application/o/token/ +OIDC_OP_USER_ENDPOINT=https://auth.yourwebsite.com/application/o/userinfo/ diff --git a/muzak/settings.py b/muzak/settings.py index 582cd20..a975483 100644 --- a/muzak/settings.py +++ b/muzak/settings.py @@ -1,4 +1,8 @@ from pathlib import Path +import os +import dotenv + +dotenv.load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -15,6 +19,7 @@ INSTALLED_APPS = [ 'playlist.apps.PlaylistConfig', 'django.contrib.admin', 'django.contrib.auth', + 'mozilla_django_oidc', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', @@ -73,6 +78,18 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +AUTHENTICATION_BACKENDS = ( + 'mozilla_django_oidc.auth.OIDCAuthenticationBackend', + 'django.contrib.auth.backends.ModelBackend', +) + +OIDC_RP_CLIENT_ID = os.getenv('OIDC_RP_CLIENT_ID') +OIDC_RP_CLIENT_SECRET = os.getenv('OIDC_RP_CLIENT_SECRET') +OIDC_OP_AUTHORIZATION_ENDPOINT = os.getenv('OIDC_OP_AUTHORIZATION_ENDPOINT') +OIDC_OP_TOKEN_ENDPOINT = os.getenv('OIDC_OP_TOKEN_ENDPOINT') +OIDC_OP_USER_ENDPOINT = os.getenv('OIDC_OP_USER_ENDPOINT') +LOGIN_REDIRECT_URL = "/" +LOGOUT_REDIRECT_URL = "/" LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' diff --git a/muzak/urls.py b/muzak/urls.py index 8026509..925251a 100644 --- a/muzak/urls.py +++ b/muzak/urls.py @@ -19,5 +19,6 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('oidc/', include('mozilla_django_oidc.urls')), path('', include('playlist.urls')), ] diff --git a/playlist/templates/Login.html b/playlist/templates/Login.html new file mode 100644 index 0000000..67fdaf4 --- /dev/null +++ b/playlist/templates/Login.html @@ -0,0 +1,9 @@ +{% if user.is_authenticated %} +

Current user: {{ user.email }}

+
+ {% csrf_token %} + +
+{% else %} + Login +{% endif %} diff --git a/playlist/urls.py b/playlist/urls.py index 6286f5b..e618e4b 100644 --- a/playlist/urls.py +++ b/playlist/urls.py @@ -4,5 +4,6 @@ from . import views urlpatterns = [ path('track/', views.TrackDetailView.as_view()), path('track', views.NominateView.as_view()), - path('', views.TrackListView.as_view()), + #path('', views.TrackListView.as_view()), + path('', views.AuthView.as_view()), ] diff --git a/playlist/views.py b/playlist/views.py index 5629014..8bc1112 100644 --- a/playlist/views.py +++ b/playlist/views.py @@ -11,6 +11,10 @@ spt = spotify.Spotify() # TODO auth mixins +class AuthView(View): + def get(self, request): + return TemplateResponse(request, "Login.html", {}) + class TrackListView(View): def get(self, request): return HttpResponse("..") @@ -40,7 +44,6 @@ class NominateView(View): # Album artists album_artists = [] for artist in json["album"]["artists"]: - #album_artists.append(Artist.objects.from_json(artist)) album_artists.append(from_json(Artist, artist)) del json["album"]["artists"] @@ -53,12 +56,10 @@ class NominateView(View): # Track artists track_artists = [] for artist in json["artists"]: - #track_artists.append(Artist.objects.from_json(artist)) track_artists.append(from_json(Artist, artist)) del json["artists"] # Track - #track = Track.objects.from_json(json) track = from_json(Track, json) for artist in track_artists: track.artists.add(artist) diff --git a/requirements.txt b/requirements.txt index 0928ea4..32d503f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Django requests pillow python-dotenv +mozilla-django-oidc