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