flask-security icon indicating copy to clipboard operation
flask-security copied to clipboard

login with numeric field

Open stamaimer opened this issue 9 years ago • 6 comments

I have read #124 #396 #464. And I use the method mentioned above to login user with phone number. After read the get_user method of SQLAlchemyUserDatastore. I found get_user try to use _is_numeric transform identifier(the value of email key in login form) to integer. If the identifier can transform to a integer, it will use as the primary key to get user from user table. I don't know why do this before get_identity_attributes. I found this will cause bug if someone want to use phone number as the user identity.

stamaimer avatar Mar 13 '17 13:03 stamaimer

A phone number would still be a string as it has numeric and has punctuation '555-555-5555' or something as such. if you are trying to login with JUST a numeric value then over class (super) the UserDatastore and replace the get_user method. Otherwise this is a a specific call to get the ID (Primary Key) of the database row and does a ...query.get(ID) call to the database (in SQLA at least). This is a fundamental call to retrieve one specific database row (this is important when doing a login). Your case is edge and prob best served by using an alternative UserDatastore or super classing the in-built datastore and replacing just that one method to suit your needs.

good luck :)

biomap avatar Mar 22 '17 07:03 biomap

@biomap Thanks for your reply. In China there is only digits in phone number. And i can't find where use the get_user by primary key in the project. And i also find find_user in UserStore or subclasses.

stamaimer avatar Mar 22 '17 07:03 stamaimer

    def _is_numeric(self, value):
        try:
            int(value)
        except (TypeError, ValueError):
            return False
        return True

This is bad from several points of view. It means that emails with leading digits will fail to log in. It may potentially lead to a security issue, if the app retrieves a different user than expected.

devrandom avatar Apr 19 '18 20:04 devrandom

isinstance(value, int) would be the right thing to do here

devrandom avatar Apr 19 '18 22:04 devrandom

alternatively, if you want to support strings, then match against /^\d+$/

devrandom avatar Apr 19 '18 22:04 devrandom

@devrandom can you please submit a PR? You can start from #633. Thanks!

jirikuncar avatar Oct 10 '18 19:10 jirikuncar