我想驗證金字塔login
目的的加密密碼。因此,如果用戶和密碼匹配,則系統將授權用戶。目前,我發現編寫一個函數來比較密碼時,在數據庫中進行加密並且輸入到Pyramid的login
表單中的密碼未加密時很困難。現在,我沒有在登錄視圖中進行驗證。存儲和驗證加密的密碼登錄金字塔
我對使用安全措施/代碼的整個過程感到陌生,並且希望做到這一點。我在看這Auth tutorial,但User
類中的加密稍有不同,我使用Pyramid的Auth套件。任何指導如何成功和巧妙地做到這一點將不勝感激。
軟件:Python的2.7.9,1.5.7金字塔,SQLAlchemy的1.0.9
數據庫類:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(15), nullable=False, unique=True)
email = Column(String(300))
password = Column(String(300), nullable=False)
def __init__(self, username, password, email):
self.username = username
self.password = hashlib.sha224(password).hexdigest()
self.email = email
def __repr__(self):
return "<User(username ='%s', password='%s', email='%s')>" % (self.username, self.password, self.email)
意見
@view_config(route_name='login', renderer='templates/login.jinja2')
@forbidden_view_config(renderer='templates/login.jinja2')
def login(request):
login_url = request.route_url('login')
referrer = request.url
if referrer == login_url:
referrer = '/' # never use the login form itself as came_from
came_from = request.params.get('came_from', referrer)
message = ''
login = ''
password = ''
if 'form.submitted' in request.params:
login = request.params['login']
password = request.params['password']
user = api.retrieve_user(login) # need some way to validate password
if user is not None: # need to check user/password here, redirect if wrong
headers = remember(request, login)
return HTTPFound(location = came_from,
headers = headers)
message = 'Failed login'
return dict(
message = message,
url = request.application_url + '/login',
came_from = came_from,
login = login,
password = password,
)
請不要使用sha224存儲用戶密碼,請使用合適的庫,以便您安全地存儲密碼(suc h as [passlib](http://pythonhosted.org/passlib/))。 –