Bottle: accessing the same session on different Canister devices

0

Hello, I'm developing a project for the study character and I'm having a big problem.

I'm using:

  • astroid == 2.0.4
  • bottle == 0.12.13
  • canister == 1.5.1
  • colorama == 0.4.0
  • cymysql == 0.9.12
  • Cython == 0.29
  • isort == 4.3.4
  • lazy-object-proxy == 1.3.1
  • mccabe == 0.6.1
  • PyJWT == 1.6.4
  • pylint == 2.1.1
  • six == 1.11.0
  • wrapt == 1.10.11

As you can see I use canister to create and maintain my sessions, the problem I face is the following, when entering login and password, perform confirmation of existence and confirmation of data in the database and start a session, if I access the my using my computer's ip: 8080 it does not take me to the home screen, it does not take me to the login screen, it takes me to my dashboard, as if I had already logged into that device, I thought maybe it could be a problem with my network or something, this way I hosted in pythonanywhere and continued with the same problem.

from bottle import Bottle, TEMPLATE_PATH
from Data.SQL import MySQL
import os
import canister
from canister import session

# Diretorio base.
base_path = os.getcwd().replace('\', '/')
# Instancia da aplicacao.
app = Bottle()
# Path de configuracao.
app.config.load_config('{}/config/lobster.config'.format(base_path))
# Instalacao do plugin de sessoes.
app.install(canister.Canister())

# Instancia do banco de dados.
banco_mysql = MySQL()

from app.Controllers import *

When the query is performed and the data is valid, the following methods are executed by creating both sessions and cookies

def definir_cookie_login(self, usuario_id, nome, email, na, unidade):
    response.set_cookie('usuario_id', str(usuario_id))
    response.set_cookie('nome_usuario', str(nome))
    response.set_cookie('email', str(email))
    response.set_cookie('na', str(na))
    response.set_cookie('unidade', str(unidade))

def iniciar_sessao(self, usuario_id, nome, email, na, unidade):
    session.data['usuario_id'] = str(usuario_id)
    session.data['nome_usuario'] = str(nome)
    session.data['email'] = str(email)
    session.data['na'] = str(na)
    session.data['unidade'] = str(unidade)

Does anyone have any ideas? how can I make each access attempt on a different device or even on a different browser to create a new session instead of accessing the one that is already being used.

    
asked by anonymous 10.12.2018 / 03:07

1 answer

0

I realized that when I created session.data it would only create a session.data for all sessions, as I needed a parse form to differentiate the sessions I decided to create the session.data keys using something that differs each session and even though I allowed the session to persist, so I used session.sid concatenated with the keys I was creating, I do not know if this is the most pythonic way (probably not) or the most correct (most likely not), but it was the way I was able to solve my problem and it's working now, I deployed it through pythonanywhere.com . I tested on several devices and so far I have not had problems with performance or duplicate sessions, at first I thought about using the client ip, however request.environ.get('REMOTE_ADDR') gives me the local ip of the client or it would be possible to have two people access using the ip 192.168.0.x or 192.168.15.x etc ...

The method that initialized my session was as follows:

def iniciar_sessao(self, usuario_id, nome, email, na, unidade):
    session.user = request.environ.get('REMOTE_ADDR')
    session.data[session.sid+'usuario_id'] = str(usuario_id)
    session.data[session.sid+'nome_usuario'] = str(nome)
    session.data[session.sid+'email'] = str(email)
    session.data[session.sid+'na'] = str(na)
    session.data[session.sid+'unidade'] = str(unidade)

And to end the sessions:

def encerrar_sessao(self):
    del session.data[session.sid+'usuario_id']
    del session.data[session.sid+'nome_usuario']
    del session.data[session.sid+'email']
    del session.data[session.sid+'na']
    del session.data[session.sid+'unidade']
    session.user = None

If someone knows in some way more practical (or more pythonica) can share with me, I would like to improve my codes.

If the form is correct, I hope it helps someone else.

    
12.12.2018 / 14:48