Skip to content

Commit

Permalink
fix: check account index in interactive mode
Browse files Browse the repository at this point in the history
When performing an operation that requires an account, check if the
provided account index is valid. If not, return a json encoded error
message and an error code. Further, issue a warning on stderr if no
accounts are registered.

Xref: #42
Signed-off-by: Felix Moessbauer <[email protected]>
  • Loading branch information
fmoessbauer committed Nov 4, 2024
1 parent 55a3791 commit 633654a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions linux-entra-sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ def register_terminate_with_parent():


def run_interactive():
def _get_account(accounts, idx):
try:
return accounts['accounts'][idx]
except IndexError:
json.dump({"error": f"invalid account index {idx}"},
indent=2, fp=sys.stdout)
print()
sys.exit(1)

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interactive", action="store_true",
help="run in interactive mode")
Expand All @@ -275,16 +284,19 @@ def run_interactive():
return

accounts = ssomib.get_accounts()
if len(accounts['accounts']) == 0:
print("warning: no accounts registered.", file=sys.stderr)

if args.command == 'getAccounts':
json.dump(accounts, indent=2, fp=sys.stdout)
elif args.command == 'getVersion':
json.dump(ssomib.get_broker_version(), indent=2, fp=sys.stdout)
elif args.command == "acquirePrtSsoCookie":
account = accounts['accounts'][args.account]
account = _get_account(accounts, args.account)
cookie = ssomib.acquire_prt_sso_cookie(account, args.ssoUrl)
json.dump(cookie, indent=2, fp=sys.stdout)
elif args.command == "acquireTokenSilently":
account = accounts['accounts'][args.account]
account = _get_account(accounts, args.account)
token = ssomib.acquire_token_silently(account)
json.dump(token, indent=2, fp=sys.stdout)
# add newline
Expand Down

0 comments on commit 633654a

Please sign in to comment.