|
28 | 28 | # The full license is in the file LICENSE, distributed with this software. |
29 | 29 | # ----------------------------------------------------------------------------- |
30 | 30 | from __future__ import division |
31 | | -from re import match |
| 31 | +from re import sub |
32 | 32 |
|
33 | 33 | from qiita_core.exceptions import (IncorrectEmailError, IncorrectPasswordError, |
34 | 34 | IncompetentQiitaDeveloperError) |
@@ -476,15 +476,40 @@ def validate_email(email): |
476 | 476 | bool |
477 | 477 | Whether or not the email is valid |
478 | 478 | """ |
479 | | - valid_chars = "a-zA-Z0-9\.\+\-" |
480 | | - pattern = r"[%s]+@[%s]+\.[%s]+" % (valid_chars, valid_chars, valid_chars) |
481 | | - |
| 479 | + # Do not accept email addresses that have unicode characters |
482 | 480 | try: |
483 | 481 | email.encode('ascii') |
484 | 482 | except UnicodeError: |
485 | 483 | return False |
486 | 484 |
|
487 | | - return True if match(pattern, email) is not None else False |
| 485 | + # we are not allowing quoted strings in the email address |
| 486 | + if '"' in email: |
| 487 | + return False |
| 488 | + |
| 489 | + # Must have exactly 1 @ symbol |
| 490 | + if email.count('@') != 1: |
| 491 | + return False |
| 492 | + |
| 493 | + local_part, domain_part = email.split('@') |
| 494 | + |
| 495 | + # Neither part can be blank |
| 496 | + if not (local_part and domain_part): |
| 497 | + return False |
| 498 | + |
| 499 | + # The local part cannot begin or end with a dot |
| 500 | + if local_part.startswith('.') or local_part.endswith('.'): |
| 501 | + return False |
| 502 | + |
| 503 | + # This is the full set of allowable characters for the local part. |
| 504 | + local_valid_chars = "[a-zA-Z0-9#_~!$&'()*+,;=:.-]" |
| 505 | + if len(sub(local_valid_chars, '', local_part)): |
| 506 | + return False |
| 507 | + |
| 508 | + domain_valid_chars = "[a-zA-Z0-9.]" |
| 509 | + if len(sub(domain_valid_chars, '', domain_part)): |
| 510 | + return False |
| 511 | + |
| 512 | + return True |
488 | 513 |
|
489 | 514 |
|
490 | 515 | def validate_password(password): |
|
0 commit comments