|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def _ensure_repo_root_on_syspath() -> None: |
| 11 | + repo_root = Path(__file__).resolve().parents[2] |
| 12 | + root = str(repo_root) |
| 13 | + if root not in sys.path: |
| 14 | + sys.path.insert(0, root) |
| 15 | + |
| 16 | + |
| 17 | +def run( |
| 18 | + tenant_id: str, |
| 19 | + client_id: str, |
| 20 | + client_secret: str, |
| 21 | + scope: str, |
| 22 | + supports_obo: bool, |
| 23 | + user_assertion: str | None, |
| 24 | + authority_host: str, |
| 25 | + authority_scheme: str, |
| 26 | + timeout_s: float, |
| 27 | +) -> dict[str, object]: |
| 28 | + _ensure_repo_root_on_syspath() |
| 29 | + from predicate_authority import ( # pylint: disable=import-error |
| 30 | + EntraCompatibilityConfig, |
| 31 | + EntraTenantCapabilities, |
| 32 | + run_entra_obo_compatibility_check, |
| 33 | + ) |
| 34 | + |
| 35 | + result = run_entra_obo_compatibility_check( |
| 36 | + config=EntraCompatibilityConfig( |
| 37 | + tenant_id=tenant_id, |
| 38 | + client_id=client_id, |
| 39 | + client_secret=client_secret, |
| 40 | + scope=scope, |
| 41 | + authority_host=authority_host, |
| 42 | + authority_scheme=authority_scheme, |
| 43 | + ), |
| 44 | + capabilities=EntraTenantCapabilities(supports_obo=supports_obo), |
| 45 | + user_assertion=user_assertion, |
| 46 | + timeout_s=timeout_s, |
| 47 | + ) |
| 48 | + result["delegation_path"] = ( |
| 49 | + "idp_obo_token_exchange" |
| 50 | + if bool(result.get("obo_ok", False)) |
| 51 | + else "authority_mandate_delegation" |
| 52 | + ) |
| 53 | + return result |
| 54 | + |
| 55 | + |
| 56 | +def main() -> None: |
| 57 | + parser = argparse.ArgumentParser( |
| 58 | + description="Entra OBO compatibility demo for delegation flow." |
| 59 | + ) |
| 60 | + parser.add_argument("--tenant-id", default=os.getenv("ENTRA_TENANT_ID")) |
| 61 | + parser.add_argument("--client-id", default=os.getenv("ENTRA_CLIENT_ID")) |
| 62 | + parser.add_argument("--client-secret", default=os.getenv("ENTRA_CLIENT_SECRET")) |
| 63 | + parser.add_argument( |
| 64 | + "--scope", default=os.getenv("ENTRA_SCOPE", "api://predicate-authority/.default") |
| 65 | + ) |
| 66 | + parser.add_argument("--user-assertion", default=os.getenv("ENTRA_USER_ASSERTION")) |
| 67 | + parser.add_argument( |
| 68 | + "--authority-host", default=os.getenv("ENTRA_AUTHORITY_HOST", "login.microsoftonline.com") |
| 69 | + ) |
| 70 | + parser.add_argument("--authority-scheme", default=os.getenv("ENTRA_AUTHORITY_SCHEME", "https")) |
| 71 | + parser.add_argument("--timeout-s", type=float, default=5.0) |
| 72 | + parser.add_argument("--supports-obo", action="store_true") |
| 73 | + args = parser.parse_args() |
| 74 | + |
| 75 | + missing = [ |
| 76 | + name |
| 77 | + for name, value in ( |
| 78 | + ("tenant_id", args.tenant_id), |
| 79 | + ("client_id", args.client_id), |
| 80 | + ("client_secret", args.client_secret), |
| 81 | + ("scope", args.scope), |
| 82 | + ) |
| 83 | + if value is None or str(value).strip() == "" |
| 84 | + ] |
| 85 | + if missing: |
| 86 | + raise SystemExit(f"Missing required arguments/env vars: {', '.join(missing)}") |
| 87 | + |
| 88 | + payload = run( |
| 89 | + tenant_id=str(args.tenant_id), |
| 90 | + client_id=str(args.client_id), |
| 91 | + client_secret=str(args.client_secret), |
| 92 | + scope=str(args.scope), |
| 93 | + supports_obo=bool(args.supports_obo), |
| 94 | + user_assertion=(str(args.user_assertion) if args.user_assertion is not None else None), |
| 95 | + authority_host=str(args.authority_host), |
| 96 | + authority_scheme=str(args.authority_scheme), |
| 97 | + timeout_s=float(args.timeout_s), |
| 98 | + ) |
| 99 | + print(json.dumps(payload, indent=2, sort_keys=True)) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments