Ejemplo n.º 1
0
    def test_create(self):
        from sanction.credentials import credentials_factory
        data = {
            "access_token": "test_token",
            "token_type": "Bearer",
            "expires_in": 3600,
            "refresh_token": "test_refresh"
        }
        c = credentials_factory("Bearer", "invalid", data)
        self.assertEquals(c.access_token, data["access_token"])

        self.assertEquals(c.http_header(), {"Authorization": "Bearer %s" % 
            data["access_token"]})

        self.assertEquals(c.query_param(), "access_token=%s" %
           data["access_token"])

        c.access_token = "foo"
        self.assertEquals(c.access_token, "foo")

        self.assertEquals(c.refresh_token, data["refresh_token"])
        c.refresh_token = "foo"
        self.assertEquals(c.refresh_token, "foo")

        try:
            c = credentials_factory("foo", "invalid", data)
            self.fail()

        except NotImplementedError: pass
Ejemplo n.º 2
0
    def authorization_received(self, data, expected_state=None):
        if "code" in data:
            if expected_state is not None:
                if expected_state != data["state"]:
                    raise InvalidStateError("Expected %s, got %s." % (
                        expected_state, data["state"]))

            o = self.parse_access_token(self.adapter.service.request(
                self.adapter.token_endpoint, body=urlencode({
                    "code": data["code"],
                    "client_id": self.client_id,
                    "client_secret": self.client_secret,
                    "redirect_uri": self.redirect_uri,
                    "grant_type": "authorization_code"
                }), method="POST", headers={"Content-type":
                "application/x-www-form-urlencoded"}))

            self.adapter.credentials = credentials_factory(o["token_type"],
                self.adapter.name, o)
            return self.adapter.credentials 

        elif "error" in data:
            raise exception_factory(data["error"], data)

        raise Exception("Unhandled authorization data received")
Ejemplo n.º 3
0
    def refresh_token(self):
        assert(self.adapter.credentials.refresh_token is not None)

        o = self.parse_access_token(self.adapter.service.request(
            self.adapter.token_endpoint, body=urlencode({
                    "grant_type": "refresh_token",
                    "refresh_token": self.adapter.credentials.refresh_token
                }), method="POST", headers={"Content-type":
                "application/x-www-form-urlencoded"}))

        self.adapter.credentials = credentials_factory(o["token_type"],
            self.adapter.name, o)

        return self.adapter.credentials
Ejemplo n.º 4
0
    def test_context(self):
        from sanction.credentials import BearerCredentials
        from sanction.credentials import credentials_factory

        class ContextCredentials(BearerCredentials):
            context = "my_context"
            token_type = "Bearer"

        c = credentials_factory("Bearer", "my_context", {
            "access_token":"test_token",
            "token_type":"Bearer",
            "expires_in":3600
        })

        self.assertTrue(isinstance(c, ContextCredentials))