File: create_user_and_phone.py

package info (click to toggle)
python-duo-client 5.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: python: 7,105; sh: 6; makefile: 4
file content (61 lines) | stat: -rwxr-xr-x 1,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import pprint
import sys

import duo_client

argv_iter = iter(sys.argv[1:])
def get_next_arg(prompt):
    try:
        return next(argv_iter)
    except StopIteration:
        return input(prompt)

# 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: '),
    host=get_next_arg('API hostname ("api-....duosecurity.com"): '),
)

USERNAME = get_next_arg('user login name: ')
REALNAME = get_next_arg('user full name: ')

# Refer to http://www.duosecurity.com/docs/adminapi for more
# information about phone types and platforms.
PHONE_NUMBER = get_next_arg('phone number (e.g. +1-555-123-4567): ')
PHONE_TYPE = get_next_arg('phone type (e.g. mobile): ')
PHONE_PLATFORM = get_next_arg('phone platform (e.g. google android): ')

# Create and return a new user object.
user = admin_api.add_user(
    username=USERNAME,
    realname=REALNAME,
)
print('Created user:')
pprint.pprint(user)

# Create and return a new phone object.
phone = admin_api.add_phone(
    number=PHONE_NUMBER,
    type=PHONE_TYPE,
    platform=PHONE_PLATFORM,
)
print('Created phone:')
pprint.pprint(phone)

# Associate the user with the phone.
admin_api.add_user_phone(
    user_id=user['user_id'],
    phone_id=phone['phone_id'],
)
print('Added phone', phone['number'], 'to user', user['username'])

# Send two SMS messages to the phone with information about installing
# the app for PHONE_PLATFORM and activating it with this Duo account.
act_sent = admin_api.send_sms_activation_to_phone(
    phone_id=phone['phone_id'],
    install='1',
)
print('SMS activation sent to', phone['number'] + ':')
pprint.pprint(act_sent)