jazzband / django-two-factor-auth

Complete Two-Factor Authentication for Django providing the easiest integration into most Django projects.
MIT License
1.67k stars 445 forks source link

Optional phone number validator #129

Open bigmassa opened 8 years ago

bigmassa commented 8 years ago

Hey Bouke,

Just want to say love this project and am currently using it on two of my sites. Fantastic work :)

Just a thought for you, what I have had to do for both projects is folk the project so I can override you validator to use my own, am still fairly new to python so know of no other way to get round this so feel free to advise. https://github.com/bigmassa/django-two-factor-auth/commit/52f542604a1619ea6d831de71829db38bfeb5841

What ive done is following:

from django.conf import settings from django.core.exceptions import ValidationError from django.utils.module_loading import import_string from django.utils.translation import ugettextlazy as from phonenumber_field.phonenumber import to_python

def get_validator_class(): if getattr(settings, 'TWO_FACTOR_PHONENUMBER_VALIDATOR', None): return import_string(settings.TWO_FACTOR_PHONENUMBER_VALIDATOR) return validate_international_phonenumber

def validate_international_phonenumber(value): phone_number = to_python(value) if phone_number and not phone_number.is_valid(): raise ValidationError(validate_international_phonenumber.message, code='invalid')

validate_internationalphonenumber.message = \ ('Please enter a valid phone number, including your country code ' 'starting with + or 00.')

This way I can check for the likes of country codes etc on the phone number. Just wanted to let you know incase this is something you have not thought of or it may be that I have not found a way to do it.

Once again thanks for this, a great repo :)

Stuart

Bouke commented 8 years ago

Hi Stuart. First of all, thank you! Can you elaborate on why you need additional validators, besides the newly added validation of the phonenumbers library? Maybe I'll make validating phone numbers a responsibility of the gateway, so you can customize the validation over there.

bigmassa commented 8 years ago

Hi Bouke, it was just to only allow country code 44 (uk), thats it, but like I said it may be just that ive not come accross a better way yet.

Thanks Stu