duo_library.py
2.64 KB
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
62
63
64
65
66
67
#! /usr/bin/env python3
#
# duo_library.py - simple DUO Python library client template
#
# Version 1.0, latest version, documentation and bugtracker available at:
# https://gitlab.lindenaar.net/scripts/duo
#
# Copyright (c) 2019 Frederik Lindenaar
#
# This script is free software: you can redistribute and/or modify it under the
# terms of version 3 of the GNU General Public License as published by the Free
# Software Foundation, or (at your option) any later version of the license.
#
# This script is distributed in the hope that it will be useful but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, visit <http://www.gnu.org/licenses/> to download it.
#
""" simple DUO Python library client template.
This python script uses the DUO API library for python to perform some
simple requests to test it and as template to implement a simple client
using this library. The logic implemented in this file matches that of
the pure Python3 implementation of the API client in duo_api.py to compare
them and decide how best to integrate with DUO in your environment.
To run this example:
1. create a virtualenv in this directory with: virtualenv .
2. activate the virtualenv with: . ./bin/activate
3. install the required libraries with: pip install -r requirements.txt
4. update this script and provide your API host, ikey and skey below
5. run this script
See https://duo.com/docs/authapi and https://duo.com/docs/adminapi for the
available endpoints, parameters and responses. For the Python library and
its documentation see: https://github.com/duosecurity/duo_client_python
"""
import duo_client as DUO
# Main logic to execute is case this is the called script
if __name__ == '__main__':
# Instantiate DUO Auth API class, make sure you set parameters below!
host='api-XXXXXXXX.duosecurity.com'
ikey='XXXXXXXXXXXXXXXXXXXX'
skey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
duo_auth_api = DUO.Auth(host=host, ikey=ikey, skey=skey)
# Simple example, check the connectivity
response = duo_auth_api.check()
print(response)
# More complex example, request DUO token authorization with parameters
response = duo_auth_api.auth(
username='username',
factor='push',
device='auto',
type='Network Access',
display_username='Test User',
pushinfo='explanation=Text+section(s)+shown+to+the+user&mode=TEST')
print(response)