首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在django通道上使用令牌身份验证来验证websocket?

如何在django通道上使用令牌身份验证来验证websocket?
EN

Stack Overflow用户
提问于 2017-04-13 12:52:46
回答 7查看 30.2K关注 0票数 67

我们希望我们的websockets使用django-通道,但是我们也需要进行身份验证。我们有一个与django- rest框架一起运行的rest,在那里我们使用令牌对用户进行身份验证,但是似乎没有将相同的功能内置到django-通道中。

EN

Stack Overflow用户

发布于 2019-06-02 21:34:06

下面的Django-Channel 2中间件对djangorestframework-jwt生成的JWT进行身份验证。

令牌可以通过djangorestframework-jwt进行设置,如果JWT_AUTH_COOKIE WebSocket 被定义为,则还会发送给 connections

settings.py

代码语言:javascript
复制
JWT_AUTH = {
    'JWT_AUTH_COOKIE': 'JWT',     # the cookie will also be sent on WebSocket connections
}

routing.py:

代码语言:javascript
复制
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from json_token_auth import JsonTokenAuthMiddlewareStack
from yourapp.consumers import SocketCostumer

application = ProtocolTypeRouter({
    "websocket": JsonTokenAuthMiddlewareStack(
        URLRouter([
            path("socket/", SocketCostumer),
        ]),
    ),

})

json_token_auth.py

代码语言:javascript
复制
from http import cookies

from channels.auth import AuthMiddlewareStack
from django.contrib.auth.models import AnonymousUser
from django.db import close_old_connections
from rest_framework_jwt.authentication import BaseJSONWebTokenAuthentication


class JsonWebTokenAuthenticationFromScope(BaseJSONWebTokenAuthentication):
    """
    Extracts the JWT from a channel scope (instead of an http request)
    """

    def get_jwt_value(self, scope):
        try:
            cookie = next(x for x in scope['headers'] if x[0].decode('utf-8') == 'cookie')[1].decode('utf-8')
            return cookies.SimpleCookie(cookie)['JWT'].value
        except:
            return None


class JsonTokenAuthMiddleware(BaseJSONWebTokenAuthentication):
    """
    Token authorization middleware for Django Channels 2
    """

    def __init__(self, inner):
        self.inner = inner

    def __call__(self, scope):

        try:
            # Close old database connections to prevent usage of timed out connections
            close_old_connections()

            user, jwt_value = JsonWebTokenAuthenticationFromScope().authenticate(scope)
            scope['user'] = user
        except:
            scope['user'] = AnonymousUser()

        return self.inner(scope)


def JsonTokenAuthMiddlewareStack(inner):
    return JsonTokenAuthMiddleware(AuthMiddlewareStack(inner))
票数 11
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43392889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档