| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # coding: utf-8 | |
| 2 | |
| 3 # This file is part of the Adblock Plus web scripts, | |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | |
| 5 # | |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | |
| 7 # it under the terms of the GNU General Public License version 3 as | |
| 8 # published by the Free Software Foundation. | |
| 9 # | |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 # GNU General Public License for more details. | |
| 14 # | |
| 15 # You should have received a copy of the GNU General Public License | |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | |
| 18 import os | |
| 19 import errno | |
| 20 import fcntl | |
| 21 import random | |
| 22 import string | |
| 23 import wsgiref.util | |
| 24 from urlparse import parse_qs, urljoin | |
| 25 from urllib import urlencode | |
| 26 | |
| 27 from sitescripts.utils import get_config, sendMail | |
| 28 from sitescripts.web import url_handler, form_handler | |
| 29 | |
| 30 VERIFICATION_PATH = '/verifyEmail' | |
| 31 VERIFICATION_CODE_PARAM = 'code' | |
|
kzar
2015/04/21 17:14:31
Nit: Seems to me that the constant VERIFICATION_CO
Sebastian Noack
2015/04/21 18:10:30
What's ugly about it? Yes, the code would be short
kzar
2015/04/22 09:17:46
Well ugliness is subjective but to me VERIFICATION
| |
| 32 | |
| 33 def generate_verification_code(): | |
| 34 return ''.join(random.sample(string.letters + string.digits, 32)) | |
| 35 | |
| 36 def get_filename_for_verification_code(config, verification_code): | |
| 37 directory = config.get('submitEmail', 'pendingVerficationsDirectory') | |
| 38 return os.path.join(directory, verification_code) | |
| 39 | |
| 40 def create_file_for_verification_code(config): | |
| 41 flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, 'O_BINARY', 0) | |
| 42 | |
| 43 while True: | |
| 44 verification_code = generate_verification_code() | |
| 45 filename = get_filename_for_verification_code(config, verification_code) | |
| 46 | |
| 47 try: | |
| 48 return (os.open(filename, flags), verification_code) | |
| 49 except OSError as e: | |
| 50 if e.errno != errno.EEXIST: | |
| 51 raise | |
| 52 | |
| 53 def verify_email_address(config, verification_code): | |
| 54 if verification_code in ('', os.path.curdir, os.path.pardir): | |
|
kzar
2015/04/21 17:14:31
Maybe we should check that the code matches a rege
Sebastian Noack
2015/04/21 18:10:30
This would duplicate the format of the verificatio
kzar
2015/04/22 09:17:46
Well if the filterhits code review is anything to
Sebastian Noack
2015/04/22 10:06:00
I don't know which comment in that other review yo
kzar
2015/04/22 10:13:04
Exactly, that's what I meant, I think we should ad
Sebastian Noack
2015/04/22 12:19:06
Ok, went with this now, but without duplicating th
| |
| 55 return False | |
| 56 if os.path.sep in verification_code: | |
| 57 return False | |
| 58 | |
| 59 verification_filename = get_filename_for_verification_code(config, verificatio n_code) | |
| 60 try: | |
| 61 file = open(verification_filename, 'rb') | |
| 62 except IOError as e: | |
| 63 if e.errno == errno.ENOENT: | |
| 64 return False | |
| 65 raise | |
| 66 | |
| 67 with file: | |
| 68 email = file.read() | |
| 69 | |
| 70 verified_filename = config.get('submitEmail', 'verifiedEmailAddressesFile') | |
| 71 with open(verified_filename, 'ab', 0) as file: | |
| 72 fcntl.lockf(file, fcntl.LOCK_EX) | |
| 73 try: | |
| 74 print >>file, email | |
| 75 finally: | |
| 76 fcntl.lockf(file, fcntl.LOCK_UN) | |
| 77 | |
| 78 try: | |
| 79 os.unlink(verification_filename) | |
| 80 except OSError as e: | |
| 81 if e.errno != errno.ENOENT: | |
| 82 raise | |
| 83 | |
| 84 return True | |
| 85 | |
| 86 @url_handler('/submitEmail') | |
| 87 @form_handler | |
| 88 def submitEmail(environ, start_response, data): | |
| 89 email = data.get('email', '').strip() | |
| 90 if not email or '\r' in email or '\n' in email: | |
|
kzar
2015/04/21 17:14:31
Should we also check that the email address hasn't
Sebastian Noack
2015/04/21 18:10:30
That won't scale the way data are stored. But we c
kzar
2015/04/22 09:17:46
OK, fair enough.
| |
| 91 start_response('400 Bad Request', [('Content-Type', 'text/plain')]) | |
| 92 if email: | |
| 93 message = 'No newlines allowed in email address.' | |
| 94 else | |
| 95 message = 'No email address given.' | |
| 96 return [message] | |
|
kzar
2015/04/21 17:14:31
Nit: Instead of assigning `message` wouldn't it be
Sebastian Noack
2015/04/21 18:10:30
Then the else block would be redundant. And removi
kzar
2015/04/22 09:17:46
Well it just seems like busy-work to me. Keeping s
Sebastian Noack
2015/04/22 10:06:00
Wouldn't it be the same when doing it the other wa
| |
| 97 | |
| 98 config = get_config() | |
| 99 fd, verification_code = create_file_for_verification_code(config) | |
| 100 try: | |
| 101 os.write(fd, email.encode('utf-8')) | |
| 102 finally: | |
| 103 os.close(fd) | |
| 104 | |
| 105 sendMail( | |
| 106 config.get('submitEmail', 'verificationEmailTemplate'), | |
| 107 { | |
| 108 'recipient': email, | |
| 109 'verification_url': '%s?%s' % ( | |
| 110 urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH), | |
| 111 urlencode([(VERIFICATION_CODE_PARAM, verification_code)]) | |
| 112 ) | |
| 113 } | |
| 114 ) | |
| 115 | |
| 116 start_response('200 OK', [('Content-Type', 'text/plain')]) | |
| 117 return ["Thanks for your submission! You'll receive a verification email short ly."] | |
| 118 | |
| 119 @url_handler(VERIFICATION_PATH) | |
| 120 def verifyEmail(environ, start_response): | |
| 121 config = get_config() | |
| 122 | |
| 123 params = parse_qs(environ.get('QUERY_STRING', '')) | |
| 124 verification_code = params.get(VERIFICATION_CODE_PARAM, [''])[0] | |
| 125 | |
| 126 if verify_email_address(config, verification_code): | |
| 127 option = 'successfulVerificationRedirectLocation' | |
| 128 else: | |
| 129 option = 'unknownVerificationCodeRedirectLocation' | |
| 130 | |
| 131 start_response('303 See other', [('Location', config.get('submitEmail', option ))]) | |
| 132 return [] | |
| OLD | NEW |