[英]PyMongo how to access hashed password without username
我正在關注( https://github.com/ravi-sharma/flask-mongo/blob/master/app.py )這是我制作的登錄頁面的示例,但我卡住了。 這是他使用的代碼,我無法修改以適應我試圖做的事情:
@app.route('/signin', methods=['GET', 'POST'])
def signin():
if request.method == 'POST':
users = mongo.db.users
signin_user = users.find_one({'username': request.form['username']})
if signin_user:
if bcrypt.hashpw(request.form['password'].encode('utf-8'), signin_user['password'].encode('utf-8')) == \
signin_user['password'].encode('utf-8'):
session['username'] = request.form['username']
return redirect(url_for('index'))
flash('Username and password combination is wrong')
return render_template('signin.html')
return render_template('signin.html')
在該版本的代碼中,他使用用戶名從 mongodb 訪問數據,然后將請求表單密碼與數據庫中的密碼進行比較。 我試圖完成的是類似的事情,但使用令牌,而不是用戶/通行證。 我遇到的問題是,如果沒有用戶名,我如何訪問令牌?,以及如何將密碼請求表與集合中的文檔進行比較。 繼承人什么我試過:
def account_login():
try:
if request.method == 'POST':
tokens_collection = mongo.db.user_tokens
user_login = tokens.find_one({'tokens': request.form['token']})
if user_login:
if bcrypt.hashpw(request.form['token'].encode('utf-8'), user_login['token']) == user_login['token']:
session['token'] = request.form['token']
return redirect(url_for("account"))
flash('Token was incorrect, something went wrong')
return redirect(url_for("account"))
return redirect(url_for("account"))
except Exception as e:
print(f"account login error: {e}")
它不起作用的原因是因為用戶輸入的值和數據庫中的值不同,idk 如何比較它們以從 if user_login 中獲得 True 值。 如果這需要更多說明,請讓我知道,提前致謝!
我最終只是沒有散列令牌,現在工作正常。 我認為問題是由於某種原因,散列密碼作為二進制文件上傳到 mongodb,所以我無法將它與令牌的散列版本匹配。 我不知道如何將它從二進制轉換為哈希。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.