Example #1
0
    def _request_token(self, env):
        """
        Retrieves a new access token from the OAuth2 server.
        """
        params = {}

        content = env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
        post_params = parse_qs(content)
        # Convert to dict for easier access
        for param, value in post_params.items():
            decoded_param = param.decode('utf-8')
            decoded_value = value[0].decode('utf-8')
            if decoded_param == "username" or decoded_param == "password":
                params[decoded_param] = decoded_value

        params["grant_type"] = "password"
        params["client_id"] = self.client_id
        params["client_secret"] = self.client_secret
        # Request an access token by POSTing a request to the auth server.
        try:
            response = urllib2.urlopen(self.token_endpoint, urlencode(params))
        except HTTPError, he:
            if he.code == 400:
                error_body = json.loads(he.read())
                body = self.SERVER_ERROR_TEMPLATE\
                    .format(error_type=error_body["error"],
                            error_description=error_body["error_description"])
                return "400 Bad Request", body, {"Content-Type": "text/html"}
            if he.code == 401:
                return "302 Found", "", {"Location": "/login?failed=1"}
    def _request_token(self, env):
        """
        Retrieves a new access token from the OAuth2 server.
        """
        params = {}

        content = env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
        post_params = parse_qs(content)
        # Convert to dict for easier access
        for param, value in post_params.items():
            decoded_param = param.decode('utf-8')
            decoded_value = value[0].decode('utf-8')
            if decoded_param == "username" or decoded_param == "password":
                params[decoded_param] = decoded_value

        params["grant_type"] = "password"
        params["client_id"] = self.client_id
        params["client_secret"] = self.client_secret
        # Request an access token by POSTing a request to the auth server.
        try:
            response = urllib2.urlopen(self.token_endpoint, urlencode(params))
        except HTTPError, he:
            if he.code == 400:
                error_body = json.loads(he.read())
                body = self.SERVER_ERROR_TEMPLATE\
                    .format(error_type=error_body["error"],
                            error_description=error_body["error_description"])
                return "400 Bad Request", body, {"Content-Type": "text/html"}
            if he.code == 401:
                return "302 Found", "", {"Location": "/login?failed=1"}
Example #3
0
    def __init__(self, env):
        """
        :param env: Wsgi environment
        """
        self.method = env["REQUEST_METHOD"]
        self.query_params = {}
        self.query_string = env["QUERY_STRING"]
        self.path = env["PATH_INFO"]
        self.post_params = {}

        for param, value in parse_qs(env["QUERY_STRING"]).items():
            self.query_params[param] = value[0]

        if (self.method == "POST" and env["CONTENT_TYPE"]
                == "application/x-www-form-urlencoded"):
            self.post_params = {}
            content = env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
            post_params = parse_qs(content)

            for param, value in post_params.items():
                self.post_params[param] = value[0]
Example #4
0
    def __init__(self, env):
        """
        :param env: Wsgi environment
        """
        self.method       = env["REQUEST_METHOD"]
        self.query_params = {}
        self.query_string = env["QUERY_STRING"]
        self.path         = env["PATH_INFO"]
        self.post_params  = {}

        for param,value in parse_qs(env["QUERY_STRING"]).items():
            self.query_params[param] = value[0]

        if (self.method == "POST"
            and env["CONTENT_TYPE"] == "application/x-www-form-urlencoded"):
            self.post_params = {}
            content = env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
            post_params = parse_qs(content)

            for param,value in post_params.items():
                self.post_params[param] = value[0]
Example #5
0
    def __init__(self, env):
        """
        :param env: Wsgi environment
        """
        self.method = env["REQUEST_METHOD"]
        self.query_params = {}
        self.query_string = env["QUERY_STRING"]
        self.path = env["PATH_INFO"]
        self.post_params = {}
        self.env_raw = env

        for param, value in parse_qs(env["QUERY_STRING"]).items():
            self.query_params[param] = value[0]

        if self.method == "POST" and env["CONTENT_TYPE"] == "application/x-www-form-urlencoded":
            self.post_params = {}
            content = env["wsgi.input"].read(int(env["CONTENT_LENGTH"]))
            post_params = parse_qs(content)

            for param, value in post_params.items():
                decoded_param = param.decode("utf-8")
                decoded_value = value[0].decode("utf-8")
                self.post_params[decoded_param] = decoded_value