示例#1
0
    async def test_token_request_from_json(self):
        token_request = await self.ably.auth.create_token_request(
            key_name=self.key_name, key_secret=self.key_secret)
        assert isinstance(token_request, TokenRequest)

        token_request_dict = token_request.to_dict()
        assert token_request == TokenRequest.from_json(token_request_dict)

        token_request_str = json.dumps(token_request_dict)
        assert token_request == TokenRequest.from_json(token_request_str)
示例#2
0
    def test_token_request_from_json(self):
        token_request = self.ably.auth.create_token_request(
            key_name=self.key_name, key_secret=self.key_secret)
        assert isinstance(token_request, TokenRequest)

        token_request_dict = token_request.to_dict()
        assert token_request == TokenRequest.from_json(token_request_dict)

        token_request_str = json.dumps(token_request_dict)
        assert token_request == TokenRequest.from_json(token_request_str)
示例#3
0
    def test_token_request_from_json(self):
        token_request = self.ably.auth.create_token_request(
            key_name=self.key_name, key_secret=self.key_secret)
        self.assertIsInstance(token_request, TokenRequest)

        token_request_dict = token_request.to_dict()
        self.assertEqual(
            token_request,
            TokenRequest.from_json(token_request_dict),
        )

        token_request_str = json.dumps(token_request_dict)
        self.assertEqual(
            token_request,
            TokenRequest.from_json(token_request_str),
        )
示例#4
0
    def request_token(
            self,
            token_params=None,
            # auth_options
            key_name=None,
            key_secret=None,
            auth_callback=None,
            auth_url=None,
            auth_method=None,
            auth_headers=None,
            auth_params=None,
            query_time=None):
        token_params = token_params or {}
        token_params = dict(self.auth_options.default_token_params,
                            **token_params)
        key_name = key_name or self.auth_options.key_name
        key_secret = key_secret or self.auth_options.key_secret

        log.debug("Auth callback: %s" % auth_callback)
        log.debug("Auth options: %s" % six.text_type(self.auth_options))
        if query_time is None:
            query_time = self.auth_options.query_time
        query_time = bool(query_time)
        auth_callback = auth_callback or self.auth_options.auth_callback
        auth_url = auth_url or self.auth_options.auth_url

        auth_params = auth_params or self.auth_options.auth_params or {}

        auth_method = (auth_method or self.auth_options.auth_method).upper()

        auth_headers = auth_headers or self.auth_options.auth_headers or {}

        log.debug("Token Params: %s" % token_params)
        if auth_callback:
            log.debug("using token auth with authCallback")
            token_request = auth_callback(token_params)
        elif auth_url:
            log.debug("using token auth with authUrl")

            token_request = self.token_request_from_auth_url(
                auth_method, auth_url, token_params, auth_headers, auth_params)
        else:
            token_request = self.create_token_request(token_params,
                                                      key_name=key_name,
                                                      key_secret=key_secret,
                                                      query_time=query_time)
        if isinstance(token_request, TokenDetails):
            return token_request
        elif isinstance(token_request, dict) and 'issued' in token_request:
            return TokenDetails.from_dict(token_request)
        elif isinstance(token_request, dict):
            token_request = TokenRequest.from_json(token_request)
        elif isinstance(token_request, six.text_type):
            return TokenDetails(token=token_request)
        # python2
        elif isinstance(token_request,
                        six.binary_type) and six.binary_type == str:
            return TokenDetails(token=token_request)

        token_path = "/keys/%s/requestToken" % token_request.key_name

        response = self.ably.http.post(token_path,
                                       headers=auth_headers,
                                       body=token_request.to_dict(),
                                       skip_auth=True)

        AblyException.raise_for_response(response)
        response_dict = response.to_native()
        log.debug("Token: %s" % str(response_dict.get("token")))
        return TokenDetails.from_dict(response_dict)