My Angular 2 app(用打字稿编码)有一个简单的身份验证方案:
abc123...
Authorization
标题现在我想添加websockets。我想知道如何在那里验证用户。由于我无法控制将哪些标头发送到websocket服务器(WS),因此我无法发送JWT。
到目前为止我的想法(尚未实施):
let sock = new WebSocket('wss://example.com/channel/');
open
事件。套接字打开后:
type='auth'
payload='JWT_VALUE'
auth
。收到后,服务器会读取有效负载,验证JWT_VALUE
并设置isAuthenticated
标志
isAuthenticated
的客户端发送任何其他类型的消息,服务器将断开套接字2个问题:服务器资源可以由连接但从不发送JWT的客户端占用,如果客户端未经过身份验证,则更干净的解决方案会阻止握手。
其他想法:
new WebSocket('wss://example.com/channel/<JWT>/')
如何在websockets上验证客户端?假设用户已经通过HTTP登录并且Angular 2应用程序具有JWT令牌。
答案 0 :(得分:10)
我选择了以下协议:
1。客户端登录网站并接收和身份验证令牌(JSON Web令牌)
GET /auth
{
user: 'maggie',
pwd: 'secret'
}
// response
{ token: '4ad42f...' }
2. 经过身份验证的客户端请求websocket连接票
GET /ws_ticket
Authorization: Bearer 4ad42f...
// response: single-use ticket (will only pass validation once)
{ ticket: 'd76a55...', expires: 1475406042 }
3. 客户端打开websocket,在查询参数中发送票证
var socket = new WebSocket('wss://example.com/channel/?ticket=d76a55...');
4. Websocket服务器(PHP)然后在接受握手之前验证票证
/**
* Receives the URL used to connect to websocket. Return true to admit user,
* false to reject the connection
*/
function acceptConnection($url){
$params = parse_str(parse_url($url, PHP_URL_QUERY));
return validateTicket($params['ticket']);
}
/** Returns true if ticket is valid, never-used, and not expired. */
function validateTicket($ticket){/*...*/}
答案 1 :(得分:0)
客户端打开websocket,在查询参数中发送用户名和密码
ws://<username>:<password>@<ip-address><path>
示例:new $ WebSocket('ws:// user:123456@127.0.0.0/util')
答案 2 :(得分:0)
使用djangorestframework-jwt生成您的JWT和以下Django-Channels 2中间件。
可以通过djangorestframework-jwt http API设置令牌,并且如果定义了JWT_AUTH_COOKIE
,也将为WebSocket连接发送令牌。
settings.py
JWT_AUTH = {
'JWT_AUTH_COOKIE': 'JWT', # the cookie will also be sent on WebSocket connections
}
routing.py:
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
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))