-
Notifications
You must be signed in to change notification settings - Fork 0
/
associate.py
60 lines (45 loc) · 1.66 KB
/
associate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cgi
import sandbox
import sys
from os import environ
from django.utils import simplejson as json
import logging
dfp_helper = sandbox.dfp_helper
fields = cgi.FieldStorage()
def associate(domain, verifier):
if dfp_helper.verify_incoming_association_request(domain, verifier):
response_dict = dfp_helper.association_response_dict(domain)
print "Content-Type: application/json"
print ""
print json.dumps(response_dict)
else:
logging.error("Dialback verification failed for domain %r with verifier %r", domain, verifier)
request_error("Request dialback verification failed")
def verify(domain, verifier):
if dfp_helper.verify_outgoing_association_request(domain, verifier):
print "Content-Type: application/json"
print ""
print json.dumps({"verifier": verifier})
else:
logging.error("Domain %r tried to verify with %r but that doesn't match a request we made", domain, verifier)
request_error("Not a request I made")
def request_error(message, status_line="400 Bad Request"):
print "Status: " + status_line
print "Content-Type: text/html"
print ""
print cgi.escape(message)
sys.exit(0)
if environ["REQUEST_METHOD"] != "POST":
request_error("Must make a POST request", "405 Method Not Allowed")
try:
domain = fields["domain"].value
mode = fields["mode"].value
verifier = fields["verifier"].value
except KeyError, ex:
request_error("Missing required request parameter '%s'" % ex)
if mode == "associate":
associate(domain, verifier)
elif mode == "verify":
verify(domain, verifier)
else:
request_error("Invalid mode '%s'" % mode);