def create_token_request(self, token_params=None, key_name=None, key_secret=None, query_time=None): token_params = token_params or {} token_request = {} key_name = key_name or self.auth_options.key_name key_secret = key_secret or self.auth_options.key_secret if not key_name or not key_secret: log.debug('key_name or key_secret blank') raise AblyException( "No key specified: no means to generate a token", 401, 40101) token_request['key_name'] = key_name if token_params.get('timestamp'): token_request['timestamp'] = token_params['timestamp'] else: if query_time is None: query_time = self.auth_options.query_time if query_time: token_request['timestamp'] = self.ably.time() else: token_request['timestamp'] = self._timestamp() token_request['timestamp'] = int(token_request['timestamp']) ttl = token_params.get('ttl') if ttl is not None: if isinstance(ttl, timedelta): ttl = ttl.total_seconds() * 1000 token_request['ttl'] = int(ttl) capability = token_params.get('capability') if capability is not None: token_request['capability'] = six.text_type(Capability(capability)) token_request["client_id"] = (token_params.get('client_id') or self.client_id) # Note: There is no expectation that the client # specifies the nonce; this is done by the library # However, this can be overridden by the client # simply for testing purposes token_request["nonce"] = token_params.get( 'nonce') or self._random_nonce() token_request = TokenRequest(**token_request) if token_params.get('mac') is None: # Note: There is no expectation that the client # specifies the mac; this is done by the library # However, this can be overridden by the client # simply for testing purposes. token_request.sign_request(key_secret.encode('utf8')) else: token_request.mac = token_params['mac'] return token_request
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)
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)
def create_token_request(self, token_params=None, key_name=None, key_secret=None, query_time=None): token_params = token_params or {} token_request = {} key_name = key_name or self.auth_options.key_name key_secret = key_secret or self.auth_options.key_secret if not key_name or not key_secret: log.debug('key_name or key_secret blank') raise AblyException("No key specified: no means to generate a token", 401, 40101) token_request['key_name'] = key_name if token_params.get('timestamp'): token_request['timestamp'] = token_params['timestamp'] else: if query_time is None: query_time = self.auth_options.query_time if query_time: token_request['timestamp'] = self.ably.time() else: token_request['timestamp'] = self._timestamp() token_request['timestamp'] = int(token_request['timestamp']) ttl = token_params.get('ttl') if ttl is not None: if isinstance(ttl, timedelta): ttl = ttl.total_seconds() * 1000 token_request['ttl'] = int(ttl) capability = token_params.get('capability') if capability is not None: token_request['capability'] = six.text_type(Capability(capability)) token_request["client_id"] = ( token_params.get('client_id') or self.client_id) # Note: There is no expectation that the client # specifies the nonce; this is done by the library # However, this can be overridden by the client # simply for testing purposes token_request["nonce"] = token_params.get('nonce') or self._random_nonce() token_request = TokenRequest(**token_request) if token_params.get('mac') is None: # Note: There is no expectation that the client # specifies the mac; this is done by the library # However, this can be overridden by the client # simply for testing purposes. token_request.sign_request(key_secret.encode('utf8')) else: token_request.mac = token_params['mac'] return token_request
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), )
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 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)