Skip to content
Snippets Groups Projects
db.py 2.65 KiB
Newer Older
from os import environ
from hydra_client import HydraAdmin
from graphqlclient import GraphQLClient
import urllib
Mark's avatar
Mark committed
import json

GRAPHQL_URL = environ['GRAPHQL_URL']
Mark's avatar
Mark committed
GRAPHQL_CLIENT = GraphQLClient(GRAPHQL_URL)


class User():
    def __init__(self, username):
        self.username = username
        try:
            self._load_remote_user_info()
Mark's avatar
Mark committed
        except urllib.error.HTTPError as error:
            raise BackendConnectionError(
                error.code,
                error.headers,
                ("Error during retrieval of userdata - " + error.reason))

    def _load_remote_user_info(self):
        querystring = '''{{
        getUser(username: "{0}"){{
            email,
            applications{{
                edges{{
                    node{{
                        name
                    }}
                }}
Mark's avatar
Mark committed
            }},
            roles{{
                edges{{
                    node{{
                        name
                    }}
                }}
Mark's avatar
Mark committed
        }}}}'''.format(self.username)
Mark's avatar
Mark committed
        result = json.loads(GRAPHQL_CLIENT.execute(querystring))
        data = result["data"]["getUser"]
        self.applications = list(map(lambda x: x["node"]["name"],
                                     data["applications"]["edges"]))
Mark's avatar
Mark committed
        self.roles = list(map(lambda x: x["node"]["name"],
                                     data["roles"]["edges"]))
        self.email = data["email"]

    def has_app_permission(self, appname):
Mark's avatar
Mark committed
        return appname in self.applications

    def get_oauth_session(self):
Mark's avatar
Mark committed
        """Create openID Connect token

        Use the userdata stored in the user object to create an OpenID Connect token.
Mark's avatar
Mark committed
        The token will be passed to Hydra, which will store it and serve it to all OpenID Connect
        Clients, that successfully query the /userinfo endpoint. Every field in the "id_token"
Mark's avatar
Mark committed
        dictionary can be accessed through standard scopes and claims.
        See https://openid.net/specs/openid-connect-core-1_0.html#Claims

        Returns:
            OpenID Connect token of type dict
        """
        return {
            "id_token": {
                "name": self.username,
                "preferred_username": self.username,
Mark's avatar
Mark committed
                "email" : self.email,
                "openappstack_roles": self.roles}
Mark's avatar
Mark committed


class BackendConnectionError(Exception):
    """Raised when requests to the backend server fail

    Attributes:
        code -- http response code
        headers -- http response headers
        reason -- reson for the error
    """
    def __init__(self, code, headers, reason):
        self.code = code
        self.headers = headers
        self.reason = reason