Commit d92e0412 authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

User usage enhancement in astakos client helpers

`get_user`, `user_for_token` and `authenticate` methods now accept a `usage`
parameter which requests user usage quotas to be returned along with
authenticated user information. Server side handling has also been
implemented.
parent 4c115cd5
......@@ -45,6 +45,9 @@ from . import render_fault
from astakos.im.models import AstakosUser
from astakos.im.util import epoch
from astakos.im.api.callpoint import AstakosCallpoint
callpoint = AstakosCallpoint()
logger = logging.getLogger(__name__)
format = ('%a, %d %b %Y %H:%M:%S GMT')
......@@ -114,6 +117,17 @@ def authenticate(request, user=None):
'auth_token_created': epoch(user.auth_token_created),
'auth_token_expires': epoch(user.auth_token_expires),
'has_credits': user.has_credits}
# append usage data if requested
if request.REQUEST.get('usage', None):
resource_usage = None
result = callpoint.get_user_usage(user.id)
if result.is_success:
resource_usage = result.data
else:
resource_usage = []
user_info['usage'] = resource_usage
response.content = json.dumps(user_info)
response['Content-Type'] = 'application/json; charset=UTF-8'
response['Content-Length'] = len(response.content)
......
......@@ -35,7 +35,7 @@ import logging
from time import time, mktime
from urlparse import urlparse, urlsplit, urlunsplit
from urllib import quote, unquote
from urllib import quote, unquote, urlencode
from django.conf import settings
from django.utils import simplejson as json
......@@ -76,7 +76,7 @@ def call(token, url, headers={}):
conn = get_http_connection(p.netloc, p.scheme)
try:
conn.request('GET', p.path, **kwargs)
conn.request('GET', p.path + '?' + p.query, **kwargs)
response = conn.getresponse()
headers = response.getheaders()
headers = dict((unquote(h), unquote(v)) for h,v in headers)
......@@ -93,9 +93,15 @@ def call(token, url, headers={}):
def authenticate(
token, authentication_url='http://127.0.0.1:8000/im/authenticate'):
token, authentication_url='http://127.0.0.1:8000/im/authenticate',
usage=False):
if usage:
authentication_url += "?usage=1"
return call(token, authentication_url)
@retry(3)
def get_username(
token,
......@@ -128,7 +134,7 @@ def get_user_uuid(
return data.get('uuid')
def user_for_token(token, authentication_url, override_users):
def user_for_token(token, authentication_url, override_users, usage=False):
if not token:
return None
......@@ -139,7 +145,7 @@ def user_for_token(token, authentication_url, override_users):
return None
try:
return authenticate(token, authentication_url)
return authenticate(token, authentication_url, usage=usage)
except Exception, e:
# In case of Unauthorized response return None
if e.args and e.args[-1] == 401:
......@@ -150,25 +156,29 @@ def get_user(
request,
authentication_url='http://127.0.0.1:8000/im/authenticate',
override_users={},
fallback_token=None):
fallback_token=None,
usage=False):
request.user = None
request.user_uniq = None
# Try to find token in a parameter or in a request header.
user = user_for_token(
request.GET.get('X-Auth-Token'), authentication_url, override_users)
request.GET.get('X-Auth-Token'), authentication_url, override_users,
usage=usage)
if not user:
user = user_for_token(
request.META.get('HTTP_X_AUTH_TOKEN'),
authentication_url,
override_users)
override_users,
usage=usage)
if not user:
user = user_for_token(
fallback_token, authentication_url, override_users)
fallback_token, authentication_url, override_users,
usage=usage)
if not user:
logger.warning("Cannot retrieve user details from %s",
authentication_url)
return
return None
# use user uuid, instead of email, keep email/username reference to user_id
request.user_uniq = user['uuid']
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment