user_utils.py 1.93 KB
Newer Older
Benedikt Geißler's avatar
Benedikt Geißler committed
1 2 3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

Robin Buse's avatar
Robin Buse committed
4
import re
Daniel Schreiber's avatar
Daniel Schreiber committed
5 6 7 8
try:
    from urlparse import urljoin
except ImportError:
    from urllib.parse import urljoin
Robin Buse's avatar
Robin Buse committed
9

Benedikt Geißler's avatar
Benedikt Geißler committed
10
from django.conf import settings
11
from django.core.urlresolvers import get_script_prefix
12
from rest_framework.authtoken.models import Token
13 14
from rest_framework.reverse import reverse

Robin Buse's avatar
Robin Buse committed
15
from api.models import TokenAuthUser, UserType, Resource
16 17


Robin Buse's avatar
Robin Buse committed
18
def extract_idp(eppn):
Benedikt Geißler's avatar
Benedikt Geißler committed
19
    return ".".join(re.split("\.|@", eppn)[-2:])
Robin Buse's avatar
Robin Buse committed
20 21


22
def get_local_url():
23
    return settings.HTTP_SERVER_NAME
24 25


26 27 28 29 30 31 32
def get_local_token():
    user = TokenAuthUser.objects.get(user_type=UserType.TYPE_API)
    token = Token.objects.get(user_id=user)
    return token.key


def get_remote_url_by_realm(realm):
Robin Buse's avatar
Robin Buse committed
33
    user = TokenAuthUser.objects.filter(user_type=UserType.TYPE_SYNC, realm=realm).first()
34 35 36 37 38 39
    if user:
        return user.api_url

    return None


Robin Buse's avatar
Robin Buse committed
40
def get_remote_url_by_res_uuid(uuid):
Robin Buse's avatar
Robin Buse committed
41
    res = Resource.objects.get(uuid=uuid)
Robin Buse's avatar
Robin Buse committed
42

43
    if res.sp.realm == settings.SERVER_REALM:
Robin Buse's avatar
Robin Buse committed
44 45 46 47
        # We are acting as SP here
        return get_remote_url_by_realm(res.realm_idp)
    else:
        # We are acting as IdP here
Robin Buse's avatar
Robin Buse committed
48
        return get_remote_url_by_realm(res.sp.realm)
Robin Buse's avatar
Robin Buse committed
49 50


51 52
def get_remote_token_by_realm(realm):
    user = TokenAuthUser.objects.filter(realm=realm, user_type=UserType.TYPE_SYNC).first()
53 54 55 56 57
    if user:
        token = Token.objects.get(user_id=user)
        return token.key

    return ""
Robin Buse's avatar
Robin Buse committed
58 59


Robin Buse's avatar
Robin Buse committed
60
def get_remote_token_by_res_uuid(uuid):
Robin Buse's avatar
Robin Buse committed
61
    res = Resource.objects.get(uuid=uuid)
Robin Buse's avatar
Robin Buse committed
62

63
    if res.sp.realm == settings.SERVER_REALM:
Robin Buse's avatar
Robin Buse committed
64 65 66 67
        # We are acting as SP here
        return get_remote_token_by_realm(res.realm_idp)
    else:
        # We are acting as IdP here
Robin Buse's avatar
Robin Buse committed
68
        return get_remote_token_by_realm(res.sp.realm)
69 70 71 72 73 74 75 76 77


def local_reverse(*args, **kwargs):
    r = reverse(*args, **kwargs)
    local_prefix = get_script_prefix()

    if r.startswith(local_prefix):
        r = r[len(local_prefix):]

Benedikt Geißler's avatar
Benedikt Geißler committed
78
    return urljoin(get_local_url(), r)