Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/boto/mturk/notification.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:18:57 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d30785e31577 |
|---|---|
| 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(object): | |
| 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 TODO: This is doing a form of authentication and | |
| 79 this functionality should really be merged | |
| 80 with the pluggable authentication mechanism | |
| 81 at some point. | |
| 82 """ | |
| 83 verification_input = NotificationMessage.SERVICE_NAME | |
| 84 verification_input += NotificationMessage.OPERATION_NAME | |
| 85 verification_input += self.timestamp | |
| 86 h = hmac.new(key=secret_key, digestmod=sha) | |
| 87 h.update(verification_input) | |
| 88 signature_calc = base64.b64encode(h.digest()) | |
| 89 return self.signature == signature_calc | |
| 90 | |
| 91 class Event(object): | |
| 92 def __init__(self, d): | |
| 93 self.event_type = d['EventType'] | |
| 94 self.event_time_str = d['EventTime'] | |
| 95 self.hit_type = d['HITTypeId'] | |
| 96 self.hit_id = d['HITId'] | |
| 97 if 'AssignmentId' in d: # Not present in all event types | |
| 98 self.assignment_id = d['AssignmentId'] | |
| 99 | |
| 100 #TODO: build self.event_time datetime from string self.event_time_str | |
| 101 | |
| 102 def __repr__(self): | |
| 103 return "<boto.mturk.notification.Event: %s for HIT # %s>" % (self.event_type, self.hit_id) |
