-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathdecorators.py
More file actions
53 lines (36 loc) · 1.45 KB
/
decorators.py
File metadata and controls
53 lines (36 loc) · 1.45 KB
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
from functools import wraps
from flask import request, abort, current_app, g
from cryptography import x509
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.backends import default_backend
import plistlib
def parse_plist_input_data(f):
"""Parses plist data as HTTP input from request.
The unserialized data is attached to the global **g** object as **g.plist_data**.
:status 400: If invalid plist data was supplied in the request.
"""
@wraps(f)
def decorator(*args, **kwargs):
try:
if current_app.debug:
current_app.logger.debug(request.data)
g.plist_data = plistlib.loads(request.data)
except:
current_app.logger.info('could not parse property list input data')
abort(400, 'invalid input data')
return f(*args, **kwargs)
return decorator
def pem_certificate_upload(f):
"""Parse PEM formatted certificate in request data
TODO: form field name option
"""
@wraps(f)
def decorator(*args, **kwargs):
try:
certificate_data = request.files['file'].read()
g.certificate = x509.load_pem_x509_certificate(certificate_data, backend=default_backend())
except UnsupportedAlgorithm as e:
current_app.logger.info('could not parse PEM certificate data')
abort(400, 'invalid input data')
return f(*args, **kwargs)
return decorator