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