【发布时间】:2015-06-03 18:37:45
【问题描述】:
我正在编写一个 Pyramid 应用程序,它允许在数据库表中注册任意数量的用户。这是我的登录代码:
@view_config(route_name='login', renderer='templates/login.jinja2')
def login(request):
username = request.params.get('username', '')
error = ''
if request.method == 'POST':
error = 'Login Failed'
authenticated = False
try:
authenticated = do_login(request)
except ValueError as e:
error = str(e)
if authenticated:
headers = remember(request, username)
return HTTPFound(request.route_url('home'), headers=headers)
return {'error': error, 'username': username}
在哪里
def do_login(request):
username = request.params.get('username', None)
password = request.params.get('password', None)
if not (username and password):
raise ValueError('both username and password required')
manager = BCRYPTPasswordManager()
cur = request.db.cursor()
try:
cur.execute("SELECT password FROM users WHERE username=%s", [username])
except psycopg2.Error:
raise ValueError("That username already exists!")
actual_password = cur.fetchall()[0][0] # Extrrrract the data
return manager.check(actual_password, password)
一旦给定用户通过身份验证,我想在所有视图上显示用户名。我的理解是身份验证信息存储在cookie中,并且该cookie看起来像(auth_tkt =“”)。如何从此 cookie 中获取“当前”用户名?
还是我比我意识到的更困惑?
【问题讨论】:
-
PS,我知道我会在 psycopg2.Error 的情况下引发错误的错误消息。这只是一个错字。
标签: python authentication cookies pyramid