From a medium article by the creator of Quart
: https://medium.com/@pgjones/websockets-in-quart-f2067788d1ee :
Authentication
The ability to control the acceptance is most useful when authorising requests, as it allows the request headers to be checked and either the upgrade accepted or a 401 returned. A simple example is,
def auth_required(func): @wraps(func) async def wrapper(*args, **kwargs): auth = websocket.authorization if ( auth is not None and auth.username == current.app.config['USERNAME'] and compare_digest( auth.password, current.app.config['PASSWORD'], ) ): return await func(*args, **kwargs) else: abort(401) return wrapper @app.websocket('/ws') @auth_required async def ws(): ...
But how does client-side javascript cause the websocket.authorization
attribute to be populated?
var ws = new WebSocket('ws://' + 'user' + ':' + 'pass' + '@' + document.domain + ':' + location.port + '/ws');
appears to have been deprecated in the last couple of years, and custom headers in the HTTP request for a websocket are not supported.