Revision cb821f25
Added by Serghei Mihai almost 9 years ago
ckanext/ozwillo_organization_api/plugin.py | ||
---|---|---|
2 | 2 |
import hmac |
3 | 3 |
|
4 | 4 |
import ckan.plugins as plugins |
5 |
import ckan.plugins.toolkit as toolkit |
|
6 |
|
|
5 | 7 |
import ckan.logic as logic |
6 | 8 |
|
7 | 9 |
from pylons import config |
8 | 10 |
from ckan.common import request, _ |
9 | 11 |
from ckan.logic.action.create import _group_or_org_create as group_or_org_create |
10 | 12 |
|
13 |
plugin_config_prefix = 'ckanext.ozwillo_organization_api.' |
|
14 |
|
|
11 | 15 |
def valid_signature_required(func): |
12 |
plugin_config_prefix = 'ckanext.ozwillo_organization_api.' |
|
16 |
|
|
13 | 17 |
signature_header_name = config.get(plugin_config_prefix + 'signature_header_name', |
14 | 18 |
'X-Hub-Signature') |
15 | 19 |
instantiated_secret = config.get(plugin_config_prefix + 'instantiated_secret', |
... | ... | |
18 | 22 |
def wrapper(context, data): |
19 | 23 |
if signature_header_name in request.headers: |
20 | 24 |
if request.headers[signature_header_name].startswith('sha1='): |
21 |
algo, hash = request.headers[signature_header_name].rsplit('=') |
|
22 |
computed_hash = hmac.new(instantiated_secret, str(data), sha1).hexdigest() |
|
23 |
if hash != computed_hash: |
|
25 |
algo, received_hmac = request.headers[signature_header_name].rsplit('=') |
|
26 |
computed_hmac = hmac.new(instantiated_secret, str(data), sha1).hexdigest() |
|
27 |
# the received hmac is uppercase according to |
|
28 |
# http://doc.ozwillo.com/#ref-3-2-1 |
|
29 |
if received_hmac != computed_hmac.upper(): |
|
24 | 30 |
raise logic.NotAuthorized(_('Invalid HMAC')) |
25 | 31 |
else: |
26 | 32 |
raise logic.ValidationError(_('Invalid HMAC algo')) |
... | ... | |
31 | 37 |
|
32 | 38 |
@valid_signature_required |
33 | 39 |
def create_organization(context, data_dict): |
34 |
pass |
|
40 |
|
|
41 |
destruction_secret = config.get(plugin_config_prefix + 'destruction_secret', |
|
42 |
'changeme') |
|
43 |
|
|
44 |
client_id = data_dict.pop('client_id') |
|
45 |
client_secret = data_dict.pop('client_secret') |
|
46 |
instance_id = data_dict.pop('instance_id') |
|
47 |
|
|
48 |
# re-mapping received dict |
|
49 |
registration_uri = data_dict.pop('instance_registration_uri') |
|
50 |
organization = data_dict['organization'] |
|
51 |
org_dict = { |
|
52 |
'type': 'organization', |
|
53 |
'name': organization['organization_name'].lower(), |
|
54 |
'id': organization['id'], |
|
55 |
'title': organization['organization_name'], |
|
56 |
'description': organization['type'], |
|
57 |
} |
|
58 |
try: |
|
59 |
delete_uri = toolkit.url_for(controller='api', action='action', |
|
60 |
logic_function="delete-organization", |
|
61 |
ver=context['api_version'], |
|
62 |
qualified=True) |
|
63 |
|
|
64 |
group_or_org_create(context, org_dict, is_org=True) |
|
65 |
|
|
66 |
# notify about organization creation |
|
67 |
services = {'services': [{ |
|
68 |
'local_id': 'organization', |
|
69 |
'name': 'Organization ' + org_dict['name'] + ' on CKAN', |
|
70 |
'service_uri': '/organization/' + org_dict['name'], |
|
71 |
'visible': True}], |
|
72 |
'instance_id': instance_id, |
|
73 |
'destruction_uri': delete_uri, |
|
74 |
'destruction_secret': destruction_secret, |
|
75 |
'needed_scopes': [{ |
|
76 |
'scope_id': 'profile', |
|
77 |
'motivation': 'Used to link user to the organization' |
|
78 |
}] |
|
79 |
} |
|
80 |
requests.post(registration_uri, |
|
81 |
data = services, |
|
82 |
auth=(client_id, client_secret) |
|
83 |
) |
|
84 |
except: |
|
85 |
request.delete(registration_uri) |
|
86 |
|
|
35 | 87 |
|
36 | 88 |
@valid_signature_required |
37 | 89 |
def delete_organization(context, data_dict): |
Also available in: Unified diff
organization creation function prototype