Projet

Général

Profil

0004-implement-URL-signatures-in-the-file-validation-web-.patch

Benjamin Dauvergne, 31 mars 2016 15:19

Télécharger (3,3 ko)

Voir les différences:

Subject: [PATCH 4/4] implement URL signatures in the file validation
 web-service calls (#10444)

 wcs/file_validation.py | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)
wcs/file_validation.py
22 22
from qommon.misc import http_get_page, json_loads, http_post_request
23 23
from quixote import get_publisher, get_request
24 24

  
25
from wcs.api_utils import get_secret_and_orig, sign_url
26

  
25 27

  
26 28
def has_file_validation():
27 29
    return get_publisher().get_site_option('fargo_url') is not None
28 30

  
29 31

  
30
def fargo_get(path):
32
def fargo_url(url):
31 33
    fargo_url = get_publisher().get_site_option('fargo_url')
32
    url = urlparse.urljoin(fargo_url, path)
34
    url = urlparse.urljoin(fargo_url, url)
35
    secret, orig = get_secret_and_orig(url)
36
    if '?' in url:
37
        url += '&orig=%s' % orig
38
    else:
39
        url += '?orig=%s' % orig
40
    return sign_url(url, secret)
41

  
42

  
43
def fargo_get(url):
44
    url = fargo_url(url)
33 45
    response, status, data, auth_header = http_get_page(url)
34 46
    if status == 200:
35 47
        return json_loads(data)
36 48
    return None
37 49

  
38 50

  
51
def fargo_post_json(url, payload):
52
    url = fargo_url(url)
53
    headers = {'Content-Type': 'application/json'}
54
    response, status, response_payload, auth_header = http_post_request(
55
        url, json.dumps(payload), headers=headers)
56
    return status, json_loads(response_payload)
57

  
58

  
39 59
def sha256_of_upload(upload):
40 60
    return hashlib.sha256(upload.get_content()).hexdigest()
41 61

  
......
61 81

  
62 82

  
63 83
def get_validation(url):
64
    response, status, data, auth_header = http_get_page(url)
65
    if status == 200:
66
        return json_loads(data)['data']
67
    return None
84
    return fargo_get(url)
68 85

  
69 86

  
70 87
def get_validations(document_type):
......
94 111
def validate(filled, field, upload):
95 112
    '''Compute link to Fargo to validate the given document'''
96 113
    document_type_id = field.document_type['id']
97
    path = 'api/validation/%s/' % urllib.quote(document_type_id)
98
    fargo_url = get_publisher().get_site_option('fargo_url')
99
    url = urlparse.urljoin(fargo_url, path)
114
    url = '/api/validation/%s/' % urllib.quote(document_type_id)
100 115
    payload = {}
101 116
    if filled.user:
102 117
        if filled.user.name_identifiers:
......
108 123
    payload['content_hash'] = sha256_of_upload(upload)
109 124
    for meta_field in field.metadata:
110 125
        payload[meta_field['name']] = upload.metadata.get(meta_field['name'], '')
111
    headers = {'Content-Type': 'application/json'}
112
    response, status, response_payload, auth_header = http_post_request(url, json.dumps(payload),
113
                                                                        headers=headers)
126
    status, response = fargo_post_json(url, payload)
114 127
    if status == 201:
115
        upload.metadata = json_loads(response_payload)['data']
128
        upload.metadata = response['data']
116 129
        filled.data['%s_structured' % field.id] = upload.metadata
117 130
        filled.store()
118
-