def test_adal_authentication(self):
        def success_auth():
            return {'tokenType': 'https', 'accessToken': 'cryptictoken'}

        credentials = AdalAuthentication(success_auth)
        session = credentials.signed_session()
        self.assertEqual(session.headers['Authorization'],
                         'https cryptictoken')

        def error():
            raise adal.AdalError("You hacker", {})

        credentials = AdalAuthentication(error)
        with self.assertRaises(AuthenticationError) as cm:
            session = credentials.signed_session()

        def expired():
            raise adal.AdalError("Too late",
                                 {'error_description': "AADSTS70008: Expired"})

        credentials = AdalAuthentication(expired)
        with self.assertRaises(TokenExpiredError) as cm:
            session = credentials.signed_session()

        def connection_error():
            raise ConnectionError("Plug the network")

        credentials = AdalAuthentication(connection_error)
        with self.assertRaises(AuthenticationError) as cm:
            session = credentials.signed_session()
Exemple #2
0
def get_access_tokens(tenant_id, client_id, client_secret):
    context = adal.AuthenticationContext(f'https://login.microsoftonline.com/{tenant_id}', validate_authority=True)
    cred1 = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        'https://management.core.windows.net/',
        client_id,
        client_secret)
    cred2 = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d',
        client_id,
        client_secret)
    return \
        cred1.signed_session(None).headers.get(cred1.header).replace('Bearer ', ''), \
        cred2.signed_session(None).headers.get(cred2.header).replace('Bearer ', '')