root / snf-astakos-app / astakos / im / api / admin.py @ 7fa3d29f
History | View | Annotate | Download (7 kB)
1 |
# Copyright 2011-2012 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 |
import logging |
35 |
|
36 |
from functools import wraps |
37 |
from time import time, mktime |
38 |
|
39 |
from django.http import HttpResponse |
40 |
from django.utils import simplejson as json |
41 |
|
42 |
from astakos.im.api.faults import ( |
43 |
Fault, Unauthorized, InternalServerError, BadRequest, |
44 |
Forbidden |
45 |
) |
46 |
from astakos.im.api import render_fault, _get_user_by_email, _get_user_by_username |
47 |
from astakos.im.models import AstakosUser |
48 |
from astakos.im.util import epoch |
49 |
|
50 |
logger = logging.getLogger(__name__) |
51 |
format = ('%a, %d %b %Y %H:%M:%S GMT')
|
52 |
|
53 |
|
54 |
def api_method(http_method=None, token_required=False, perms=None): |
55 |
"""Decorator function for views that implement an API method."""
|
56 |
if not perms: |
57 |
perms = [] |
58 |
|
59 |
def decorator(func): |
60 |
@wraps(func)
|
61 |
def wrapper(request, *args, **kwargs): |
62 |
try:
|
63 |
if http_method and request.method != http_method: |
64 |
raise BadRequest('Method not allowed.') |
65 |
x_auth_token = request.META.get('HTTP_X_AUTH_TOKEN')
|
66 |
if token_required:
|
67 |
if not x_auth_token: |
68 |
raise Unauthorized('Access denied') |
69 |
try:
|
70 |
user = AstakosUser.objects.get(auth_token=x_auth_token) |
71 |
if not user.has_perms(perms): |
72 |
raise Forbidden('Unauthorized request') |
73 |
except AstakosUser.DoesNotExist, e:
|
74 |
raise Unauthorized('Invalid X-Auth-Token') |
75 |
kwargs['user'] = user
|
76 |
response = func(request, *args, **kwargs) |
77 |
return response
|
78 |
except Fault, fault:
|
79 |
return render_fault(request, fault)
|
80 |
except BaseException, e: |
81 |
logger.exception('Unexpected error: %s' % e)
|
82 |
fault = InternalServerError('Unexpected error')
|
83 |
return render_fault(request, fault)
|
84 |
return wrapper
|
85 |
return decorator
|
86 |
|
87 |
|
88 |
@api_method(http_method='GET', token_required=True) |
89 |
def authenticate_old(request, user=None): |
90 |
# Normal Response Codes: 204
|
91 |
# Error Response Codes: internalServerError (500)
|
92 |
# badRequest (400)
|
93 |
# unauthorised (401)
|
94 |
if not user: |
95 |
raise BadRequest('No user') |
96 |
|
97 |
# Check if the is active.
|
98 |
if not user.is_active: |
99 |
raise Unauthorized('User inactive') |
100 |
|
101 |
# Check if the token has expired.
|
102 |
if (time() - mktime(user.auth_token_expires.timetuple())) > 0: |
103 |
raise Unauthorized('Authentication expired') |
104 |
|
105 |
if not user.signed_terms: |
106 |
raise Unauthorized('Pending approval terms') |
107 |
|
108 |
response = HttpResponse() |
109 |
response.status = 204
|
110 |
user_info = {'username': user.username,
|
111 |
'uniq': user.email,
|
112 |
'auth_token': user.auth_token,
|
113 |
'auth_token_created': user.auth_token_created.isoformat(),
|
114 |
'auth_token_expires': user.auth_token_expires.isoformat(),
|
115 |
'has_credits': user.has_credits,
|
116 |
'has_signed_terms': user.signed_terms,
|
117 |
'groups': [g.name for g in user.groups.all()]} |
118 |
response.content = json.dumps(user_info) |
119 |
response['Content-Type'] = 'application/json; charset=UTF-8' |
120 |
response['Content-Length'] = len(response.content) |
121 |
return response
|
122 |
|
123 |
|
124 |
@api_method(http_method='GET', token_required=True) |
125 |
def authenticate(request, user=None): |
126 |
# Normal Response Codes: 204
|
127 |
# Error Response Codes: internalServerError (500)
|
128 |
# badRequest (400)
|
129 |
# unauthorised (401)
|
130 |
if not user: |
131 |
raise BadRequest('No user') |
132 |
|
133 |
# Check if the is active.
|
134 |
if not user.is_active: |
135 |
raise Unauthorized('User inactive') |
136 |
|
137 |
# Check if the token has expired.
|
138 |
if (time() - mktime(user.auth_token_expires.timetuple())) > 0: |
139 |
raise Unauthorized('Authentication expired') |
140 |
|
141 |
if not user.signed_terms: |
142 |
raise Unauthorized('Pending approval terms') |
143 |
|
144 |
response = HttpResponse() |
145 |
response.status = 204
|
146 |
user_info = {'userid': user.username,
|
147 |
'email': [user.email],
|
148 |
'name': user.realname,
|
149 |
'auth_token': user.auth_token,
|
150 |
'auth_token_created': epoch(user.auth_token_created),
|
151 |
'auth_token_expires': epoch(user.auth_token_expires),
|
152 |
'has_credits': user.has_credits,
|
153 |
'is_active': user.is_active,
|
154 |
'groups': [g.name for g in user.groups.all()]} |
155 |
response.content = json.dumps(user_info) |
156 |
response['Content-Type'] = 'application/json; charset=UTF-8' |
157 |
response['Content-Length'] = len(response.content) |
158 |
return response
|
159 |
|
160 |
|
161 |
@api_method(http_method='GET', token_required=True, perms=['im.can_access_userinfo']) |
162 |
def get_user_by_email(request, user=None): |
163 |
# Normal Response Codes: 200
|
164 |
# Error Response Codes: internalServerError (500)
|
165 |
# badRequest (400)
|
166 |
# unauthorised (401)
|
167 |
# forbidden (403)
|
168 |
# itemNotFound (404)
|
169 |
email = request.GET.get('name')
|
170 |
return _get_user_by_email(email)
|
171 |
|
172 |
|
173 |
@api_method(http_method='GET', token_required=True, perms=['im.can_access_userinfo']) |
174 |
def get_user_by_username(request, user_id, user=None): |
175 |
# Normal Response Codes: 200
|
176 |
# Error Response Codes: internalServerError (500)
|
177 |
# badRequest (400)
|
178 |
# unauthorised (401)
|
179 |
# forbidden (403)
|
180 |
# itemNotFound (404)
|
181 |
return _get_user_by_username(user_id)
|