Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auth code flow in WSL Ubuntu 18.04 #332

Closed
jiasli opened this issue Mar 27, 2021 · 4 comments · Fixed by #333
Closed

Support auth code flow in WSL Ubuntu 18.04 #332

jiasli opened this issue Mar 27, 2021 · 4 comments · Fixed by #333

Comments

@jiasli
Copy link
Contributor

jiasli commented Mar 27, 2021

It will be good for MSAL to support auth code flow in WSL Ubuntu 18.04.

webbrowser doesn't work in WSL Ubuntu 18.04 due to the lack of www-browser:

$ python3 -V
Python 3.6.9
$ python3 -c "import webbrowser; print(webbrowser.open('https://microsoft.com'))"
False
$ python3 -c "import webbrowser; print(webbrowser.get())"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/webbrowser.py", line 51, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser
$ which www-browser
$

WSL Ubuntu 20.04 already supports this:

$ python3 -V
Python 3.8.5
$ python3 -c "import webbrowser; print(webbrowser.get().name)"
www-browser
$ which www-browser
/usr/bin/www-browser

Azure CLI supports WSL Ubuntu 18.04 by

https://github.com/Azure/azure-cli/blob/3bff786b9e337aa35bb4cc64a09e5600a35663f8/src/azure-cli-core/azure/cli/core/util.py#L637-L648

def open_page_in_browser(url):
    import subprocess
    import webbrowser
    platform_name, _ = _get_platform_info()


    if is_wsl():   # windows 10 linux subsystem
        try:
            # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
            # Ampersand (&) should be quoted
            return subprocess.call(['powershell.exe', '-Command', 'Start-Process "{}"'.format(url)])
        except OSError:  # WSL might be too old  # FileNotFoundError introduced in Python 3
            pass

Without this functionality, Azure CLI will have a regression when using MSAL.

@jiasli
Copy link
Contributor Author

jiasli commented Mar 27, 2021

Test script:

import msal

app = msal.PublicClientApplication('04b07795-8ddb-461a-bbee-02f9e1bf7b46', authority='https://login.microsoftonline.com/organizations')
app.acquire_token_interactive(['https://management.azure.com/.default'])

Error:

$ python test.py
Found no browser in current environment. If this program is being run inside a container which has access to host network (i.e. started by `docker run --net=host -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: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?...

@rayluo
Copy link
Collaborator

rayluo commented Mar 29, 2021

@jiasli , how reliable is that "...Start-Process url" approach? Also, isn't that is_wsl() also catch WSL2? On WSL2, is there any difference between the "... Start-Process url" approach and the Python native webbrowser approach?

@jiasli
Copy link
Contributor Author

jiasli commented Mar 29, 2021

Thanks @rayluo for the rapid response.

how reliable is that "...Start-Process url" approach?

Start-Process url approach should be reliable because powershell.exe is available since Windows 7. WSL is available since Windows 10, so powershell.exe must exist on the system. At least, it has been proven to be very reliable with Azure CLI. 🤣

Also, isn't that is_wsl() also catch WSL2?

Yes, detecting WSL 2 with is_wsl() is added by Azure/azure-cli#16556.

On WSL2, is there any difference between the "... Start-Process url" approach and the Python native webbrowser approach?

The problem is not regarding WSL 1 or WSL 2, but whether the Ubuntu image has www-browser.

  • Python native webbrowser approach internally calls www-browser.
  • If www-browser doesn't exist on the system, we fall back to calling powershell.exe with Start-Process url.

@jiasli
Copy link
Contributor Author

jiasli commented Mar 29, 2021

The help message of www-browser reveals more info:

$ www-browser --help
www-browser - Part of wslu, a collection of utilities for Windows 10 Windows Subsystem for Linux
Usage: wslview (--register|--unregister|--help|--version) [LINK]

For more help for www-browser, visit the following site: https://github.com/wslutilities/wslu/wiki/www-browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants