radha
radha

Reputation: 47

regular expression for password in web2py

I am using the auth module for the login process for my web2py application.I want to validate the password using regular expressions but couldn't find a way for it.How could we modify the access control to accept regex

Upvotes: 0

Views: 210

Answers (1)

Anthony
Anthony

Reputation: 25536

In a model file (likely in db.py), you should have the following line, which defines all the Auth tables, including db.auth_user:

auth.define_tables()

When the db.auth_user table is defined, the requires attribute of its password field is set to be a list containing a single CRYPT validator. If you would like to apply a regular expression validation, you can do so by inserting an IS_MATCH validator before the CRYPT validator in the requires attribute. To do this, anywhere after the above line, include the following:

password_is_match = IS_MATCH(r'your_regex', error_message='Your error message',
                             search=True)
db.auth_user.password.requires.insert(0, password_is_match)

Note, if you do not set search=True, the validator will prepend your regex with a "^".

Also, note that the reason for inserting the IS_MATCH validator before the CRYPT validator is that the CRYPT validator transforms the password by hashing it, so any validators applied after the CRYPT validator will receive the hashed password rather than the original.

Upvotes: 2

Related Questions