implement api token auth

This commit is contained in:
simon 2022-01-11 14:15:36 +07:00
parent 917e73ec4d
commit 382e89abb7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
6 changed files with 29 additions and 10 deletions

View File

@ -1,5 +1,3 @@
"""api models""" """api models"""
from django.db import models # noqa: F401 # from django.db import models
# Create your models here.

View File

@ -6,28 +6,27 @@ from api.views import (
PlaylistApiView, PlaylistApiView,
VideoApiView, VideoApiView,
) )
from django.contrib.auth.decorators import login_required
from django.urls import path from django.urls import path
urlpatterns = [ urlpatterns = [
path( path(
"video/<slug:video_id>/", "video/<slug:video_id>/",
login_required(VideoApiView.as_view()), VideoApiView.as_view(),
name="api-video", name="api-video",
), ),
path( path(
"channel/<slug:channel_id>/", "channel/<slug:channel_id>/",
login_required(ChannelApiView.as_view()), ChannelApiView.as_view(),
name="api-channel", name="api-channel",
), ),
path( path(
"playlist/<slug:playlist_id>/", "playlist/<slug:playlist_id>/",
login_required(PlaylistApiView.as_view()), PlaylistApiView.as_view(),
name="api-playlist", name="api-playlist",
), ),
path( path(
"download/<slug:video_id>/", "download/<slug:video_id>/",
login_required(DownloadApiView.as_view()), DownloadApiView.as_view(),
name="api-download", name="api-download",
), ),
] ]

View File

@ -2,6 +2,11 @@
import requests import requests
from home.src.config import AppConfig from home.src.config import AppConfig
from rest_framework.authentication import (
SessionAuthentication,
TokenAuthentication,
)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
@ -9,6 +14,8 @@ from rest_framework.views import APIView
class ApiBaseView(APIView): class ApiBaseView(APIView):
"""base view to inherit from""" """base view to inherit from"""
authentication_classes = [SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]
search_base = False search_base = False
def __init__(self): def __init__(self):

View File

@ -45,6 +45,7 @@ INSTALLED_APPS = [
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django.contrib.humanize", "django.contrib.humanize",
"rest_framework", "rest_framework",
"rest_framework.authtoken",
"api", "api",
] ]

View File

@ -97,6 +97,10 @@
</div> </div>
<div class="settings-group"> <div class="settings-group">
<h2 id="integrations">Integrations</h2> <h2 id="integrations">Integrations</h2>
<div class="settings-item">
<p>API token:</p>
<p>{{ api_token }}</p>
</div>
<div class="settings-item"> <div class="settings-item">
<p>Integrate with <a href="https://returnyoutubedislike.com/">returnyoutubedislike.com</a> to get dislikes and average ratings back: <span class="settings-current">{{ config.downloads.integrate_ryd }}</span></p> <p>Integrate with <a href="https://returnyoutubedislike.com/">returnyoutubedislike.com</a> to get dislikes and average ratings back: <span class="settings-current">{{ config.downloads.integrate_ryd }}</span></p>
<i>Before activating that, make sure you have a scraping sleep interval of at least 3 secs set to avoid ratelimiting issues.</i><br> <i>Before activating that, make sure you have a scraping sleep interval of at least 3 secs set to avoid ratelimiting issues.</i><br>

View File

@ -31,6 +31,7 @@ from home.src.index import YoutubePlaylist
from home.src.index_management import get_available_backups from home.src.index_management import get_available_backups
from home.src.searching import Pagination, SearchHandler from home.src.searching import Pagination, SearchHandler
from home.tasks import extrac_dl, subscribe_to from home.tasks import extrac_dl, subscribe_to
from rest_framework.authtoken.models import Token
class ArchivistViewConfig(View): class ArchivistViewConfig(View):
@ -682,8 +683,7 @@ class SettingsView(View):
take post request from the form to update settings take post request from the form to update settings
""" """
@staticmethod def get(self, request):
def get(request):
"""read and display current settings""" """read and display current settings"""
config_handler = AppConfig(request.user.id) config_handler = AppConfig(request.user.id)
colors = config_handler.colors colors = config_handler.colors
@ -692,10 +692,12 @@ class SettingsView(View):
user_form = UserSettingsForm() user_form = UserSettingsForm()
app_form = ApplicationSettingsForm() app_form = ApplicationSettingsForm()
scheduler_form = SchedulerSettingsForm() scheduler_form = SchedulerSettingsForm()
token = self.get_token(request)
context = { context = {
"title": "Settings", "title": "Settings",
"config": config_handler.config, "config": config_handler.config,
"api_token": token,
"colors": colors, "colors": colors,
"available_backups": available_backups, "available_backups": available_backups,
"user_form": user_form, "user_form": user_form,
@ -705,6 +707,14 @@ class SettingsView(View):
return render(request, "home/settings.html", context) return render(request, "home/settings.html", context)
@staticmethod
def get_token(request):
"""get existing or create new token of user"""
# pylint: disable=no-member
token = Token.objects.get_or_create(user=request.user)[0]
print(token)
return token
@staticmethod @staticmethod
def post(request): def post(request):
"""handle form post to update settings""" """handle form post to update settings"""