Commit 5f1e36aa authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

pithos: Update view_method decorator

* If no token found in the cookie redirect to astakos login
with next parameter pointing to the request path
* Extend the list of the valid response statuses
* If no expected response status is received, pass to the raised
exception the response status instead of the whole response.
parent 1c8fdb54
......@@ -125,3 +125,5 @@ BACKEND_BLOCK_SIZE = getattr(
# The backend block hash algorithm
BACKEND_HASH_ALGORITHM = getattr(
settings, 'PITHOS_BACKEND_HASH_ALGORITHM', 'sha256')
LOGIN_URL = join_urls(ASTAKOS_VIEWS_PREFIX, 'login')
......@@ -35,7 +35,8 @@ from functools import wraps
from datetime import datetime
from urllib import quote, unquote
from django.http import HttpResponse, Http404, HttpResponseForbidden
from django.http import (HttpResponse, HttpResponseRedirect, Http404,
HttpResponseForbidden)
from django.template.loader import render_to_string
from django.utils import simplejson as json
from django.utils.http import http_date, parse_etags
......@@ -64,7 +65,7 @@ from pithos.api.settings import (BACKEND_DB_MODULE, BACKEND_DB_CONNECTION,
RADOS_STORAGE, RADOS_POOL_BLOCKS,
RADOS_POOL_MAPS, TRANSLATE_UUIDS,
PUBLIC_URL_SECURITY, PUBLIC_URL_ALPHABET,
COOKIE_NAME, BASE_HOST, UPDATE_MD5)
COOKIE_NAME, BASE_HOST, UPDATE_MD5, LOGIN_URL)
from pithos.api.resources import resources
from pithos.backends import connect_backend
from pithos.backends.base import (NotAllowedError, QuotaError, ItemNotExists,
......@@ -1125,17 +1126,22 @@ def view_method():
def decorator(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
request.META['HTTP_X_AUTH_TOKEN'] = get_token_from_cookie(request)
token = get_token_from_cookie(request)
if token is None:
return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL,
request.path))
request.META['HTTP_X_AUTH_TOKEN'] = token
# Get the response object
response = func(request, *args, **kwargs)
if response.status_code == 200:
if response.status_code in [200, 206, 304, 412, 416]:
return response
elif response.status_code == 404:
raise Http404()
elif response.status_code in [401, 403]:
return HttpResponseForbidden()
else:
raise Exception(response)
# unexpected response status
raise Exception(response.status_code)
return wrapper
return decorator
......
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