~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/mturk/notification.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
"""
 
23
Provides NotificationMessage and Event classes, with utility methods, for
 
24
implementations of the Mechanical Turk Notification API.
 
25
"""
 
26
 
 
27
import hmac
 
28
try:
 
29
    from hashlib import sha1 as sha
 
30
except ImportError:
 
31
    import sha
 
32
import base64
 
33
import re
 
34
 
 
35
class NotificationMessage:
 
36
 
 
37
    NOTIFICATION_WSDL = "http://mechanicalturk.amazonaws.com/AWSMechanicalTurk/2006-05-05/AWSMechanicalTurkRequesterNotification.wsdl"
 
38
    NOTIFICATION_VERSION = '2006-05-05'
 
39
 
 
40
    SERVICE_NAME = "AWSMechanicalTurkRequesterNotification"
 
41
    OPERATION_NAME = "Notify"
 
42
 
 
43
    EVENT_PATTERN = r"Event\.(?P<n>\d+)\.(?P<param>\w+)"
 
44
    EVENT_RE = re.compile(EVENT_PATTERN)
 
45
 
 
46
    def __init__(self, d):
 
47
        """
 
48
        Constructor; expects parameter d to be a dict of string parameters from a REST transport notification message
 
49
        """
 
50
        self.signature = d['Signature'] # vH6ZbE0NhkF/hfNyxz2OgmzXYKs=
 
51
        self.timestamp = d['Timestamp'] # 2006-05-23T23:22:30Z
 
52
        self.version = d['Version'] # 2006-05-05
 
53
        assert d['method'] == NotificationMessage.OPERATION_NAME, "Method should be '%s'" % NotificationMessage.OPERATION_NAME
 
54
 
 
55
        # Build Events
 
56
        self.events = []
 
57
        events_dict = {}
 
58
        if 'Event' in d:
 
59
            # TurboGears surprised me by 'doing the right thing' and making { 'Event': { '1': { 'EventType': ... } } } etc.
 
60
            events_dict = d['Event']
 
61
        else:
 
62
            for k in d:
 
63
                v = d[k]
 
64
                if k.startswith('Event.'):
 
65
                    ed = NotificationMessage.EVENT_RE.search(k).groupdict()
 
66
                    n = int(ed['n'])
 
67
                    param = str(ed['param'])
 
68
                    if n not in events_dict:
 
69
                        events_dict[n] = {}
 
70
                    events_dict[n][param] = v
 
71
        for n in events_dict:
 
72
            self.events.append(Event(events_dict[n]))
 
73
 
 
74
    def verify(self, secret_key):
 
75
        """
 
76
        Verifies the authenticity of a notification message.
 
77
        """
 
78
        verification_input = NotificationMessage.SERVICE_NAME + NotificationMessage.OPERATION_NAME + self.timestamp
 
79
        h = hmac.new(key=secret_key, digestmod=sha)
 
80
        h.update(verification_input)
 
81
        signature_calc = base64.b64encode(h.digest())
 
82
        return self.signature == signature_calc
 
83
 
 
84
class Event:
 
85
    def __init__(self, d):
 
86
        self.event_type = d['EventType']
 
87
        self.event_time_str = d['EventTime']
 
88
        self.hit_type = d['HITTypeId']
 
89
        self.hit_id = d['HITId']
 
90
        self.assignment_id = d['AssignmentId']
 
91
 
 
92
        #TODO: build self.event_time datetime from string self.event_time_str
 
93
 
 
94
    def __repr__(self):
 
95
        return "<boto.mturk.notification.Event: %s for HIT # %s>" % (self.event_type, self.hit_id)