Commit 88855258 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.

Conflicts:
	snf-pithos-app/pithos/api/settings.py
	snf-pithos-app/pithos/api/util.py
parent 4ac15150
#coding=utf8
from django.conf import settings
from synnefo.lib import parse_base_url, join_urls
from synnefo.lib.services import fill_endpoints
from synnefo.lib.services import fill_endpoints, get_public_endpoint
from synnefo.util.keypath import get_path, set_path
from pithos.api.services import pithos_services as vanilla_pithos_services
from astakosclient import astakos_services as vanilla_astakos_services
......@@ -103,5 +103,9 @@ PUBLIC_URL_SECURITY = getattr(settings, 'PITHOS_PUBLIC_URL_SECURITY', 16)
PUBLIC_URL_ALPHABET = getattr(
settings,
'PITHOS_PUBLIC_URL_ALPHABET',
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
ASTAKOS_UI_URL = get_public_endpoint(astakos_services, 'astakos_ui', '').rstrip('/')
# Astakos login URL to redirect if the user information is missing
LOGIN_URL = join_urls(ASTAKOS_UI_URL, '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
......@@ -63,7 +64,7 @@ from pithos.api.settings import (BACKEND_DB_MODULE, BACKEND_DB_CONNECTION,
RADOS_POOL_MAPS, TRANSLATE_UUIDS,
PUBLIC_URL_SECURITY,
PUBLIC_URL_ALPHABET,
COOKIE_NAME, BASE_HOST)
COOKIE_NAME, BASE_HOST, LOGIN_URL)
from pithos.api.resources import resources
from pithos.backends.base import (NotAllowedError, QuotaError, ItemNotExists,
VersionNotExists)
......@@ -1113,16 +1114,21 @@ 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