mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 02:59:17 +02:00
Basic channels middleware for token auth
This commit is contained in:
parent
498aa1137b
commit
5c2ddc56c4
5 changed files with 138 additions and 0 deletions
47
api/funkwhale_api/common/auth.py
Normal file
47
api/funkwhale_api/common/auth.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
from urllib.parse import parse_qs
|
||||
|
||||
import jwt
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.utils.encoding import smart_text
|
||||
|
||||
from rest_framework import exceptions
|
||||
from rest_framework_jwt.settings import api_settings
|
||||
from rest_framework_jwt.authentication import BaseJSONWebTokenAuthentication
|
||||
|
||||
|
||||
|
||||
class TokenHeaderAuth(BaseJSONWebTokenAuthentication):
|
||||
def get_jwt_value(self, request):
|
||||
|
||||
try:
|
||||
qs = request.get('query_string', b'').decode('utf-8')
|
||||
parsed = parse_qs(qs)
|
||||
token = parsed['token'][0]
|
||||
except KeyError:
|
||||
raise exceptions.AuthenticationFailed('No token')
|
||||
|
||||
if not token:
|
||||
raise exceptions.AuthenticationFailed('Empty token')
|
||||
|
||||
return token
|
||||
|
||||
|
||||
class TokenAuthMiddleware:
|
||||
"""
|
||||
Custom middleware (insecure) that takes user IDs from the query string.
|
||||
"""
|
||||
|
||||
def __init__(self, inner):
|
||||
# Store the ASGI application we were passed
|
||||
self.inner = inner
|
||||
|
||||
def __call__(self, scope):
|
||||
auth = TokenHeaderAuth()
|
||||
try:
|
||||
user, token = auth.authenticate(scope)
|
||||
except exceptions.AuthenticationFailed:
|
||||
user = AnonymousUser()
|
||||
|
||||
scope['user'] = user
|
||||
return self.inner(scope)
|
Loading…
Add table
Add a link
Reference in a new issue