def test_token_auth(self): token = {"my_token": 123} auth = OAuthTokenAuthentication("client_id", token) session = auth.signed_session() self.assertEqual(session.token, token)
def test_token_auth(self): token = {"my_token":123} auth = OAuthTokenAuthentication("client_id", token) session = auth.signed_session() self.assertEqual(session.token, token)
def test_token_auth(self): token = {'access_token': '123456789'} auth = OAuthTokenAuthentication("client_id", token) session = auth.signed_session() request = PreparedRequest() request.prepare("GET", "https://example.org") session.auth(request) assert request.headers == {'Authorization': 'Bearer 123456789'}
def test_credential_headers(self): httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint", body='[{"title": "Test Data"}]', content_type="application/json") token = { 'access_token': 'eswfld123kjhn1v5423', 'refresh_token': 'asdfkljh23490sdf', 'token_type': 'Bearer', 'expires_in': '3600', } creds = OAuthTokenAuthentication("client_id", token) cfg = Configuration("https://my_service.com") client = ServiceClient(creds, cfg) def hook(aptr, req, *args, **kwargs): self.assertTrue('Authorization' in req.headers) self.assertEqual(req.headers['Authorization'], 'Bearer eswfld123kjhn1v5423') client.add_hook('request', hook) url = client.format_url("/get_endpoint") request = client.get(url, {'check': True}) response = client.send(request) check = httpretty.last_request() self.assertEqual(response.json(), [{"title": "Test Data"}]) token['expires_in'] = '-30' creds = OAuthTokenAuthentication("client_id", token) client = ServiceClient(creds, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check': True}) with self.assertRaises(TokenExpiredError): response = client.send(request)
def test_credential_headers(self): httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint", body='[{"title": "Test Data"}]', content_type="application/json") token = { 'access_token': 'eswfld123kjhn1v5423', 'refresh_token': 'asdfkljh23490sdf', 'token_type': 'Bearer', 'expires_in': '3600', } cfg = Configuration("https://my_service.com") cfg.credentials = OAuthTokenAuthentication("client_id", token) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert 'Authorization' in response.request.headers assert response.request.headers['Authorization'] == 'Bearer eswfld123kjhn1v5423' httpretty.has_request() assert response.json() == [{"title": "Test Data"}] # Expiration test token['expires_in'] = '-30' cfg.credentials = OAuthTokenAuthentication("client_id", token) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) with pytest.raises(TokenExpiredError): response = client.send(request)
) # The pattern to acquire a token looks like this. result = None # Firstly, looks up a token from cache # Since we are looking for token for the current app, NOT for an end user, # notice we give account parameter as None. result = app.acquire_token_silent(config["scope"], account=None) if not result: logging.info( "No suitable token exists in cache. Let's get a new one from AAD.") result = app.acquire_token_for_client(scopes=config["scope"]) credentials = OAuthTokenAuthentication(config['clientId'], result['access_token']) try: client = adtdataplane.AzureDigitalTwinsAPI(credentials=credentials, base_url=baseUrl) logging.info("Service client created – ready to go") except ValueError as err: print('Client creation failed with error: {0}'.format(err)) # load models # Load model file with open(r"models\SampleModel.json") as f: dtdl = json.load(f) dtdl_list = [] dtdl_list.append(dtdl) try: