Search code examples
pythonauthenticationazure-ad-msal

Why is python MSAL authentication stuck at ConfidentialClientApplication?


I have this bit of python code, which runs perfectly on my computer but gets stuck in this funcion when I run it on an EC2 with Amazon Linux. Checked all networks and firewall rules, everthing is fine. In fact, if I get a token from my PC and try to access sharepoint directly via cURL (without autentication) it actually works. What am I missing?

certfile = 'cert.pem'
thumbprint = 'XXX'
tenantID = 'XXX-XXX-XXX-XXX-XXX'
clientID = 'XXX-XXX-XXX-XXX-XXX'
scope = ['https://xxx.sharepoint.com/.default']
authority = f"https://login.microsoftonline.com/{tenantID}"

def msal_certificate_auth(clientID, scope, authority, thumbprint, certfile):      
    app = msal.ConfidentialClientApplication(clientID, authority=authority,
                                                client_credential={"thumbprint": thumbprint, "private_key": open(certfile).read()},
                                                azure_region=None                                                  ) 
    result = app.acquire_token_for_client(scopes=scope)
    return result

It hangs exactly at ConfidentialClientApplication but I am not sure how to debug deeper. Documentation over https://msal-python.readthedocs.io/en/latest/


Solution

  • Please check if the proxy is set as expected if behind the firewall. I was using following and it was stuck forever.

    msal.PublicClientApplication(config["client_id"], authority=config["authority"], validate_authority=False, verify=False)
    

    Proxy fixing did the trick.