First working version of the shared web login.

This commit is contained in:
Griatch 2016-05-30 21:36:38 +02:00
parent 81170b69d0
commit a31441b3ce
9 changed files with 130 additions and 8 deletions

View file

@ -20,11 +20,16 @@ settings.INPUT_FUNC_MODULES.
"""
from future.utils import viewkeys
import importlib
from django.conf import settings
from evennia.commands.cmdhandler import cmdhandler
from evennia.players.models import PlayerDB
from evennia.utils.logger import log_err
from evennia.utils.utils import to_str, to_unicode
# django browser sessions
BrowserSessionStore = importlib.import_module(settings.SESSION_ENGINE).SessionStore
# always let "idle" work since we use this in the webclient
_IDLE_COMMAND = settings.IDLE_COMMAND
@ -35,6 +40,7 @@ _NA = lambda o: "N/A"
_ERROR_INPUT = "Inputfunc {name}({session}): Wrong/unrecognized input: {inp}"
# All global functions are inputfuncs available to process inputs
def text(session, *args, **kwargs):
@ -99,6 +105,35 @@ def default(session, cmdname, *args, **kwargs):
log_err(err)
def browser_sessid(session, *args, **kwargs):
"""
This is a utility function for the webclient (only) to communicate its
current browser session hash. This is important in order to link
the browser session to the evennia session. Only the very first
storage request will be accepted, the following ones will be ignored.
Args:
browserid (str): Browser session hash
"""
if not session.browserid:
print "stored browserid:", session, args[0]
session.browserid = args[0]
if not session.logged_in:
# automatic log in if the django browser session already authenticated.
browsersession = BrowserSessionStore(session_key=args[0])
uid = browsersession.get("logged_in", None)
if uid:
try:
player = PlayerDB.objects.get(pk=uid)
except Exception:
return
session.sessionhandler.login(session, player)
def client_options(session, *args, **kwargs):
"""
This allows the client an OOB way to inform us about its name and capabilities.

View file

@ -38,6 +38,7 @@ from evennia.utils.text2html import parse_html
_RE_SCREENREADER_REGEX = re.compile(r"%s" % settings.SCREENREADER_REGEX_STRIP, re.DOTALL + re.MULTILINE)
class WebSocketClient(Protocol, Session):
"""
Implements the server-side of the Websocket connection.

View file

@ -10,6 +10,7 @@ from builtins import object
import re
import weakref
import importlib
from time import time
from django.utils import timezone
from django.conf import settings
@ -19,6 +20,8 @@ from evennia.utils.utils import make_iter, lazy_property
from evennia.commands.cmdsethandler import CmdSetHandler
from evennia.server.session import Session
BrowserSessionStore = importlib.import_module(settings.SESSION_ENGINE).SessionStore
_GA = object.__getattribute__
_SA = object.__setattr__
_ObjectDB = None
@ -160,6 +163,7 @@ class ServerSession(Session):
"Initiate to avoid AttributeErrors down the line"
self.puppet = None
self.player = None
self.browserid = None
self.cmdset_storage_string = ""
self.cmdset = CmdSetHandler(self, True)
@ -220,6 +224,13 @@ class ServerSession(Session):
self.puppet = None
self.cmdset_storage = settings.CMDSET_SESSION
if self.browserid:
# this is only set by a webclient inputcommand.
bsession = BrowserSessionStore(session_key=self.browserid)
bsession["logged_in"] = player.id # this also saves the bsession
bsession.save()
print ("serversession.login:", bsession.session_key)
# Update account's last login time.
self.player.last_login = timezone.now()
self.player.save()

View file

@ -369,6 +369,10 @@ class ServerSessionHandler(SessionHandler):
"""
if session.logged_in:
# don't log in a session that is already logged in.
return
# we have to check this first before uid has been assigned
# this session.
@ -589,6 +593,17 @@ class ServerSessionHandler(SessionHandler):
return sessions[0] if len(sessions) == 1 else sessions
sessions_from_character = sessions_from_puppet
def sessions_from_browserid(self, browserid):
"""
Given a browserid, return all sessions having this id.
Args
browserid (str): The browserid hash
"""
return [session for session in self.values()
if session.browserid and session.browserid == browserid]
def announce_all(self, message):
"""
Send message to all connected sessions