Authenticate django user in crossbar

#1

Hi, any tips as i can do this?
I tried migrate from cyclone-sse to crossbar, before i can do something like this:
from django.conf import settings
from django.utils.importlib import import_module
django_session_engine = import_module(settings.SESSION_ENGINE)
def get_uid_from_sid(session_key):
“”"
Return user id uid based on django session id session_key
“”"
session = django_session_engine.SessionStore(session_key)
return session.get(’_auth_user_id’, None)
class BroadHandler(BroadcastHandler, RedisMixin):

@defer.inlineCallbacks
def authorize(self):
sid = self.get_cookie(‘sessionid’)
if not sid:
defer.returnValue(False)
self.client_uid = yield deferToThread(get_uid_from_sid, sid)

0 Likes

#2

Hi Sergey!

I’ve forwarded your question to one of our devs with a background which includes Django. I’ll try to get back to you in the coming days.

Regards,

Alex

···

Am Sonntag, 30. August 2015 13:27:41 UTC+2 schrieb Sergey Maranchuk:

Hi, any tips as i can do this?
I tried migrate from cyclone-sse to crossbar, before i can do something like this:
from django.conf import settings
from django.utils.importlib import import_module
django_session_engine = import_module(settings.SESSION_ENGINE)
def get_uid_from_sid(session_key):
“”"
Return user id uid based on django session id session_key
“”"
session = django_session_engine.SessionStore(session_key)
return session.get(’_auth_user_id’, None)
class BroadHandler(BroadcastHandler, RedisMixin):

@defer.inlineCallbacks
def authorize(self):
sid = self.get_cookie(‘sessionid’)
if not sid:
defer.returnValue(False)
self.client_uid = yield deferToThread(get_uid_from_sid, sid)

0 Likes

#3

i already have some workeround for this)

authorizer:
import sys
import os
from os.path import dirname

from twisted.internet import defer
from twisted.internet.threads import deferToThread

from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.exception import ApplicationError
sys.path.append(dirname(dirname(file)))

os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “app.settings.dev”)

from django.conf import settings
from django.utils.importlib import import_module

django_session_engine = import_module(settings.SESSION_ENGINE)

def get_uid_from_sid(session_key):
“”"
Return user id uid based on django session id session_key
“”"
session = django_session_engine.SessionStore(session_key)
return session.get(’_auth_user_id’, None)

class DjangoAuthenticator(ApplicationSession):

@defer.inlineCallbacks
def onJoin(self, details):
    @defer.inlineCallbacks
    def authenticate(realm, authid, ticket):
        # `authid` - user id
        # `ticket` - session id
        uid = yield deferToThread(get_uid_from_sid, ticket)
        if uid is not None and uid == authid:
            defer.returnValue({"role": "user"})
        else:
            raise ApplicationError("com.app.invalid_ticket",
            "could not authenticate session - invalid ticket '{}' for authid {}".format(ticket, authid))
    try:
        yield self.register(authenticate, 'com.steamcups.authenticate')
        print("Django authenticator registered")
    except Exception as e:
        print("could not register Django authericator: {0}".format(e))

``

in front:
$wampProvider.init({
url: ‘{{ settings.WS_BACKEND }}’,
realm: ‘realm1’,
{% if user.is_authenticated %}
authmethods: [“ticket”],
authid: “{{ user.id }}”,
ticket: “{{ request.session.session_key }}”
{% else %}
authmethods: [“anonymous”]
{% endif %}
});

``

PS: i use standalone django and session id as ticket for auth.

···

четверг, 10 сентября 2015 г., 13:27:29 UTC+3 пользователь Alexander Gödde написал:

Hi Sergey!

I’ve forwarded your question to one of our devs with a background which includes Django. I’ll try to get back to you in the coming days.

Regards,

Alex

Am Sonntag, 30. August 2015 13:27:41 UTC+2 schrieb Sergey Maranchuk:

Hi, any tips as i can do this?
I tried migrate from cyclone-sse to crossbar, before i can do something like this:
from django.conf import settings
from django.utils.importlib import import_module
django_session_engine = import_module(settings.SESSION_ENGINE)
def get_uid_from_sid(session_key):
“”"
Return user id uid based on django session id session_key
“”"
session = django_session_engine.SessionStore(session_key)
return session.get(’_auth_user_id’, None)
class BroadHandler(BroadcastHandler, RedisMixin):

@defer.inlineCallbacks
def authorize(self):
sid = self.get_cookie(‘sessionid’)
if not sid:
defer.returnValue(False)
self.client_uid = yield deferToThread(get_uid_from_sid, sid)

0 Likes