So my journey with flask tryton is not over.
I’m developing a custom Flask API that integrates with Tryton for authentication. However, I’m running into an issue where I’m unable to retrieve the password hash for the admin user. Although I know that the admin’s password hash is correctly stored in the database, my API always returns None
for the password hash field.
Here’s a brief summary of what I’ve done so far:
- I updated the transaction context to include sensitive fields using:
Transaction().context.update({'with_password': True, 'with_password_hash': True})
- I performed a search for the admin user and then used
User.read()
to fetch the fields. - I also tried reading all fields (using an empty field list) to inspect what is being returned.
Despite these efforts, the password hash remains None
in my API output.
Below is a simplified version of my login API code:
@app.route("/database/login", methods=['POST'])
@tryton.transaction()
def login_api():
try:
data = request.get_json()
username = data.get('username')
password = data.get('password')
User = Pool().get('res.user')
Transaction().context.update({'with_password': True, 'with_password_hash': True})
user_ids = User.search([('login', '=', username)], offset=0, limit=1, order=())
if not user_ids:
return jsonify({"error": "User not found"}), 404
user_data = User.read(user_ids, [])
print("User Data:", user_data)
pwd_hash = user_data[0].get('password_hash') or user_data[0].get('password')
if not pwd_hash:
return jsonify({"error": "User has no password set"}), 500
# ... (password verification and session creation) ...
except Exception as e:
return jsonify({"error": str(e)}), 500
My question is: How can I return the password hash for the user? Is there a specific context flag or a different approach to ensure that sensitive fields like password_hash
(or password
) are included when reading user records?