tendo a usare il mio utente e sessione manangement
Per i miei gestori di web io collegare un decoratore chiamato session
e uno chiamato authorize
. Il decoratore session
allegherà una sessione a ogni richiesta e il decoratore authorize
si assicurerà che l'utente sia autorizzato.
(Una parola di cautela, decoratore autorizzare è specifico per come ho sviluppare le mie applicazioni - il nome utente è il primo parametro nella maggior parte delle richieste).
Così, per esempio un gestore di rete può essere simile:
class UserProfile(webapp.RequestHandler):
@session
@authorize
def get(self, user):
# Do some funky stuff
# The session is attached to the self object.
someObjectAttachedToSession = self.SessionObj.SomeStuff
self.response.out.write("hello %s" % user)
Nel codice precedente, il decoratore session
attribuisce alcune cose sessione che ho bisogno in base ai cookie che sono presenti in merito alla richiesta. L'intestazione authorize
si accerterà che l'utente possa accedere alla pagina solo se la sessione è quella corretta.
Il codice decoratori sono al di sotto:
import functools
from model import Session
import logging
def authorize(redirectTo = "/"):
def factory(method):
'Ensures that when an auth cookie is presented to the request that is is valid'
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
#Get the session parameters
auth_id = self.request.cookies.get('auth_id', '')
session_id = self.request.cookies.get('session_id', '')
#Check the db for the session
session = Session.GetSession(session_id, auth_id)
if session is None:
self.redirect(redirectTo)
return
else:
if session.settings is None:
self.redirect(redirectTo)
return
username = session.settings.key().name()
if len(args) > 0:
if username != args[0]:
# The user is allowed to view this page.
self.redirect(redirectTo)
return
result = method(self, *args, **kwargs)
return result
return wrapper
return factory
def session(method):
'Ensures that the sessions object (if it exists) is attached to the request.'
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
#Get the session parameters
auth_id = self.request.cookies.get('auth_id', '')
session_id = self.request.cookies.get('session_id', '')
#Check the db for the session
session = Session.GetSession(session_id, auth_id)
if session is None:
session = Session()
session.session_id = Session.MakeId()
session.auth_token = Session.MakeId()
session.put()
# Attach the session to the method
self.SessionObj = session
#Call the handler.
result = method(self, *args, **kwargs)
self.response.headers.add_header('Set-Cookie', 'auth_id=%s; path=/; HttpOnly' % str(session.auth_token))
self.response.headers.add_header('Set-Cookie', 'session_id=%s; path=/; HttpOnly' % str(session.session_id))
return result
return wrapper
def redirect(method, redirect = "/user/"):
'When a known user is logged in redirect them to their home page'
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
try:
if self.SessionObj is not None:
if self.SessionObj.settings is not None:
# Check that the session is correct
username = self.SessionObj.settings.key().name()
self.redirect(redirect + username)
return
except:
pass
return method(self, *args, **kwargs)
return wrapper
Anche se ho usato Django-on-AppEngine, non vorrei sostenere ignorando appengines built autenticazione su Django. Qualche ragione che fai? (Sì, uso Django anche al di fuori di Appengine) – ironfroggy
"Builtin auth" di GAE supporta solo account Google, in particolare non supporta la "registrazione utente", che la domanda richiede in modo specifico (è anche per questo che non mi sono nemmeno preoccupato di suggerire OpenID questa volta - le ultime volte che ho fatto gli askers l'ho visto apparentemente come un attacco al loro desiderio di gestire la propria registrazione utente! -). –
Gli utenti possono registrarsi contro l'app in particolare la prima volta che effettuano l'accesso con le proprie credenziali Google (o OpenId). –