Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand it, what you want is this:</p> <ol> <li>Encrypt the user's password when creating the account. Use your salt and algorithm</li> <li>When the user logs in, hash the incoming password the same way you did when you stored it</li> <li>Compare the two hashes using regular string comparison in your db request</li> </ol> <p>So, something like this for the login code:</p> <pre><code>from passlib.hash import sha256_crypt passHash = sha256_crypt.encrypt(typed_password) // call your sqlalchemy code to query the db with this value (below) // In your SQLAlchemy code assuming "users" is your users table // and "password" is your password field s = users.select(and_(users.username == typed_username, users.password == passHash)) rs = s.execute() </code></pre> <p>rs would be the resultset of matching users (should be zero or one of course).</p> <p>Disclaimer - I did not test any of this</p> <p><strong>Edit:</strong> Thank you for pointing out that PassLib uses a different salt each time it's run. Your best bet in that case, since there doesn't seem to be a straightforward way to do it with sqlalchemy, is the below:</p> <pre><code>s=users.select(users.username == typed_username) rs = s.execute() userRow = rs.fetchone() if (sha256_crypt.verify(userRow.password)): # you have a match </code></pre> <p>Also, to address your request for abstracting: a common methodology for handling this operation is to create a "security" utility class for getting the user (object) that matches the passed login credentials.</p> <p>The problem with your current setup is that the User constructor has two different operational goals that, though related, are not necessarily the same thing: authenticating a user and getting a User object (for, say, a list of users in a group). The constructor becomes needlessly complex in that case. It's better to put that logic where it can be encapsulated with other security or login-related functionality such as logging in a user via session ID or SSO token instead of username/password:</p> <pre><code>security.loginUser(username, password) # or security.loginUser(single_sign_on_token), etc. for polymorphic Security loggedInUser = security.getLoggedInUser() ... later ... otherUser = User(username) #single job, simple, clean </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload