javascript - twisted websocket chatserver openid authentication -
i have python chatserver uses twisted , autobahn websockets connection.
factory = messageserverfactory("ws://localhost:9000", debug=debug, debugcodepaths=debug) factory.protocol = messageserverprotocol factory.setprotocoloptions(allowhixie76=true) listenws(factory)
this server
import logging autobahn.websocket import websocketserverfactory, websocketserverprotocol databaseconnector import dbconnector loginmanager import loginmanager messagetypes import messageparser class messageserverprotocol(websocketserverprotocol): def onopen(self): self.factory.register(self) def onmessage(self, msg, binary): if not binary: self.factory.processmessage(self, msg) def connectionlost(self, reason): websocketserverprotocol.connectionlost(self, reason) self.factory.unregister(self) class messageserverfactory(websocketserverfactory): logging.basicconfig(filename='log/dastan.log',format='%(levelname)s:%(message)s',level=logging.warning) def __init__(self, url, debug=false, debugcodepaths=false): websocketserverfactory.__init__(self, url, debug=debug, debugcodepaths=debugcodepaths) self.clients = {} self.connector = dbconnector() self.messages = messageparser() self.manager = loginmanager() def register(self, client): print "%s connected" % client.peerstr def unregister(self, client): if self.clients.has_key(client): self.processlogout(client) print "%s disconnected" % client.peerstr def processmessage(self, client, msg): try: msg = self.messages.parsemessage(msg) action = msg['type'] except valueerror, e: logging.warning("[parse]:%s", e.message) client.sendmessage(self.messages.createerrormessage("could not parse message")) return if action == "chatmessage": self.processchatmessage(client, msg) # elif action == "login": # self.processlogin(client, msg) # elif action == "logout": # self.processlogout(client) elif action == "openid": self.manager.processlogin(client,msg) def processchatmessage(self, client, msg): if not self.clients.has_key(client): client.sendmessage(self.messages.createerrormessage('not authorized')) return if not msg['message']: client.sendmessage(self.messages.createerrormessage('invalid message')) return if not msg['recipient']: client.sendmessage(self.messages.createerrormessage('invalid recipient')) return if msg['recipient'] in self.clients.values(): c in self.clients: if self.clients[msg['recipient']]: c.sendmessage(self.messages.chatmessage(msg['sender'], msg['message'])) print "sent message %s %s: '%s' .." % (msg['sender'], msg['recipient'], msg['message']) else: client.sendmessage(self.messages.createerrormessage('user not registered')) def checksender(self, user, client): if user in self.clients.values() , self.clients[client] == user: return else: self.clients[client] = user
an independent html/js client can connect , send chat messages. want implement open id authentication (performed server), before opening websocket.
this onload function:
var wsuri = "ws://192.168.0.12:9000"; if ("websocket" in window) { sock = new websocket(wsuri); } else if ("mozwebsocket" in window) { sock = new mozwebsocket(wsuri); } else { log("browser not support websocket!"); window.location = "http://autobahn.ws/unsupportedbrowser"; } if (sock) { sock.onopen = function () { log("connected " + wsuri); } sock.onclose = function (e) { log("connection closed (wasclean = " + e.wasclean + ", code = " + e.code + ", reason = '" + e.reason + "')"); sock = null; } sock.onmessage = function (e) { receive(e.data); } }
as i'm new python/twisted don't know how , examples show websocket chatroom without authentification.
how can implement openid properly? requires redirection, break ws connection.
you cannot open ws
before redirection. open after , code should work.
good luck.
by way, google on it's homepage.
Comments
Post a Comment