I’m trying to Dockerize a Python webapp that uses MSAL to handle authentication. I am acquiring the access token by using:
auth_response = public_app.acquire_token_interactive(scopes=user_scopes, port=5000)
token = auth_response['access_token']
The authentication runs smoothly whenever I test it locally. However, when I try to run my code in a Docker container, I get this error message:
Found no browser in current environment. If this program is being run
inside a container which either (1) has access to host network (i.e.
started bydocker run --net=host -it ...
), or (2) published port
5000 to host network (i.e. started bydocker run -p 127.0.0.1:5000:5000 -it ...
), you can use browser on host to visit the following link. Otherwise, this auth attempt would either timeout
(current timeout setting is None) or be aborted by CTRL+C. Auth
URI:…
`
When I click on the auth uri generated in the log, I am able to login as normal, and the Docker-ized application runs perfectly. How can I redirect to the generated Auth URI? MSAL uses webbrowser.get()
to normally redirect.
2
Answers
Somewhat randomly, we just had this same need arise in our environment–using
msal-node
rather than python, but the same interactive browser flow.To accomplish it, I wrote this utility: https://github.com/sam-mfb/oauth2-forwarder
More details are on the project, but basically it uses a client-server tcp connection to forward the client/container’s browser request out to the host and then send the response back into the container.
You might find it useful for your situation. If you need help feel free to raise an issue on the repo.
For the acquire_token_interactive I was also searching for a solution. According to this github thread this is the official way to go:
Source: https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/422
However I solved the issue with the device code flow instead as described here:
https://learn.microsoft.com/en-us/entra/msal/python/getting-started/acquiring-tokens
I just show the the usercode and the link from the response:
So the user can click on the link and just type in the code. Additionally I show a QR code that contains the URL if anyone feels more comfortable typing that on the phone.
Hope that helps.