def __init__(self, ably, options): self.__ably = ably self.__auth_options = options if options.token_details: self.__client_id = options.token_details.client_id else: self.__client_id = options.client_id self.__client_id_validated = False self.__basic_credentials = None self.__auth_params = None self.__token_details = None self.__time_offset = None must_use_token_auth = options.use_token_auth is True must_not_use_token_auth = options.use_token_auth is False can_use_basic_auth = options.key_secret is not None and options.client_id is None if not must_use_token_auth and can_use_basic_auth: # We have the key, no need to authenticate the client # default to using basic auth log.debug("anonymous, using basic auth") self.__auth_mechanism = Auth.Method.BASIC basic_key = "%s:%s" % (options.key_name, options.key_secret) basic_key = base64.b64encode(basic_key.encode('utf-8')) self.__basic_credentials = basic_key.decode('ascii') return elif must_not_use_token_auth and not can_use_basic_auth: raise ValueError( 'If use_token_auth is False you must provide a key') # Using token auth self.__auth_mechanism = Auth.Method.TOKEN if options.token_details: self.__token_details = options.token_details elif options.auth_token: self.__token_details = TokenDetails(token=options.auth_token) else: self.__token_details = None if options.auth_callback: log.debug("using token auth with auth_callback") elif options.auth_url: log.debug("using token auth with auth_url") elif options.key_secret: log.debug("using token auth with client-side signing") elif options.auth_token: log.debug("using token auth with supplied token only") elif options.token_details: log.debug("using token auth with supplied token_details") else: raise ValueError( "Can't authenticate via token, must provide " "auth_callback, auth_url, key, token or a TokenDetail")
def test_when_not_renewable_with_token_details(self): token_details = TokenDetails(token='a_dummy_token') self.ably = RestSetup.get_ably_rest(key=None, token_details=token_details, use_binary_protocol=False) self.ably.channels[self.channel].publish('evt', 'msg') assert 1 == self.publish_attempts publish = self.ably.channels[self.channel].publish match = "The provided token is not renewable and there is no means to generate a new token" with pytest.raises(AblyAuthException, match=match): publish('evt', 'msg') assert 0 == self.token_requests
async def test_when_not_renewable_with_token_details(self): token_details = TokenDetails(token='a_dummy_token') self.ably = await RestSetup.get_ably_rest(key=None, token_details=token_details, use_binary_protocol=False) await self.ably.channels[self.channel].publish('evt', 'msg') assert self.mocked_api["publish_attempt_route"].call_count == 1 publish = self.ably.channels[self.channel].publish match = "The provided token is not renewable and there is no means to generate a new token" with pytest.raises(AblyAuthException, match=match): await publish('evt', 'msg') assert not self.mocked_api["request_token_route"].called
def test_when_not_renewable_with_token_details(self): token_details = TokenDetails(token='a_dummy_token') self.ably = AblyRest(token_details=token_details, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], use_binary_protocol=False) self.ably.channels[self.channel].publish('evt', 'msg') self.assertEquals(1, self.publish_attempts) publish = self.ably.channels[self.channel].publish self.assertRaisesRegexp( AblyAuthException, "The provided token is not renewable and there is" " no means to generate a new token", publish, 'evt', 'msg') self.assertEquals(0, self.token_requests)
def test_auth_token_details(self): td = TokenDetails() ably = AblyRest(token_details=td) self.assertEqual(Auth.Method.TOKEN, ably.auth.auth_mechanism) self.assertIs(ably.auth.token_details, td)
def callback(token_params): self.assertEquals(token_params, called_token_params) return TokenDetails(token='another_token_string')
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(**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, native_data=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)
def test_auth_token_details(self): td = TokenDetails() ably = AblyRest(token_details=td) assert Auth.Method.TOKEN == ably.auth.auth_mechanism assert ably.auth.token_details is td
def callback(token_params): assert token_params == called_token_params return TokenDetails(token='another_token_string')
def test_with_token_details(self): td = TokenDetails() ably = AblyRest(token_details=td) self.assertIs(ably.options.token_details, td)
def test_with_token_details(self): td = TokenDetails() ably = AblyRest(token_details=td) assert ably.options.token_details is td