diff --git a/.gitignore b/.gitignore index bd3f730..052a52c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ prototype_*.py *.secret *.client_secret +# Environment files +*.env + # Certificate files *.pem *.key diff --git a/.vscode/settings.json b/.vscode/settings.json index eb415e3..9be0d9f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,6 @@ "debug.autoExpandLazyVariables": "off", "debug.inlineValues": "off", "debugpy.debugJustMyCode": true, - "debugpy.showPythonInlineValues": false + "debugpy.showPythonInlineValues": false, + "python.terminal.useEnvFile": true } diff --git a/get-token.py b/get-token.py index dbfc951..6c45c10 100755 --- a/get-token.py +++ b/get-token.py @@ -12,17 +12,38 @@ Usage: Now you can use the ADO_TOKEN environment variable, for example using curl: - curl -H "Authorization: Bearer $ADO_TOKEN" https://dev.azure.com/your_organization/_apis/projects?api-version=7.1 + curl -sH "Authorization: Bearer $ADO_TOKEN" "https://dev.azure.com/$ADO_ORGANIZATION_URL/_apis/projects?api-version=7.1" """ from sk.azure import get_token +from argparse import ArgumentParser -token = get_token( - tenant_id="a7740229-47b6-45de-ad22-83721462b1bf", - client_id="840671c4-5dc4-40e5-aab9-7c3a07bbd652", - pem_path="./slawek-mba.pem" -) +args = ArgumentParser(description="Get Azure DevOps token and print it for exporting as environment variable.") +args.add_argument("--tenant-id", type=str, required=True, help="Azure AD Tenant ID") +args.add_argument("--client-id", type=str, required=True, help="Azure AD Client ID") +args.add_argument("--pem-path", type=str, help="Path to PEM file for authentication (optional)") +args.add_argument("--client-secret", type=str, help="Client Secret for authentication (optional)") +args = args.parse_args() -# print(f"Obtained token: {token}") +if args.pem_path: + token = get_token( + tenant_id=args.tenant_id, + client_id=args.client_id, + pem_path=args.pem_path + ) +elif args.client_secret: + if not args.client_secret: + print("Client secret file is empty.") + exit(1) + token = get_token( + tenant_id=args.tenant_id, + client_id=args.client_id, + client_secret=args.client_secret + ) +else: + token = get_token( + tenant_id=args.tenant_id, + client_id=args.client_id + ) -print(f"export ADO_TOKEN='{token}'") \ No newline at end of file +print(f"export ADO_TOKEN='{token}'")