root / snf-astakos-app / astakos / api / util.py @ 276f454e
History | View | Annotate | Download (5.5 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from functools import wraps |
35 |
from time import time, mktime |
36 |
|
37 |
from django.http import HttpResponse |
38 |
from django.utils import simplejson as json |
39 |
|
40 |
from astakos.im.models import AstakosUser, Service |
41 |
from snf_django.lib.api import faults |
42 |
|
43 |
from astakos.im.forms import FeedbackForm |
44 |
from astakos.im.functions import send_feedback as send_feedback_func |
45 |
|
46 |
import logging |
47 |
logger = logging.getLogger(__name__) |
48 |
|
49 |
absolute = lambda request, url: request.build_absolute_uri(url)
|
50 |
|
51 |
|
52 |
def json_response(content, status_code=None): |
53 |
response = HttpResponse() |
54 |
if status_code is not None: |
55 |
response.status_code = status_code |
56 |
|
57 |
response.content = json.dumps(content) |
58 |
response['Content-Type'] = 'application/json; charset=UTF-8' |
59 |
response['Content-Length'] = len(response.content) |
60 |
return response
|
61 |
|
62 |
|
63 |
def is_integer(x): |
64 |
return isinstance(x, (int, long)) |
65 |
|
66 |
|
67 |
def are_integer(lst): |
68 |
return all(map(is_integer, lst)) |
69 |
|
70 |
|
71 |
def user_from_token(func): |
72 |
@wraps(func)
|
73 |
def wrapper(request, *args, **kwargs): |
74 |
try:
|
75 |
token = request.x_auth_token |
76 |
except AttributeError: |
77 |
raise faults.Unauthorized("No authentication token") |
78 |
|
79 |
if not token: |
80 |
raise faults.Unauthorized("Invalid X-Auth-Token") |
81 |
|
82 |
try:
|
83 |
request.user = AstakosUser.objects.get(auth_token=token) |
84 |
except AstakosUser.DoesNotExist:
|
85 |
raise faults.Unauthorized('Invalid X-Auth-Token') |
86 |
|
87 |
return func(request, *args, **kwargs)
|
88 |
return wrapper
|
89 |
|
90 |
|
91 |
def service_from_token(func): |
92 |
"""Decorator for authenticating service by it's token.
|
93 |
|
94 |
Check that a service with the corresponding token exists. Also,
|
95 |
if service's token has an expiration token, check that it has not
|
96 |
expired.
|
97 |
|
98 |
"""
|
99 |
@wraps(func)
|
100 |
def wrapper(request, *args, **kwargs): |
101 |
try:
|
102 |
token = request.x_auth_token |
103 |
except AttributeError: |
104 |
raise faults.Unauthorized("No authentication token") |
105 |
|
106 |
if not token: |
107 |
raise faults.Unauthorized("Invalid X-Auth-Token") |
108 |
try:
|
109 |
service = Service.objects.get(auth_token=token) |
110 |
except Service.DoesNotExist:
|
111 |
raise faults.Unauthorized("Invalid X-Auth-Token") |
112 |
|
113 |
# Check if the token has expired
|
114 |
expiration_date = service.auth_token_expires |
115 |
if expiration_date:
|
116 |
expires_at = mktime(expiration_date.timetuple()) |
117 |
if time() > expires_at:
|
118 |
raise faults.Unauthorized("Authentication expired") |
119 |
|
120 |
request.service_instance = service |
121 |
return func(request, *args, **kwargs)
|
122 |
return wrapper
|
123 |
|
124 |
|
125 |
def __get_uuid_displayname_catalogs(request, user_call=True): |
126 |
# Normal Response Codes: 200
|
127 |
# Error Response Codes: BadRequest (400)
|
128 |
|
129 |
try:
|
130 |
input_data = json.loads(request.raw_post_data) |
131 |
except:
|
132 |
raise faults.BadRequest('Request body should be json formatted.') |
133 |
else:
|
134 |
uuids = input_data.get('uuids', [])
|
135 |
if uuids is None and user_call: |
136 |
uuids = [] |
137 |
displaynames = input_data.get('displaynames', [])
|
138 |
if displaynames is None and user_call: |
139 |
displaynames = [] |
140 |
user_obj = AstakosUser.objects |
141 |
d = {'uuid_catalog': user_obj.uuid_catalog(uuids),
|
142 |
'displayname_catalog': user_obj.displayname_catalog(displaynames)}
|
143 |
|
144 |
response = HttpResponse() |
145 |
response.content = json.dumps(d) |
146 |
response['Content-Type'] = 'application/json; charset=UTF-8' |
147 |
response['Content-Length'] = len(response.content) |
148 |
return response
|
149 |
|
150 |
|
151 |
def send_feedback(request, email_template_name='im/feedback_mail.txt'): |
152 |
form = FeedbackForm(request.POST) |
153 |
if not form.is_valid(): |
154 |
logger.error("Invalid feedback request: %r", form.errors)
|
155 |
raise faults.BadRequest('Invalid data') |
156 |
|
157 |
msg = form.cleaned_data['feedback_msg']
|
158 |
data = form.cleaned_data['feedback_data']
|
159 |
try:
|
160 |
send_feedback_func(msg, data, request.user, email_template_name) |
161 |
except:
|
162 |
return HttpResponse(status=502) |
163 |
return HttpResponse(status=200) |