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

doc: add report_user_by_email.py to examples #242

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/report_user_by_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

""" Script to illustrate how to retrieve a user from the Duo Admin API using the associated email address"""

from __future__ import absolute_import, print_function
import sys
import getpass
from pprint import pprint

import duo_client
from six.moves import input

argv_iter = iter(sys.argv[1:])


def get_next_arg(prompt, secure=False):
"""Read information from STDIN, using getpass when sensitive information should not be echoed to tty"""
try:
return next(argv_iter)
except StopIteration:
if secure is True:
return getpass.getpass(prompt)
else:
return input(prompt)


def main():
""" Primary script execution code """
# Configuration and information about objects to create.
admin_api = duo_client.Admin(
ikey=get_next_arg('Admin API integration key ("DI..."): '),
skey=get_next_arg('integration secret key: ', secure=True),
host=get_next_arg('API hostname ("api-....duosecurity.com"): '),
)

# Retrieve user info from API:
email_address = get_next_arg('E-mail address of user to retrieve: ')
req_params = {"email": email_address}
# There is no get_user_by_email in the duo_client_python library, so we use the generic api_call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a function in there? I don't know if this a common thing that people might want.

Copy link
Contributor Author

@MarkTripod-Duo MarkTripod-Duo Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new method to the library specifically for this use case should be rather trivial. I do not have any data on generalized API usage for user searches via email address to offer an opinion on whether a separate method is warranted. Perhaps adding a new optional parameter to the get_users() method to specify the email address could be an alternate possible update to the library. However, adding a get_user_by_email() method would maintain consistency with other methods available.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote let's add a get_user_by_email method - I agree it's easy and consistent. Do you want to handle that Mark?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New PR submitted.

user = admin_api.json_api_call(
method='GET',
path='/admin/v1/users',
params=req_params
)

if user:
pprint(user, indent=2)
else:
print(f"User with email [{email_address}] could not be found.")


if __name__ == '__main__':
main()
Loading