2"""Prototype: invoked by the desktop environment (via a .desktop file
3registered as the handler for x-scheme-handler/ms-appx-web) when a browser
4hands off a ms-appx-web:// navigation to the OS.
6Pulls the freerdp_id out of the *state* query parameter (Azure wants a
7redirect_uri that is exactly ms-appx-web://Microsoft.AAD.BrokerPlugin/<client_id> -
8but state is an app-opaque value the IdP just echoes back verbatim, so the helper prefixes
9it with "<freerdp_id>." before sending the user to the IdP) and forwards the
10whole URL to that session's local listener over a unix socket named
11freerdp_aad_<freerdp_id>.
14 ms-appx-web://Microsoft.AAD.BrokerPlugin/<client_id>?code=...&state=<freerdp_id>.<original_state>
23 if len(sys.argv) != 2:
24 print(f
"usage: {sys.argv[0]} <url>", file=sys.stderr)
28 parsed = urllib.parse.urlparse(url)
34 if parsed.netloc.lower() ==
"microsoft.aad.brokerplugin":
35 state_values = urllib.parse.parse_qs(parsed.query).get(
"state")
37 print(f
"no state parameter found in {url!r}", file=sys.stderr)
40 freerdp_id = state_values[0].split(
".", 1)[0]
41 runtime_dir = os.environ.get(
"XDG_RUNTIME_DIR",
"/tmp")
42 sock_path = os.path.join(runtime_dir, f
"freerdp_aad_{freerdp_id}.sock")
45 with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
as sock:
46 sock.connect(sock_path)
47 sock.sendall(url.encode() + b
"\n")
48 except OSError
as exc:
49 print(f
"could not reach {sock_path}: {exc}", file=sys.stderr)
55if __name__ ==
"__main__":
56 raise SystemExit(main())