109 changes: 94 additions & 15 deletions geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import os
import gc
import re
import six
import ast
import base64
import copy
import datetime
import time
import base64
import logging
import os
import re
import subprocess
import select
import tempfile
import tarfile
import time
import shutil
import string
import requests
import urlparse
import urllib
import gc
import tarfile
import weakref
import datetime
import requests
import tempfile
import urlparse
import traceback
import subprocess

from osgeo import ogr
from slugify import slugify
Expand All @@ -50,8 +50,9 @@
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.http import HttpResponse
from django.http import Http404, HttpResponse
from django.utils.http import is_safe_url
from django.middleware.csrf import get_token
from django.shortcuts import get_object_or_404
# use lazy gettext because some translated strings are used before
# i18n infra is up
Expand All @@ -61,7 +62,10 @@
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth import get_user_model
from geonode import geoserver, qgis_server, GeoNodeException # noqa
from geonode.base.auth import get_or_create_token
from geonode.base.auth import (extend_token,
get_or_create_token,
get_token_from_auth_header,
get_token_object_from_session)

try:
import json
Expand Down Expand Up @@ -135,6 +139,80 @@ def extract_tarfile(upload_file, extension='.shp', tempdir=None):
return absolute_base_file


def get_headers(request, url, raw_url, allowed_hosts=[]):
headers = {}
cookies = None
csrftoken = None

if settings.SESSION_COOKIE_NAME in request.COOKIES and is_safe_url(
url=raw_url, host=url.hostname):
cookies = request.META["HTTP_COOKIE"]

for cook in request.COOKIES:
name = str(cook)
value = request.COOKIES.get(name)
if name == 'csrftoken':
csrftoken = value
cook = "%s=%s" % (name, value)
cookies = cook if not cookies else (cookies + '; ' + cook)

csrftoken = get_token(request) if not csrftoken else csrftoken

if csrftoken:
headers['X-Requested-With'] = "XMLHttpRequest"
headers['X-CSRFToken'] = csrftoken
cook = "%s=%s" % ('csrftoken', csrftoken)
cookies = cook if not cookies else (cookies + '; ' + cook)

if cookies:
if 'JSESSIONID' in request.session and request.session['JSESSIONID']:
cookies = cookies + '; JSESSIONID=' + \
request.session['JSESSIONID']
headers['Cookie'] = cookies

if request.method in ("POST", "PUT") and "CONTENT_TYPE" in request.META:
headers["Content-Type"] = request.META["CONTENT_TYPE"]

access_token = None
site_url = urlparse.urlsplit(settings.SITEURL)
allowed_hosts += [url.hostname]
# We want to convert HTTP_AUTH into a Beraer Token only when hitting the local GeoServer
if site_url.hostname in allowed_hosts:
# we give precedence to obtained from Aithorization headers
if 'HTTP_AUTHORIZATION' in request.META:
auth_header = request.META.get(
'HTTP_AUTHORIZATION',
request.META.get('HTTP_AUTHORIZATION2'))
if auth_header:
headers['Authorization'] = auth_header
access_token = get_token_from_auth_header(auth_header, create_if_not_exists=True)
# otherwise we check if a session is active
elif request and request.user.is_authenticated:
access_token = get_token_object_from_session(request.session)

# we extend the token in case the session is active but the token expired
if access_token and access_token.is_expired():
extend_token(access_token)
else:
access_token = get_or_create_token(request.user)

if access_token:
headers['Authorization'] = 'Bearer %s' % access_token

pragma = "no-cache"
referer = request.META[
"HTTP_REFERER"] if "HTTP_REFERER" in request.META else \
"{scheme}://{netloc}/".format(scheme=site_url.scheme,
netloc=site_url.netloc)
encoding = request.META["HTTP_ACCEPT_ENCODING"] if "HTTP_ACCEPT_ENCODING" in request.META else "gzip"
headers.update({"Pragma": pragma,
"Referer": referer,
"Accept-encoding": encoding,
})

return (headers, access_token)


def _get_basic_auth_info(request):
"""
grab basic auth info
Expand Down Expand Up @@ -1294,11 +1372,12 @@ def __init__(self):
self.password = ogc_server_settings['PASSWORD'] if 'PASSWORD' in ogc_server_settings else 'geoserver'

def request(self, url, method='GET', data=None, headers={}, stream=False, timeout=None, user=None):

if (user or self.username != 'admin') and \
check_ogc_backend(geoserver.BACKEND_PACKAGE) and 'Authorization' not in headers:
if connection.cursor().db.vendor not in ('sqlite', 'sqlite3', 'spatialite'):
try:
if user and isinstance(user, basestring):
user = get_user_model().objects.get(username=user)
_u = user or get_user_model().objects.get(username=self.username)
access_token = get_or_create_token(_u)
if access_token and not access_token.is_expired():
Expand Down