Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/submit_email/web/submit_email.py

Issue 29323033: Issue 2846 - Service for processing Microsoft Edge announcement subscription (Closed)
Left Patch Set: Comments addressed Created Aug. 19, 2015, 9:59 a.m.
Right Patch Set: Rename the ADBLOCKBROWSER_PRODUCT_NAME to DEFAULT_PRODUCT Created Aug. 19, 2015, 3:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/submit_email/template/edge_verification.mail ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 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 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 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/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import fcntl 18 import fcntl
19 import hmac 19 import hmac
20 import hashlib 20 import hashlib
21 import wsgiref.util 21 import wsgiref.util
22 from urlparse import parse_qsl, urljoin 22 from urlparse import parse_qsl, urljoin
23 from urllib import urlencode, quote 23 from urllib import urlencode, quote
24 24
25 from sitescripts.utils import get_config, sendMail, encode_email_address 25 from sitescripts.utils import get_config, sendMail, encode_email_address
26 from sitescripts.web import url_handler, form_handler, send_simple_response 26 from sitescripts.web import url_handler, form_handler, send_simple_response
27 27
28 VERIFICATION_PATH = '/verifyEmail' 28 VERIFICATION_PATH = '/verifyEmail'
29 DEFAULT_PRODUCT = 'adblockbrowser'
29 30
30 def sign(config, data): 31 def sign(config, data):
31 secret = config.get('submit_email', 'secret') 32 secret = config.get('submit_email', 'secret')
32 return hmac.new(secret, data, hashlib.sha1).hexdigest() 33 return hmac.new(secret, data, hashlib.sha1).hexdigest()
33 34
34 @url_handler('/submitEmail') 35 @url_handler('/submitEmail')
35 @form_handler 36 @form_handler
36 def submit_email(environ, start_response, data): 37 def submit_email(environ, start_response, data):
37 email = data.get('email', '').strip() 38 email = data.get('email', '').strip()
38 try: 39 try:
39 email = encode_email_address(email) 40 email = encode_email_address(email)
40 except ValueError: 41 except ValueError:
41 return send_simple_response( 42 return send_simple_response(
42 start_response, 400, 43 start_response, 400,
43 'Please enter a valid email address.' 44 'Please enter a valid email address.'
44 ) 45 )
45 46
46 config = get_config() 47 config = get_config()
47 params = [('email', email), ('signature', sign(config, email))] 48 params = [('email', email), ('signature', sign(config, email))]
48 lang = data.get('lang') 49 lang = data.get('lang')
49 if lang: 50 if lang:
50 params.append(('lang', lang)) 51 params.append(('lang', lang))
51 52
52 email_template = 'verification_email_template' 53 product = data.get('product', DEFAULT_PRODUCT)
53 product = data.get('product') 54 email_template = product + '_verification_email_template'
Sebastian Noack 2015/08/19 10:10:08 What I meant is: product = data.get('product',
Oleksandr 2015/08/19 13:56:42 Done.
54 if product is not None: 55 params.append(('product', product))
55 email_template = product + '_verification_email_template'
56 params.append(('product', product))
57 56
58 sendMail( 57 sendMail(
59 config.get('submit_email', email_template), 58 config.get('submit_email', email_template),
60 { 59 {
61 'recipient': email, 60 'recipient': email,
62 'verification_url': '%s?%s' % ( 61 'verification_url': '%s?%s' % (
63 urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH), 62 urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH),
64 urlencode(params) 63 urlencode(params)
65 ) 64 )
66 } 65 }
(...skipping 11 matching lines...) Expand all
78 params = dict(parse_qsl(environ.get('QUERY_STRING', ''))) 77 params = dict(parse_qsl(environ.get('QUERY_STRING', '')))
79 78
80 email = params.get('email', '') 79 email = params.get('email', '')
81 signature = params.get('signature', '') 80 signature = params.get('signature', '')
82 if sign(config, email) != signature: 81 if sign(config, email) != signature:
83 return send_simple_response( 82 return send_simple_response(
84 start_response, 403, 83 start_response, 403,
85 'Invalid signature in verification request.' 84 'Invalid signature in verification request.'
86 ) 85 )
87 86
88 filename = config.get('submit_email', 'filename') 87 product = params.get('product', DEFAULT_PRODUCT)
89 product = params.get('product') 88 filename = config.get('submit_email', product + '_filename')
Sebastian Noack 2015/08/19 10:10:08 Same here.
90 if product is not None:
91 filename = config.get('submit_email', product + '_filename')
92 89
93 with open(filename, 'ab', 0) as file: 90 with open(filename, 'ab', 0) as file:
94 fcntl.lockf(file, fcntl.LOCK_EX) 91 fcntl.lockf(file, fcntl.LOCK_EX)
95 try: 92 try:
96 print >>file, email 93 print >>file, email
97 finally: 94 finally:
98 fcntl.lockf(file, fcntl.LOCK_UN) 95 fcntl.lockf(file, fcntl.LOCK_UN)
99 96
100 location = config.get('submit_email', 'successful_verification_redirect_locati on') 97 location = config.get('submit_email', 'successful_verification_redirect_locati on')
101 location = location.format(lang=quote(params.get('lang') or 'en', '')) 98 location = location.format(lang=quote(params.get('lang') or 'en', ''))
102 start_response('303 See Other', [('Location', location)]) 99 start_response('303 See Other', [('Location', location)])
103 return [] 100 return []
LEFTRIGHT

Powered by Google App Engine
This is Rietveld