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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811
|
"""
Duo Security Administration API reference client implementation.
<http://www.duosecurity.com/docs/adminapi>
USERS
User objects are returned in the following format:
{"username": <str:username>,
"user_id": <str:user id>,
"realname": <str:real name>,
"status": <str:status>,
"notes": <str:notes>,
"last_login": <int:unix timestamp>|None,
"phones": [<phone object>, ...],
"tokens": [<token object>, ...]}
User status is one of:
USER_STATUS_ACTIVE, USER_STATUS_BYPASS, USER_STATUS_DISABLED,
USER_STATUS_LOCKED_OUT
Note: USER_STATUS_LOCKED_OUT can only be set by the system. You cannot
set a user to be in the USER_STATUS_LOCKED_OUT state.
ENDPOINTS
Endpoint objects are returned in the following format:
{"username": <str:username>,
"email": <str:email>,
"epkey": <str:epkey>,
"os_family": <str:os family>,
"os_version": <str:os version>,
"model": <str:model>,
"type": <str:type>,
"browsers": [<browser object, ...]|None}
BROWSERS
Browser objects are returned in the following format:
{"browser_family": <str:browser family>,
"browser_version": <str:browser version>,
"flash_version": <str: flash version>,
"java_version": <str: java version>}
PHONES
Phone objects are returned in the following format:
{"phone_id": <str:phone id>,
"number": <str:phone number>,
"extension": <str:phone extension>|'',
"predelay": <str:predelay in seconds>|None,
"postdelay": <str:postdelay in seconds>|None,
"type": <str:phone type>|"Unknown",
"platform": <str:phone platform>|"Unknown",
"activated": <bool:is push enabled>,
"sms_passcodes_sent": <bool:have sms OTP been sent>,
"users": [<user object>, ...]}
DESKTOP_TOKENS
Desktop token objects are returned in the following format:
{"desktoptoken_id": <str:phone id>,
"name": <str>,
"platform": <str:phone platform>|"Unknown",
"type"": "Desktop Token",
"users": [<user object>, ...]}
TOKENS
Token objects are returned in the following format:
{"type": <str:token type>,
"serial": <str:token serial number>,
"token_id": <str:token id>,
"totp_step": <int:number of seconds> or null,
"users": [<user object>, ...]}
Token type is one of:
TOKEN_HOTP_6, TOKEN_HOTP_8, TOKEN_YUBIKEY
SETTINGS
Settings objects are returned in the following format:
{'inactive_user_expiration': <int:days until expiration>|0,
'pending_deletion_days': <int:days a user will be in pending deletion status>,
'sms_message': <str:sms message>,
'name': <str:name>,
'sms_batch': <int:sms batch size>,
'lockout_threshold': <int:lockout threshold>
'lockout_expire_duration': <int:minutes until expiration>|0,
'sms_expiration': <int:minutes until expiration>|0,
'log_retention_days': <int:days to retain>|0,
'sms_refresh': <bool:is sms refresh enabled (0|1)>,
'telephony_warning_min': <int:credits>',
'minimum_password_length': <int:minimum length>,
'password_requires_upper_alpha': <bool:is upper alpha character required>,
'password_requires_lower_alpha': <bool:is lower alpha character required>,
'password_requires_numeric': <bool:is numeric character required>,
'password_requires_special': <bool:is special character required>,
'security_checkup_enabled': <bool:is the security checkup feature enabled>,
'user_managers_can_put_users_in_bypass': <bool:can user managers put users in bypass status>,
'email_activity_notification_enabled': <bool:can users get activity notifications via email>,
'push_activity_notification_enabled': <bool:can users get activity notifications via Duo Mobile>,
'unenrolled_user_lockout_threshold': <int:days until a user will be locked out due to being unenrolled>,
'enrollment_universal_prompt_enabled': <bool:will email enrollment use Universal Prompt experience>,
}
INTEGRATIONS
Integration objects are returned in the following format:
{'adminapi_admins': <bool:admins permission (0|1)>,
'adminapi_info': <bool:info permission (0|1)>,
'adminapi_integrations': <bool:integrations permission (0|1)>,
'adminapi_read_log': <bool:read log permission (0|1)>,
'adminapi_read_resource': <bool:read resource permission (0|1)>,
'adminapi_settings': <bool:settings permission (0|1)>,
'adminapi_write_resource': <bool:write resource permission (0|1)>,
'self_service_allowed': <bool:self service permission (0|1)>,
'enroll_policy': <str:enroll policy (enroll|allow|deny)>,
'username_normalization_policy': <str:normalization policy (simple|none)>,
'greeting': <str:voice greeting>,
'integration_key': <str:integration key>,
'name': <str:integration name>,
'notes': <str:notes>,
'secret_key': <str:secret key>,
'type': <str:integration type>,
'visual_style': Deprecated; ignored if specified.}
See the adminapi docs for possible values for enroll_policy, ip_whitelist, and type.
ADMINISTRATIVE UNITS
Administrative unit objects are returned in the following format:
{'admin_unit_id': <str:administrative unit id>,
'name': <str:administrative unit name>,
'description': <str:administrative unit description>,
'restrict_by_groups': <bool:group restriction (0|1)>,
'restrict_by_integrations': <bool:integration restriction (0|1)>,
'admins': [<str:admin key>, ...],
'groups': [<str:group key>, ...],
'integrations': [<str:integration key>, ...],
}
ERRORS
Methods will raise a RuntimeError when an error is encountered. When
the call returns a HTTP status other than 200, error will be populated with
the following fields:
message - String description of the error encountered such as
"Received 404 Not Found"
status - HTTP status such as 404 (int)
reason - Status description such as "Not Found"
data - Detailed error code such as
{"code": 40401, "message": "Resource not found", "stat": "FAIL"}
"""
import base64
import json
import time
import urllib.parse
import warnings
from typing import List, Optional
from datetime import datetime, timedelta, timezone
from . import Accounts, client
from .logs.telephony import Telephony
USER_STATUS_ACTIVE = "active"
USER_STATUS_BYPASS = "bypass"
USER_STATUS_DISABLED = "disabled"
USER_STATUS_LOCKED_OUT = "locked out"
TOKEN_HOTP_6 = "h6"
TOKEN_HOTP_8 = "h8"
TOKEN_YUBIKEY = "yk"
VALID_AUTHLOG_REQUEST_PARAMS = [
"mintime",
"maxtime",
"limit",
"sort",
"next_offset",
"event_types",
"reasons",
"results",
"users",
"applications",
"groups",
"factors",
"api_version",
"assessment",
"detections",
]
VALID_ACTIVITY_REQUEST_PARAMS = ["mintime", "maxtime", "limit", "sort", "next_offset"]
class Admin(client.Client):
account_id = None
def api_call(self, method, path, params):
if self.account_id is not None:
params['account_id'] = self.account_id
return super(Admin, self).api_call(
method,
path,
params,
)
@classmethod
def _canonicalize_ip_whitelist(klass, ip_whitelist):
if isinstance(ip_whitelist, str):
return ip_whitelist
else:
return ','.join(ip_whitelist)
pass
@staticmethod
def _canonicalize_bypass_codes(codes):
if isinstance(codes, str):
return codes
else:
return ','.join([str(int(code)) for code in codes])
def get_administrative_units(self, admin_id=None, group_id=None,
integration_key=None, limit=None, offset=0):
"""
Retrieves a list of administrative units optionally filtered by admin,
group, or integration. At most one of admin_id, group_id, or
integration_key should be passed.
Args:
admin_id(str): id of admin (optional)
group_id(str): id of group (optional)
integration_key(str): id of integration (optional)
limit: The max number of administrative units to fetch at once.
Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of administrative units
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
params = {}
if admin_id is not None:
params['admin_id'] = admin_id
if group_id is not None:
params['group_id'] = group_id
if integration_key is not None:
params['integration_key'] = integration_key
if limit:
params['limit'] = limit
params['offset'] = offset
return self.json_api_call('GET',
'/admin/v1/administrative_units',
params)
iterator = self.get_administrative_units_iterator(
admin_id, group_id, integration_key)
return list(iterator)
def get_administrative_units_iterator(self, admin_id=None, group_id=None,
integration_key=None, ):
"""
Provides a generator which produces administrative_units. Under the
hood, this generator uses pagination, so it will only store one page of
administrative_units at a time in memory.
Returns: A generator which produces administrative_units.
Raises RuntimeError on error.
"""
params = {}
if admin_id is not None:
params['admin_id'] = admin_id
if group_id is not None:
params['group_id'] = group_id
if integration_key is not None:
params['integration_key'] = integration_key
return self.json_paging_api_call('GET',
'/admin/v1/administrative_units',
params)
def get_administrator_log(self,
mintime=0):
"""
Returns administrator log events.
mintime - Fetch events only >= mintime (to avoid duplicate
records that have already been fetched)
Returns:
[
{'timestamp': <int:unix timestamp>,
'eventtype': "administrator",
'host': <str:hostname>,
'username': <str:username>,
'action': <str:action>,
'object': <str:object name>|None,
'description': <str:description>|None}, ...
]
<action> is one of:
'admin_login',
'admin_create', 'admin_update', 'admin_delete',
'customer_update',
'group_create', 'group_update', 'group_delete',
'integration_create', 'integration_update', 'integration_delete',
'phone_create', 'phone_update', 'phone_delete',
'user_create', 'user_update', 'user_delete'
Raises RuntimeError on error.
"""
# Sanity check mintime as unix timestamp, then transform to string
mintime = str(int(mintime))
params = {
'mintime': mintime,
}
response = self.json_api_call(
'GET',
'/admin/v1/logs/administrator',
params,
)
for row in response:
row['eventtype'] = 'administrator'
row['host'] = self.host
return response
def get_offline_log(self,
mintime=0):
"""
Returns offline enrollment log events.
mintime - Fetch events only >= mintime (to avoid duplicate
records that have already been fetched)
Returns:
[
{'timestamp': <int:unix timestamp>,
'username': <str:username>,
'action': <str:action>,
'object': <str:object name>|None,
'description': <str:description>|None}, ...
]
<action> is one of:
'o2fa_user_provisioned',
'o2fa_user_deprovisioned',
'o2fa_user_reenrolled'
Raises RuntimeError on error.
"""
# Sanity check mintime as unix timestamp, then transform to string
mintime = str(int(mintime))
params = {
'mintime': mintime,
}
response = self.json_api_call(
'GET',
'/admin/v1/logs/offline_enrollment',
params,
)
return response
def get_authentication_log(self, api_version=1, **kwargs):
"""
Returns authentication log events.
api_version - The api version of the handler to use. Currently, the
default api version is v1, but the v1 api will be
deprecated in a future version of the Duo Admin API.
Please migrate to the v2 api at your earliest convenience.
For details on the differences between v1 and v2,
please see Duo's Admin API documentation. (Optional)
API Version v1:
mintime - Fetch events only >= mintime (to avoid duplicate
records that have already been fetched)
Returns:
[
{'timestamp': <int:unix timestamp>,
'eventtype': "authentication",
'host': <str:host>,
'username': <str:username>,
'factor': <str:factor>,
'result': <str:result>,
'ip': <str:ip address>,
'new_enrollment': <bool:if event corresponds to enrollment>,
'integration': <str:integration>,
'location': {
'state': '<str:state>',
'city': '<str:city>',
'country': '<str:country>'
}
}]
Raises RuntimeError on error.
API Version v2:
mintime (required) - Unix timestamp in ms; fetch records >= mintime
maxtime (required) - Unix timestamp in ms; fetch records <= maxtime
limit - Number of results to limit to
next_offset - Used to grab the next set of results from a previous response
sort - Sort order to be applied
users - List of user ids to filter on
groups - List of group ids to filter on
applications - List of application ids to filter on
results - List of results to filter to filter on
reasons - List of reasons to filter to filter on
factors - List of factors to filter on
event_types - List of event_types to filter on
Returns:
{
"authlogs": [
{
"access_device": {
"ip": <str:ip address>,
"location": {
"city": <str:city>,
"state": <str:state>,
"country": <str:country
}
},
"application": {
"key": <str:application id>,
"name": <str:application name>
},
"auth_device": {
"ip": <str:ip address>,
"location": {
"city": <str:city>,
"state": <str:state>,
"country": <str:country
},
"name": <str:device name>
},
"event_type": <str:type>,
"factor": <str:factor,
"reason": <str:reason>,
"result": <str:result>,
"timestamp": <int:unix timestamp>,
"user": {
"key": <str:user id>,
"name": <str:user name>
}
}
],
"metadata": {
"next_offset": [
<str>,
<str>
],
"total_objects": <int>
}
}
Raises RuntimeError on error.
"""
if api_version not in [1,2]:
raise ValueError("Invalid API Version")
params = {}
if api_version == 1: #v1
params['mintime'] = kwargs['mintime'] if 'mintime' in kwargs else 0;
# Sanity check mintime as unix timestamp, then transform to string
params['mintime'] = '{:d}'.format(int(params['mintime']))
warnings.warn(
'The v1 Admin API for retrieving authentication log events '
'will be deprecated in a future release of the Duo Admin API. '
'Please migrate to the v2 API.',
DeprecationWarning)
else: #v2
for k in kwargs:
if kwargs[k] is not None and k in VALID_AUTHLOG_REQUEST_PARAMS:
params[k] = kwargs[k]
if 'mintime' not in params:
params['mintime'] = (int(time.time()) - 86400) * 1000
# Sanity check mintime as unix timestamp, then transform to string
params['mintime'] = '{:d}'.format(int(params['mintime']))
if 'maxtime' not in params:
params['maxtime'] = int(time.time()) * 1000
# Sanity check maxtime as unix timestamp, then transform to string
params['maxtime'] = '{:d}'.format(int(params['maxtime']))
response = self.json_api_call(
'GET',
'/admin/v{}/logs/authentication'.format(api_version),
params,
)
if api_version == 1:
for row in response:
row['eventtype'] = 'authentication'
row['host'] = self.host
else:
for row in response['authlogs']:
row['eventtype'] = 'authentication'
row['host'] = self.host
return response
def get_activity_logs(self, **kwargs):
"""
Returns activity log events.
The activity endpoint is in public preview and subject to change.
mintime - Unix timestamp in ms; fetch records >= mintime
maxtime - Unix timestamp in ms; fetch records <= maxtime
limit - Number of results to limit to
next_offset - Used to grab the next set of results from a previous response
sort - Sort order to be applied
Returns:
{
"items": [
{
"access_device": {
"browser": <str:browser>,
"browser_version": <str:browser version>,
"ip": {
"address": <str:ip address>
},
"location": {
"city": <str:city>,
"country": <str:country>,
"state": <str:state>
},
"os": <str:os name>,
"os_version": <str:os_version>
},
"action": <str:action>,
"activity_id": <str:activity id>,
"actor": {
"details": <str:json for actor details>
"key" : <str:actor's key>,
"name": <str:actor's name>,
"type": <str:actor type>
},
"akey": <str:akey>,
"application": {
"key": <str:application's key>,
"name": <str:application's name>,
"type": <str:application's type>
},
"target": {
"details": <str:target's details if available> ,
"key" : <str:target's key>,
"name": <str:target's name>,
"type": <str:target's type>
},
"ts": <str:timestamp captured for activity>
},
],
"metadata": {
"next_offset": <str: comma seperated ts and offset value>
"total_objects": {
"relation" : <str: relational operator>
"value" : <int: total objects in the time range>
}
}
}
Raises RuntimeError on error.
"""
params = {}
today = datetime.now(tz=timezone.utc)
default_maxtime = int(today.timestamp() * 1000)
default_mintime = int((today - timedelta(days=180)).timestamp() * 1000)
for k in kwargs:
if kwargs[k] is not None and k in VALID_ACTIVITY_REQUEST_PARAMS:
params[k] = kwargs[k]
if 'mintime' not in params:
# If mintime is not provided, the script defaults it to 180 days in past
params['mintime'] = default_mintime
params['mintime'] = str(int(params['mintime']))
if 'maxtime' not in params:
#if maxtime is not provided, the script defaults it to now
params['maxtime'] = default_maxtime
params['maxtime'] = str(int(params['maxtime']))
if 'limit' in params:
params['limit'] = str(int(params['limit']))
response = self.json_api_call(
'GET',
'/admin/v2/logs/activity',
params,
)
for row in response['items']:
row['eventtype'] = 'activity'
row['host'] = self.host
return response
def get_telephony_log(self, mintime=0, api_version=1, **kwargs):
"""
Returns telephony log events.
mintime - Fetch events only >= mintime (to avoid duplicate
records that have already been fetched)
api_version - The API version of the handler to use.
Currently, the default api version is v1, but the v1 API
will be deprecated in a future version of the Duo Admin API.
Please migrate to the v2 api at your earliest convenience.
For details on the differences between v1 and v2,
please see Duo's Admin API documentation. (Optional)
v1 Returns:
[
{
'timestamp': <int:unix timestamp>,
'eventtype': "telephony",
'host': <str:host>,
'context': <str:context>,
'type': <str:type>,
'phone': <str:phone number>,
'credits': <str:credits>}
]
v2 Returns:
{
"items": [
{
'context': <str>,
'credits': <int: credits used>,
'phone': <str:phone number>,
'telephony_id': <str:UUID>,
'ts': <str:ISO timestamp>,
'txid': <str:UUID>,
'type': <str:"sms" or "phone">,
'eventtype': <str:"telephony">,
'host': <str:application hostname>
}
],
"metadata": {
"next_offset": <str: comma seperated ts and offset value>
"total_objects": {
"relation" : <str: relational operator>
"value" : <int: total objects in the time range>
}
}
}
Raises RuntimeError on error.
"""
if api_version not in [1,2]:
raise ValueError("Invalid API Version")
if api_version == 2:
return Telephony.get_telephony_logs_v2(self.json_api_call, self.host, **kwargs)
return Telephony.get_telephony_logs_v1(self.json_api_call, self.host, mintime=mintime)
def get_users_iterator(self):
"""
Returns iterator of user objects.
Raises RuntimeError on error.
"""
return self.json_paging_api_call('GET', '/admin/v1/users', {})
def get_users(self, limit=None, offset=0):
"""
Returns a list of user objects.
Params:
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET', '/admin/v1/users', {'limit': limit, 'offset': offset})
return list(self.get_users_iterator())
def get_user_by_id(self, user_id):
"""
Returns user specified by user_id.
user_id - User to fetch
Returns user object.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id
response = self.json_api_call('GET', path, {})
return response
def get_user_by_email(self, email):
"""
Returns user specified by email.
email - User to fetch
Returns user object.
Raises RuntimeError on error.
"""
params = {
'email': email,
}
response = self.json_api_call('GET',
'/admin/v1/users',
params)
return response
def get_users_by_name(self, username):
"""
Returns user specified by username.
username - User to fetch
Returns a list of 0 or 1 user objects.
Raises RuntimeError on error.
"""
params = {
'username': username,
}
response = self.json_api_call('GET',
'/admin/v1/users',
params)
return response
def get_users_by_names(self, usernames):
"""
Returns users specified by usernames.
usernames - Users to fetch
Returns a list user objects matching usernames (or aliases).
Raises RuntimeError on error.
"""
username_list = json.dumps(usernames)
params = {
'username_list': username_list,
}
response = self.json_paging_api_call('GET',
'/admin/v1/users',
params)
return response
def get_users_by_ids(self, user_ids):
"""
Returns users specified by user ids.
user_ids - Users to fetch
Returns a list user objects matching user ids.
Raises RuntimeError on error.
"""
user_id_list = json.dumps(user_ids)
params = {
'user_id_list': user_id_list,
}
response = self.json_paging_api_call('GET',
'/admin/v1/users',
params)
return response
def add_user(self, username, realname=None, status=None,
notes=None, email=None, firstname=None, lastname=None,
alias1=None, alias2=None, alias3=None, alias4=None,
aliases=None):
"""
Adds a user.
username - Username
realname - User's real name (optional)
status - User's status, defaults to USER_STATUS_ACTIVE
notes - Comment field (optional)
email - Email address (optional)
firstname - User's given name for ID Proofing (optional)
lastname - User's surname for ID Proofing (optional)
alias1..alias4 - Aliases for the user's primary username (optional)
aliases - Aliases for the user's primary username (optional)
Returns newly created user object.
Raises RuntimeError on error.
"""
params = {
'username': username,
}
if realname is not None:
params['realname'] = realname
if status is not None:
params['status'] = status
if notes is not None:
params['notes'] = notes
if email is not None:
params['email'] = email
if firstname is not None:
params['firstname'] = firstname
if lastname is not None:
params['lastname'] = lastname
if alias1 is not None:
params['alias1'] = alias1
if alias2 is not None:
params['alias2'] = alias2
if alias3 is not None:
params['alias3'] = alias3
if alias4 is not None:
params['alias4'] = alias4
if aliases is not None:
params['aliases'] = aliases
response = self.json_api_call('POST',
'/admin/v1/users',
params)
return response
def update_user(self, user_id, username=None, realname=None, status=None,
notes=None, email=None, firstname=None, lastname=None,
alias1=None, alias2=None, alias3=None, alias4=None,
aliases=None):
"""
Update username, realname, status, or notes for a user.
user_id - User ID
username - Username (optional)
realname - User's real name (optional)
status - User's status, defaults to USER_STATUS_ACTIVE
notes - Comment field (optional)
email - Email address (optional)
firstname - User's given name for ID Proofing (optional)
lastname - User's surname for ID Proofing (optional)
alias1..alias4 - Aliases for the user's primary username (optional)
aliases - Aliases for the user's primary username. (optional)
Returns updated user object.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id
params = {}
if username is not None:
params['username'] = username
if realname is not None:
params['realname'] = realname
if status is not None:
params['status'] = status
if notes is not None:
params['notes'] = notes
if email is not None:
params['email'] = email
if firstname is not None:
params['firstname'] = firstname
if lastname is not None:
params['lastname'] = lastname
if alias1 is not None:
params['alias1'] = alias1
if alias2 is not None:
params['alias2'] = alias2
if alias3 is not None:
params['alias3'] = alias3
if alias4 is not None:
params['alias4'] = alias4
if aliases is not None:
params['aliases'] = aliases
response = self.json_api_call('POST', path, params)
return response
def delete_user(self, user_id):
"""
Deletes a user. If the user is already deleted, does nothing.
user_id - User ID
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id
return self.json_api_call('DELETE', path, {})
def enroll_user(self, username, email, valid_secs=None):
"""
Enroll a user and send them an enrollment email.
username - Username
email - Email address
valid_secs - Seconds before the enrollment link expires
(if 0 it never expires)
Returns nothing.
Raises RuntimeError on error.
"""
path = '/admin/v1/users/enroll'
params = {
'username': username,
'email': email,
}
if valid_secs is not None:
params['valid_secs'] = str(valid_secs)
return self.json_api_call('POST', path, params)
def add_user_bypass_codes(
self,
user_id,
count=None,
valid_secs=None,
remaining_uses=None,
codes=None,
preserve_existing=None,
endpoint_verification=None,
):
"""
Generate bypass codes for user.
Replaces a user's bypass codes with new codes unless
`preserve_existing=True` is passed.
user_id User ID
count Number of new codes to randomly generate
valid_secs Seconds before codes expire (if 0 they will never expire)
remaining_uses The number of times this code can be used (0 is unlimited)
codes Optionally provide custom codes, otherwise will be random
count and codes are mutually exclusive
preserve_existing whether to preserve existing codes when creating new ones,
default is to remove existing bypass codes
endpoint_verification New argument for unreleased feature. Will be ignored if used.
Client will be updated again in the future when feature is released.
Returns a list of newly created codes.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/bypass_codes'
params = {}
if count is not None:
params['count'] = str(int(count))
if valid_secs is not None:
params['valid_secs'] = str(int(valid_secs))
if endpoint_verification is not None:
params["endpoint_verification"] = str(endpoint_verification).lower()
if remaining_uses is not None:
params['reuse_count'] = str(int(remaining_uses))
if codes is not None:
params['codes'] = self._canonicalize_bypass_codes(codes)
if preserve_existing is not None:
params['preserve_existing'] = preserve_existing
return self.json_api_call('POST', path, params)
def get_user_bypass_codes_iterator(self, user_id):
""" Returns an iterator of bypass codes associated with a user.
Params:
user_id (str) - The user id.
Returns:
A iterator yielding bypass code dicts.
Notes:
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/bypass_codes'
return self.json_paging_api_call('GET', path, {})
def get_user_bypass_codes(self, user_id, limit=None, offset=0):
""" Returns a list of bypass codes associated with a user.
Params:
user_id (str) - The user id.
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns:
An array of bypass code dicts.
Notes:
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/bypass_codes'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_bypass_codes_iterator(user_id))
def get_user_phones_iterator(self, user_id):
"""
Returns an iterator of phones associated with the user.
user_id - User ID
Returns an iterator of phone objects.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/phones'
return self.json_paging_api_call('GET', path, {})
def get_user_phones(self, user_id, limit=None, offset=0):
"""
Returns an array of phones associated with the user.
user_id - User ID
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns list of phone objects.
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/phones'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_phones_iterator(user_id))
def add_user_phone(self, user_id, phone_id):
"""
Associates a phone with a user.
user_id - User ID
phone_id - Phone ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/phones'
params = {
'phone_id': phone_id,
}
return self.json_api_call('POST', path, params)
def delete_user_phone(self, user_id, phone_id):
"""
Dissociates a phone from a user.
user_id - User ID
phone_id - Phone ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/phones/' + phone_id
params = {}
return self.json_api_call('DELETE', path,
params)
def get_user_tokens_iterator(self, user_id):
"""
Returns an iterator of hardware tokens associated with the user.
user_id - User ID
Returns iterator of hardware token objects.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/tokens'
return self.json_paging_api_call('GET', path, {})
def get_user_tokens(self, user_id, limit=None, offset=0):
"""
Returns an array of hardware tokens associated with the user.
user_id - User ID
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns list of hardware token objects.
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/tokens'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_tokens_iterator(user_id))
def add_user_token(self, user_id, token_id):
"""
Associates a hardware token with a user.
user_id - User ID
token_id - Token ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/tokens'
params = {
'token_id': token_id,
}
return self.json_api_call('POST', path, params)
def delete_user_token(self, user_id, token_id):
"""
Dissociates a hardware token from a user.
user_id - User ID
token_id - Hardware token ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
token_id = urllib.parse.quote_plus(str(token_id))
path = '/admin/v1/users/' + user_id + '/tokens/' + token_id
return self.json_api_call('DELETE', path, {})
def get_user_u2ftokens_iterator(self, user_id):
""" Returns an iterator of u2ftokens associated with a user.
Params:
user_id (str) - The user id.
Returns:
A generator yielding u2ftoken dicts.
Notes:
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/u2ftokens'
return self.json_paging_api_call('GET', path, {})
def get_user_u2ftokens(self, user_id, limit=None, offset=0):
""" Returns an array of u2ftokens associated with a user.
Params:
user_id (str) - The user id.
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns:
An array of u2ftoken dicts.
Notes:
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/u2ftokens'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_u2ftokens_iterator(user_id))
def get_user_webauthncredentials_iterator(self, user_id):
""" Returns an iterator of webauthncredentials associated with a user.
Params:
user_id (str) - The user id.
Returns:
A generator yielding webauthncredentials dicts.
Notes:
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/webauthncredentials'
return self.json_paging_api_call('GET', path, {})
def get_user_webauthncredentials(self, user_id, limit=None, offset=0):
""" Returns an array of webauthncredentials associated
with a user.
Params:
user_id (str) - The user id.
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns:
An array of webauthncredentials dicts.
Notes:
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/webauthncredentials'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_webauthncredentials_iterator(user_id))
def get_user_groups_iterator(self, user_id):
"""
Returns an iterator of groups associated with the user.
user_id - User ID
Returns iterator of groups objects.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/groups'
return self.json_paging_api_call('GET', path, {})
def get_user_groups(self, user_id, limit=None, offset=0):
"""
Returns an array of groups associated with the user.
user_id - User ID
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Returns list of groups objects.
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/groups'
return self.json_api_call(
'GET', path, {'limit': limit, 'offset': offset})
return list(self.get_user_groups_iterator(user_id))
def add_user_group(self, user_id, group_id):
"""
Associates a group with a user.
user_id - User ID
group_id - Group ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/groups'
params = {'group_id': group_id}
return self.json_api_call('POST', path, params)
def delete_user_group(self, user_id, group_id):
"""
Dissociates a group from a user.
user_id - User ID
group_id - Group ID
Returns nothing.
Raises RuntimeError on error.
"""
user_id = urllib.parse.quote_plus(str(user_id))
path = '/admin/v1/users/' + user_id + '/groups/' + group_id
params = {}
return self.json_api_call('DELETE', path, params)
def get_endpoint(self, epkey):
"""
Get a single endpoint from the AdminAPI by a supplied epkey.
Params:
epkey (str) - The supplied endpoint key to fetch.
Returns: The endpoint object represented as a dict.
Raises:
RuntimeError: if the request returns a non-200 status response.
"""
escaped_epkey = urllib.parse.quote_plus(str(epkey))
path = '/admin/v1/endpoints/' + escaped_epkey
return self.json_api_call('GET', path, {})
def get_endpoints_iterator(self):
"""
Returns iterator of endpoints objects.
Raises RuntimeError on error.
"""
return self.json_paging_api_call('GET', '/admin/v1/endpoints', {})
def get_endpoints(self, limit=None, offset=0):
"""
Returns a list of endpoints.
Params:
limit - The maximum number of records to return. (Optional)
offset - The offset of the first record to return. (Optional)
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call('GET', '/admin/v1/endpoints',
{'limit': limit, 'offset': offset})
return list(self.get_endpoints_iterator())
def get_phones_generator(self):
"""
Returns a generator yielding phones.
"""
return self.json_paging_api_call(
'GET',
'/admin/v1/phones',
{}
)
def get_phones(self, limit=None, offset=0):
"""
Retrieves a list of phones.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of phones
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/phones',
{'limit': limit, 'offset': offset}
)
return list(self.get_phones_generator())
def get_phone_by_id(self, phone_id):
"""
Returns a phone specified by phone_id.
phone_id - Phone ID
Returns phone object.
Raises RuntimeError on error.
"""
path = '/admin/v1/phones/' + phone_id
response = self.json_api_call('GET', path, {})
return response
def get_phones_by_number(self, number, extension=None):
"""
Returns a phone specified by number and extension.
number - Phone number
extension - Phone number extension (optional)
Returns list of 0 or 1 phone objects.
Raises RuntimeError on error.
"""
path = '/admin/v1/phones'
params = {'number': number}
if extension is not None:
params['extension'] = extension
response = self.json_api_call('GET', path,
params)
return response
def add_phone(self,
number=None,
extension=None,
name=None,
type=None,
platform=None,
predelay=None,
postdelay=None):
"""
Adds a phone.
number - Phone number (optional).
extension - Phone number extension (optional).
name - Phone name (optional).
type - The phone type (optional).
platform - The phone platform (optional).
predelay - Number of seconds to wait after the number picks up
before dialing the extension (optional).
postdelay - Number of seconds to wait after the extension is
dialed before the speaking the prompt (optional).
Returns phone object.
Raises RuntimeError on error.
"""
path = '/admin/v1/phones'
params = {}
if number is not None:
params['number'] = number
if extension is not None:
params['extension'] = extension
if name is not None:
params['name'] = name
if type is not None:
params['type'] = type
if platform is not None:
params['platform'] = platform
if predelay is not None:
params['predelay'] = predelay
if postdelay is not None:
params['postdelay'] = postdelay
response = self.json_api_call('POST', path,
params)
return response
def update_phone(self, phone_id,
number=None,
extension=None,
name=None,
type=None,
platform=None,
predelay=None,
postdelay=None):
"""
Update a phone.
number - Phone number (optional)
extension - Phone number extension (optional).
name - Phone name (optional).
type - The phone type (optional).
platform - The phone platform (optional).
predelay - Number of seconds to wait after the number picks up
before dialing the extension (optional).
postdelay - Number of seconds to wait after the extension is
dialed before the speaking the prompt (optional).
Returns phone object.
Raises RuntimeError on error.
"""
phone_id = urllib.parse.quote_plus(str(phone_id))
path = '/admin/v1/phones/' + phone_id
params = {}
if number is not None:
params['number'] = number
if extension is not None:
params['extension'] = extension
if name is not None:
params['name'] = name
if type is not None:
params['type'] = type
if platform is not None:
params['platform'] = platform
if predelay is not None:
params['predelay'] = predelay
if postdelay is not None:
params['postdelay'] = postdelay
response = self.json_api_call('POST', path,
params)
return response
def delete_phone(self, phone_id):
"""
Deletes a phone. If the phone has already been deleted, does nothing.
phone_id - Phone ID.
Raises RuntimeError on error.
"""
path = '/admin/v1/phones/' + phone_id
params = {}
return self.json_api_call('DELETE', path, params)
def send_sms_activation_to_phone(self, phone_id,
valid_secs=None,
install=None,
installation_msg=None,
activation_msg=None):
"""
Generate a Duo Mobile activation code and send it to the phone via
SMS, optionally sending an additional message with a PATH to
install Duo Mobile.
phone_id - Phone ID.
valid_secs - The number of seconds activation code should be valid for.
Default: 86400 seconds (one day).
install - '1' to also send an installation SMS message before the
activation message; '0' to not send. Default: '0'.
installation_msg - Custom installation message template to send to
the user if install was 1. Must contain
<insturl>, which is replaced with the
installation URL.
activation_msg - Custom activation message template. Must contain
<acturl>, which is replaced with the activation URL.
Returns: {
"activation_barcode": "https://api-abcdef.duosecurity.com/frame/qr?value=duo%3A%2F%2Factivation-code",
"activation_msg": "To activate the Duo Mobile app, click this link: https://m-abcdef.duosecurity.com/iphone/7dmi4Oowz5g3J47FARLs",
"installation_msg": "Welcome to Duo! To install the Duo Mobile app, click this link: http://m-abcdef.duosecurity.com",
"valid_secs": 3600
}
Raises RuntimeError on error.
"""
path = '/admin/v1/phones/' + phone_id + '/send_sms_activation'
params = {}
if valid_secs is not None:
params['valid_secs'] = str(valid_secs)
if install is not None:
params['install'] = str(int(bool(install)))
if installation_msg is not None:
params['installation_msg'] = installation_msg
if activation_msg is not None:
params['activation_msg'] = activation_msg
return self.json_api_call('POST', path,
params)
def create_activation_url(self, phone_id,
valid_secs=None,
install=None):
"""
Create an activation code for Duo Mobile.
phone_id - Phone ID.
valid_secs - The number of seconds activation code should be valid for.
Default: 86400 seconds (one day).
install - '1' to also return an installation_url for Duo
Mobile; '0' to not return. Default: '0'.
Returns: {
"activation_barcode": "https://api-abcdef.duosecurity.com/frame/qr?value=duo%3A%2F%2Factivation-code",
"activation_url": "https://m-abcdef.duosecurity.com/iphone/7dmi4Oowz5g3J47FARLs",
"valid_secs": 3600
}
Raises RuntimeError on error.
"""
path = '/admin/v1/phones/' + phone_id + '/activation_url'
params = {}
if valid_secs is not None:
params['valid_secs'] = str(valid_secs)
if install is not None:
params['install'] = str(int(bool(install)))
return self.json_api_call('POST', path, params)
def send_sms_installation(self, phone_id,
installation_msg=None):
"""
Send a message via SMS describing how to install Duo Mobile.
phone_id - Phone ID.
installation_msg - Custom installation message template to send to
the user if install was 1. Must contain
<insturl>, which is replaced with the
installation URL.
Returns: {
"installation_msg": "Welcome to Duo! To install the Duo Mobile app, click this link: http://m-abcdef.duosecurity.com",
}
Raises RuntimeError on error.
"""
path = '/admin/v1/phones/' + phone_id + '/send_sms_installation'
params = {}
if installation_msg is not None:
params['installation_msg'] = installation_msg
return self.json_api_call('POST', path, params)
def get_desktoptokens_generator(self):
"""
Returns a generator yielding desktoptokens.
"""
return self.json_paging_api_call(
'GET',
'/admin/v1/desktoptokens',
{}
)
def get_desktoptokens(self, limit=None, offset=0):
"""
Retrieves a list of desktoptokens.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of desktoptokens
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/desktoptokens',
{'limit': limit, 'offset': offset}
)
return list(self.get_desktoptokens_generator())
def get_desktoptoken_by_id(self, desktoptoken_id):
"""
Returns a desktop token specified by dtoken_id.
desktoptoken_id - Desktop Token ID
Returns desktop token object.
Raises RuntimeError on error.
"""
path = '/admin/v1/desktoptokens/' + desktoptoken_id
response = self.json_api_call('GET', path, {})
return response
def add_desktoptoken(self,
platform,
name=None):
"""
Adds a desktop token.
Returns desktop token object.
platform - The desktop token platform.
name - Desktop token name (optional).
Raises RuntimeError on error.
"""
params = {
'platform': platform,
}
if name is not None:
params['name'] = name
response = self.json_api_call('POST',
'/admin/v1/desktoptokens',
params)
return response
def delete_desktoptoken(self, desktoptoken_id):
"""
Deletes a desktop token. If the desktop token has already been deleted,
does nothing.
desktoptoken_id - Desktop token ID.
Returns nothing.
Raises RuntimeError on error.
"""
path = '/admin/v1/desktoptokens/' + urllib.parse.quote_plus(desktoptoken_id)
params = {}
return self.json_api_call('DELETE', path, params)
def update_desktoptoken(self,
desktoptoken_id,
platform=None,
name=None):
"""
Update a desktop token.
Returns desktop token object.
name - Desktop token name (optional).
platform - The desktop token platform (optional).
Raises RuntimeError on error.
"""
desktoptoken_id = urllib.parse.quote_plus(str(desktoptoken_id))
path = '/admin/v1/desktoptokens/' + desktoptoken_id
params = {}
if platform is not None:
params['platform'] = platform
if name is not None:
params['name'] = name
response = self.json_api_call('POST',
path,
params)
return response
def activate_desktoptoken(self, desktoptoken_id, valid_secs=None):
"""
Generates an activation code for a desktop token.
Returns activation info like:
{
'activation_msg': <str:message with activation url>,
'activation_url': <str:activation url>,
'valid_secs': <int:seconds>}
Raises RuntimeError on error.
"""
params = {}
if valid_secs:
params['valid_secs'] = str(valid_secs)
quoted_id = urllib.parse.quote_plus(desktoptoken_id)
response = self.json_api_call('POST',
'/admin/v1/desktoptokens/%s/activate' % quoted_id,
params)
return response
def get_tokens_generator(self):
"""
Returns a generator yielding tokens.
"""
return self.json_paging_api_call(
'GET',
'/admin/v1/tokens',
{}
)
def get_tokens(self, limit=None, offset=0):
"""
Retrieves a list of tokens.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of tokens
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/tokens',
{'limit': limit, 'offset': offset}
)
return list(self.get_tokens_generator())
def get_token_by_id(self, token_id):
"""
Returns a token.
token_id - Token ID
Returns a token object.
"""
token_id = urllib.parse.quote_plus(str(token_id))
path = '/admin/v1/tokens/' + token_id
params = {}
response = self.json_api_call('GET', path,
params)
return response
def get_tokens_by_serial(self, type, serial):
"""
Returns a token.
type - Token type, one of TOKEN_HOTP_6, TOKEN_HOTP_8, TOKEN_YUBIKEY
serial - Token serial number
Returns a list of 0 or 1 token objects.
"""
params = {
'type': type,
'serial': serial,
}
response = self.json_api_call('GET', '/admin/v1/tokens', params)
return response
def delete_token(self, token_id):
"""
Deletes a token. If the token is already deleted, does nothing.
token_id - Token ID
"""
token_id = urllib.parse.quote_plus(str(token_id))
path = '/admin/v1/tokens/' + token_id
return self.json_api_call('DELETE', path, {})
def add_hotp6_token(self, serial, secret, counter=None):
"""
Add a HOTP6 token.
serial - Token serial number
secret - HOTP secret
counter - Initial counter value (default: 0)
Returns newly added token object.
"""
path = '/admin/v1/tokens'
params = {'type': 'h6', 'serial': serial, 'secret': secret}
if counter is not None:
params['counter'] = str(int(counter))
response = self.json_api_call('POST', path,
params)
return response
def add_hotp8_token(self, serial, secret, counter=None):
"""
Add a HOTP8 token.
serial - Token serial number
secret - HOTP secret
counter - Initial counter value (default: 0)
Returns newly added token object.
"""
path = '/admin/v1/tokens'
params = {'type': 'h8', 'serial': serial, 'secret': secret}
if counter is not None:
params['counter'] = str(int(counter))
response = self.json_api_call('POST', path,
params)
return response
def add_totp6_token(self, serial, secret, totp_step=None):
"""
Add a TOTP6 token.
serial - Token serial number
secret - TOTP secret
totp_step - Time step (default: 30 seconds)
Returns newly added token object.
"""
path = '/admin/v1/tokens'
params = {'type': 't6', 'serial': serial, 'secret': secret}
if totp_step is not None:
params['totp_step'] = str(int(totp_step))
response = self.json_api_call('POST', path,
params)
return response
def add_totp8_token(self, serial, secret, totp_step=None):
"""
Add a TOTP8 token.
serial - Token serial number
secret - TOTP secret
totp_step - Time step (default: 30 seconds)
Returns newly added token object.
"""
path = '/admin/v1/tokens'
params = {'type': 't8', 'serial': serial, 'secret': secret}
if totp_step is not None:
params['totp_step'] = str(int(totp_step))
response = self.json_api_call('POST', path,
params)
return response
def update_token(self, token_id, totp_step=None):
"""
Update a token.
totp_step - Time step (optional)
Returns token object.
Raises RuntimeError on error.
"""
token_id = urllib.parse.quote_plus(str(token_id))
path = '/admin/v1/tokens/' + token_id
params = {}
if totp_step is not None:
params['totp_step'] = totp_step
response = self.json_api_call('POST', path,
params)
return response
def add_yubikey_token(self, serial, private_id, aes_key):
"""
Add a Yubikey AES token.
serial - Token serial number
secret - HOTP secret
Returns newly added token object.
"""
path = '/admin/v1/tokens'
params = {'type': 'yk', 'serial': serial, 'private_id': private_id,
'aes_key': aes_key}
response = self.json_api_call('POST', path,
params)
return response
def resync_hotp_token(self, token_id, code1, code2, code3):
"""
Resync HOTP counter. The user must generate 3 consecutive OTP
from their token and input them as code1, code2, and code3. This
function will scan ahead in the OTP sequence to find a counter
that resyncs with those 3 codes.
token_id - Token ID
code1 - First OTP from token
code2 - Second OTP from token
code3 - Third OTP from token
Returns nothing on success.
"""
token_id = urllib.parse.quote_plus(str(token_id))
path = '/admin/v1/tokens/' + token_id + '/resync'
params = {'code1': code1, 'code2': code2, 'code3': code3}
return self.json_api_call('POST', path, params)
def get_settings(self):
"""
Returns customer settings.
Returns a settings object.
Raises RuntimeError on error.
"""
return self.json_api_call('GET', '/admin/v1/settings', {})
def update_settings(self,
lockout_threshold=None,
lockout_expire_duration=None,
inactive_user_expiration=None,
pending_deletion_days=None,
log_retention_days=None,
sms_batch=None,
sms_expiration=None,
sms_refresh=None,
sms_message=None,
fraud_email=None,
fraud_email_enabled=None,
keypress_confirm=None,
keypress_fraud=None,
timezone=None,
telephony_warning_min=None,
caller_id=None,
push_enabled=None,
voice_enabled=None,
sms_enabled=None,
mobile_otp_enabled=None,
u2f_enabled=None,
user_telephony_cost_max=None,
minimum_password_length=None,
password_requires_upper_alpha=None,
password_requires_lower_alpha=None,
password_requires_numeric=None,
password_requires_special=None,
helpdesk_bypass=None,
helpdesk_bypass_expiration=None,
helpdesk_message=None,
helpdesk_can_send_enroll_email=None,
reactivation_url=None,
reactivation_integration_key=None,
security_checkup_enabled=None,
user_managers_can_put_users_in_bypass=None,
email_activity_notification_enabled=None,
push_activity_notification_enabled=None,
unenrolled_user_lockout_threshold=None,
enrollment_universal_prompt_enabled=None,
):
"""
Update settings.
lockout_threshold - <int:number of attempts>|None
lockout_expire_duration - <int:minutes>|0|None
inactive_user_expiration - <int:number of days>|None
pending_deletion_days - <int:number of days>|None
log_retention_days - <int:number of days>|0|None
sms_batch - <int:batch size>|None
sms_expiration - <int:minutes>|None
sms_refresh - True|False|None
sms_message - <str:message>|None
fraud_email - <str:email address>|None
fraud_email_enabled - True|False|None
keypress_confirm - <str:0-9, #, or *>|None
keypress_fraud - <str:0-9, #, or *>|None
timezone - <str:IANA timezone>|None
telephony_warning_min - <int:credits>
caller_id - <str:phone number>
push_enabled - Deprecated; ignored if specified.
sms_enabled - Deprecated; ignored if specified.
voice_enabled - Deprecated; ignored if specified.
mobile_otp_enabled - Deprecated; ignored if specified.
u2f_enabled - Deprecated; ignored if specified.
user_telephony_cost_max - <int:positive number of credits>
minimum_password_length - <int:length>|None
password_requires_upper_alpha - True|False|None
password_requires_lower_alpha - True|False|None
password_requires_numeric - True|False|None
password_requires_special - True|False|None
helpdesk_bypass - "allow"|"limit"|"deny"|None
helpdesk_bypass_expiration - <int:minutes>|0
helpdesk_message - <str:message|None>
helpdesk_can_send_enroll_email - True|False|None
reactivation_url - <str:url>|None
reactivation_integration_key - <str:url>|None
security_checkup_enabled - True|False|None
user_managers_can_put_users_in_bypass - True|False|None
email_activity_notification_enabled = True|False|None
push_activity_notification_enabled = True|False|None
unenrolled_user_lockout_threshold = <int:number of days>|0|None
enrollment_universal_prompt_enabled = True|False|None
Returns updated settings object.
Raises RuntimeError on error.
"""
params = {}
if lockout_threshold is not None:
params['lockout_threshold'] = str(lockout_threshold)
if lockout_expire_duration is not None:
params['lockout_expire_duration'] = str(lockout_expire_duration)
if inactive_user_expiration is not None:
params['inactive_user_expiration'] = str(inactive_user_expiration)
if pending_deletion_days is not None:
params['pending_deletion_days'] = str(pending_deletion_days)
if log_retention_days is not None:
params['log_retention_days'] = str(log_retention_days)
if sms_batch is not None:
params['sms_batch'] = str(sms_batch)
if sms_expiration is not None:
params['sms_expiration'] = str(sms_expiration)
if sms_refresh is not None:
params['sms_refresh'] = '1' if sms_refresh else '0'
if sms_message is not None:
params['sms_message'] = sms_message
if fraud_email is not None:
params['fraud_email'] = fraud_email
if fraud_email_enabled is not None:
params['fraud_email_enabled'] = ('1' if
fraud_email_enabled else '0')
if keypress_confirm is not None:
params['keypress_confirm'] = keypress_confirm
if keypress_fraud is not None:
params['keypress_fraud'] = keypress_fraud
if timezone is not None:
params['timezone'] = timezone
if telephony_warning_min is not None:
params['telephony_warning_min'] = str(telephony_warning_min)
if caller_id is not None:
params['caller_id'] = caller_id
if user_telephony_cost_max is not None:
params['user_telephony_cost_max'] = str(user_telephony_cost_max)
if minimum_password_length is not None:
params['minimum_password_length'] = str(minimum_password_length)
if password_requires_upper_alpha is not None:
params['password_requires_upper_alpha'] = ('1' if
password_requires_upper_alpha else '0')
if password_requires_lower_alpha is not None:
params['password_requires_lower_alpha'] = ('1' if
password_requires_lower_alpha else '0')
if password_requires_numeric is not None:
params['password_requires_numeric'] = ('1' if
password_requires_numeric else '0')
if password_requires_special is not None:
params['password_requires_special'] = ('1' if
password_requires_special else '0')
if helpdesk_bypass is not None:
params['helpdesk_bypass'] = str(helpdesk_bypass)
if helpdesk_bypass_expiration is not None:
params['helpdesk_bypass_expiration'] = str(helpdesk_bypass_expiration)
if helpdesk_message is not None:
params['helpdesk_message'] = str(helpdesk_message)
if helpdesk_can_send_enroll_email is not None:
params['helpdesk_can_send_enroll_email'] = ('1' if
helpdesk_can_send_enroll_email else '0')
if reactivation_url is not None:
params['reactivation_url'] = reactivation_url
if reactivation_integration_key is not None:
params['reactivation_integration_key'] = reactivation_integration_key
if security_checkup_enabled is not None:
params['security_checkup_enabled'] = ('1' if
security_checkup_enabled else '0')
if user_managers_can_put_users_in_bypass is not None:
params['user_managers_can_put_users_in_bypass'] = ('1' if
user_managers_can_put_users_in_bypass else '0')
if email_activity_notification_enabled is not None:
params['email_activity_notification_enabled'] = (
'1' if email_activity_notification_enabled else '0'
)
if push_activity_notification_enabled is not None:
params['push_activity_notification_enabled'] = (
'1' if push_activity_notification_enabled else '0'
)
if unenrolled_user_lockout_threshold is not None:
params['unenrolled_user_lockout_threshold'] = str(
unenrolled_user_lockout_threshold
)
if enrollment_universal_prompt_enabled is not None:
params['enrollment_universal_prompt_enabled'] = (
'1' if enrollment_universal_prompt_enabled else '0'
)
if not params:
raise TypeError("No settings were provided")
response = self.json_api_call('POST',
'/admin/v1/settings',
params)
return response
def set_allowed_admin_auth_methods(self,
push_enabled=None,
sms_enabled=None,
voice_enabled=None,
mobile_otp_enabled=None,
yubikey_enabled=None,
hardware_token_enabled=None,
verified_push_enabled=None,
verified_push_length=None
):
params = {}
if push_enabled is not None:
params['push_enabled'] = (
'1' if push_enabled else '0')
if sms_enabled is not None:
params['sms_enabled'] = (
'1' if sms_enabled else '0')
if mobile_otp_enabled is not None:
params['mobile_otp_enabled'] = (
'1' if mobile_otp_enabled else '0')
if hardware_token_enabled is not None:
params['hardware_token_enabled'] = (
'1' if hardware_token_enabled else '0')
if yubikey_enabled is not None:
params['yubikey_enabled'] = (
'1' if yubikey_enabled else '0')
if voice_enabled is not None:
params['voice_enabled'] = (
'1' if voice_enabled else '0')
if verified_push_enabled is not None:
params['verified_push_enabled'] = (
'1' if verified_push_enabled else '0')
if params['verified_push_enabled'] == '1':
params['verified_push_length'] = (
verified_push_length if verified_push_length is not None else 3)
response = self.json_api_call(
'POST',
'/admin/v1/admins/allowed_auth_methods',
params
)
return response
def get_allowed_admin_auth_methods(self):
params={}
response = self.json_api_call(
'GET',
'/admin/v1/admins/allowed_auth_methods',
params
)
return response
def get_info_summary(self):
"""
Returns a summary of objects in the account.
Raises RuntimeError on error.
"""
params = {}
response = self.json_api_call(
'GET',
'/admin/v1/info/summary',
params
)
return response
def get_info_telephony_credits_used(self,
mintime=None,
maxtime=None):
"""
Returns number of telephony credits used during the time period.
mintime - Limit report to data for events after this UNIX
timestamp. Defaults to thirty days ago.
maxtime - Limit report to data for events before this UNIX
timestamp. Defaults to the current time.
Raises RuntimeError on error.
"""
params = {}
if mintime is not None:
params['mintime'] = mintime
if maxtime is not None:
params['maxtime'] = maxtime
response = self.json_api_call(
'GET',
'/admin/v1/info/telephony_credits_used',
params
)
return response
def get_authentication_attempts(self,
mintime=None,
maxtime=None):
"""
Returns counts of authentication attempts, broken down by result.
mintime - Limit report to data for events after this UNIX
timestamp. Defaults to thirty days ago.
maxtime - Limit report to data for events before this UNIX
timestamp. Defaults to the current time.
Returns: {
"ERROR": <int>,
"FAILURE": <int>,
"FRAUD": <int>,
"SUCCESS": <int>
}
where each integer is the number of authentication attempts with
that result.
Raises RuntimeError on error.
"""
params = {}
if mintime is not None:
params['mintime'] = mintime
if maxtime is not None:
params['maxtime'] = maxtime
response = self.json_api_call(
'GET',
'/admin/v1/info/authentication_attempts',
params
)
return response
def get_user_authentication_attempts(self,
mintime=None,
maxtime=None):
"""
Returns number of unique users with each possible authentication result.
mintime - Limit report to data for events after this UNIX
timestamp. Defaults to thirty days ago.
maxtime - Limit report to data for events before this UNIX
timestamp. Defaults to the current time.
Returns: {
"ERROR": <int>,
"FAILURE": <int>,
"FRAUD": <int>,
"SUCCESS": <int>
}
where each integer is the number of users who had at least one
authentication attempt ending with that result. (These counts are
thus always less than or equal to those returned by
get_authentication_attempts.)
Raises RuntimeError on error.
"""
params = {}
if mintime is not None:
params['mintime'] = mintime
if maxtime is not None:
params['maxtime'] = maxtime
response = self.json_api_call(
'GET',
'/admin/v1/info/user_authentication_attempts',
params
)
return response
def get_groups_generator(self):
"""
Returns a generator yielding groups.
"""
return self.json_paging_api_call(
'GET',
'/admin/v1/groups',
{}
)
def get_groups_by_group_ids(self, group_ids):
"""
Get a list of groups by their group ids
Args:
group_ids: list of group ids to fetch
Returns:
list of groups
"""
group_id_list = json.dumps(group_ids)
return self.json_api_call(
'GET',
'/admin/v1/groups',
{'group_id_list': group_id_list}
)
def get_groups(self, limit=None, offset=0):
"""
Retrieves a list of groups.
Args:
limit: The max number of groups to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of groups
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/groups',
{'limit': limit, 'offset': offset}
)
return list(self.get_groups_generator())
def get_group(self, group_id, api_version=1):
"""
Returns a group by the group id.
group_id - The id of group (Required)
api_version - The api version of the handler to use. Currently, the
default api version is v1, but the v1 api will be
deprecated in a future version of the Duo Admin API.
Please migrate to the v2 api at your earliest convenience.
For details on the differences between v1 and v2,
please see Duo's Admin API documentation. (Optional)
"""
if api_version == 1:
url = '/admin/v1/groups/'
warnings.warn(
'The v1 Admin API for group details will be deprecated '
'in a future release of the Duo Admin API. Please migrate to '
'the v2 API.',
DeprecationWarning)
elif api_version == 2:
url = '/admin/v2/groups/'
else:
raise ValueError('Invalid API Version')
return self.json_api_call('GET', url + group_id, {})
def get_group_users(self, group_id, limit=None, offset=0):
"""
Get a paginated list of users associated with the specified
group.
group_id - The id of the group (Required)
limit - The maximum number of records to return. Maximum is 500. (Optional)
offset - The offset of the first record to return. (Optional)
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v2/groups/' + group_id + '/users',
{
'limit': limit,
'offset': offset,
})
return list(self.get_group_users_iterator(group_id))
def get_group_users_iterator(self, group_id):
"""
Returns an iterator of users associated with the specified group.
group_id - The id of the group (Required)
"""
return self.json_paging_api_call(
'GET',
'/admin/v2/groups/' + group_id + '/users',
{}
)
def create_group(self, name,
desc=None,
status=None,
push_enabled=None,
sms_enabled=None,
voice_enabled=None,
mobile_otp_enabled=None,
u2f_enabled=None,
):
"""
Create a new group.
name - The name of the group (Required)
desc - Group description (Optional)
status - Group authentication status <str: 'active'/'disabled'/'bypass'> (Optional)
push_enabled - Push factor restriction <True/False> (Optional)
sms_enabled - SMS factor restriction <True/False> (Optional)
voice_enabled - Voice factor restriction <True/False> (Optional)
mobile_otp_enabled - Mobile OTP restriction <>True/False (Optional)
"""
params = {
'name': name,
}
if desc is not None:
params['desc'] = desc
if status is not None:
params['status'] = status
if push_enabled is not None:
params['push_enabled'] = '1' if push_enabled else '0'
if sms_enabled is not None:
params['sms_enabled'] = '1' if sms_enabled else '0'
if voice_enabled is not None:
params['voice_enabled'] = '1' if voice_enabled else '0'
if mobile_otp_enabled is not None:
params['mobile_otp_enabled'] = '1' if mobile_otp_enabled else '0'
if u2f_enabled is not None:
params['u2f_enabled'] = '1' if u2f_enabled else '0'
response = self.json_api_call(
'POST',
'/admin/v1/groups',
params
)
return response
def delete_group(self, group_id):
"""
Delete a group by group_id
group_id - The id of the group (Required)
"""
return self.json_api_call(
'DELETE',
'/admin/v1/groups/' + group_id,
{}
)
def modify_group(self,
group_id,
name=None,
desc=None,
status=None,
push_enabled=None,
sms_enabled=None,
voice_enabled=None,
mobile_otp_enabled=None,
u2f_enabled=None,
):
"""
Modify a group
group_id - The id of the group to modify (Required)
name - New group name (Optional)
desc - New group description (Optional)
status - Group authentication status <str: 'active'/'disabled'/'bypass'> (Optional)
push_enabled - Push factor restriction <True/False> (Optional)
sms_enabled - SMS factor restriction <True/False> (Optional)
voice_enabled - Voice factor restriction <True/False> (Optional)
mobile_otp_enabled - Mobile OTP restriction <True/False> (Optional)
u2f_enabled - u2f restriction <True/False> (Optional)
"""
params = {}
if name is not None:
params['name'] = name
if desc is not None:
params['desc'] = desc
if status is not None:
params['status'] = status
if push_enabled is not None:
params['push_enabled'] = '1' if push_enabled else '0'
if sms_enabled is not None:
params['sms_enabled'] = '1' if sms_enabled else '0'
if voice_enabled is not None:
params['voice_enabled'] = '1' if voice_enabled else '0'
if mobile_otp_enabled is not None:
params['mobile_otp_enabled'] = '1' if mobile_otp_enabled else '0'
if u2f_enabled is not None:
params['u2f_enabled'] = '1' if u2f_enabled else '0'
response = self.json_api_call(
'POST',
'/admin/v1/groups/' + group_id,
params
)
return response
def get_integrations_generator(self):
"""
Returns a generator yielding integrations.
"""
return self.json_paging_api_call(
'GET',
'/admin/v3/integrations',
{},
)
def get_integrations(self, limit=None, offset=0):
"""
Retrieves a list of integrations.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of integrations
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v3/integrations',
{'limit': limit, 'offset': offset},
)
return list(self.get_integrations_generator())
def get_integration(self, integration_key):
"""
Returns the requested integration.
integration_key - The ikey of the integration to get
Returns list of integration objects.
Raises RuntimeError on error.
"""
params = {}
response = self.json_api_call(
'GET',
'/admin/v3/integrations/' + integration_key,
params,
)
return response
def create_integration(self,
name,
integration_type,
visual_style=None,
greeting=None,
notes=None,
enroll_policy=None,
username_normalization_policy=None,
adminapi_admins=None,
adminapi_info=None,
adminapi_integrations=None,
adminapi_read_log=None,
adminapi_read_resource=None,
adminapi_settings=None,
adminapi_write_resource=None,
trusted_device_days=None,
ip_whitelist=None,
ip_whitelist_enroll_policy=None,
groups_allowed=None,
self_service_allowed=None,
sso=None,
user_access=None):
"""Creates a new integration.
name - The name of the integration (required)
integration_type - <str: integration type constant> (required)
See adminapi docs for possible values.
visual_style - Deprecated; ignored if specified.
greeting - <str:Voice greeting> (optional, default '')
notes - <str:internal use> (optional, uses default setting)
enroll_policy - <str:'enroll'|'allow'|'deny'> (optional, default 'enroll')
username_normalization_policy - <str:'simple'|'none'> (optional, default 'none')
trusted_device_days - <int: days>|None
ip_whitelist - <str: CSV list of IP/Ranges>|None
See adminapi docs for more details.
ip_whitelist_enroll_policy - <bool: policy>
See adminapi docs for more details.
adminapi_admins - <bool: admins permission>|None
adminapi_info - <bool: info permission>|None
adminapi_integrations - <bool:integrations permission>|None
adminapi_read_log - <bool:read log permission>|None
adminapi_read_resource - <bool: read resource permission>|None
adminapi_settings - <bool: settings permission>|None
adminapi_write_resource - <bool:write resource permission>|None
groups_allowed - <str: CSV list of gkeys of groups allowed to auth>
self_service_allowed - <bool: self service permission>|None
sso - <dict: parameters for generic single sign-on> (optional)
New argument for unreleased feature. Will return an error if used.
Client will be updated again in the future when feature is released.
Returns the created integration.
Raises RuntimeError on error.
"""
params = {}
if name is not None:
params['name'] = name
if integration_type is not None:
params['type'] = integration_type
if visual_style is not None:
params['visual_style'] = visual_style
if greeting is not None:
params['greeting'] = greeting
if notes is not None:
params['notes'] = notes
if username_normalization_policy is not None:
params['username_normalization_policy'] = username_normalization_policy
if enroll_policy is not None:
params['enroll_policy'] = enroll_policy
if trusted_device_days is not None:
params['trusted_device_days'] = str(trusted_device_days)
if ip_whitelist is not None:
params['ip_whitelist'] = self._canonicalize_ip_whitelist(ip_whitelist)
if ip_whitelist_enroll_policy is not None:
params['ip_whitelist_enroll_policy'] = ip_whitelist_enroll_policy
if adminapi_admins is not None:
params['adminapi_admins'] = '1' if adminapi_admins else '0'
if adminapi_info is not None:
params['adminapi_info'] = '1' if adminapi_info else '0'
if adminapi_integrations is not None:
params['adminapi_integrations'] = '1' if adminapi_integrations else '0'
if adminapi_read_log is not None:
params['adminapi_read_log'] = '1' if adminapi_read_log else '0'
if adminapi_read_resource is not None:
params['adminapi_read_resource'] = (
'1' if adminapi_read_resource else '0')
if adminapi_settings is not None:
params['adminapi_settings'] = '1' if adminapi_settings else '0'
if adminapi_write_resource is not None:
params['adminapi_write_resource'] = (
'1' if adminapi_write_resource else '0')
if groups_allowed is not None:
params['groups_allowed'] = groups_allowed
if self_service_allowed is not None:
params['self_service_allowed'] = '1' if self_service_allowed else '0'
if sso is not None:
params['sso'] = sso
if user_access is not None:
params['user_access'] = user_access
response = self.json_api_call('POST',
'/admin/v3/integrations',
params,
)
return response
def get_registered_devices_generator(self):
"""
Returns a generator yielding Duo Desktop registered devices.
"""
return self.json_paging_api_call('GET', '/admin/v1/registered_devices', {})
def get_registered_devices(self, limit=None, offset=0):
"""
Retrieves a list of Duo Desktop registered devices.
Args:
limit: The max number of registered devices to fetch at once. [Default: None]
offset: If a 'limit' is passed, the offset to start retrieval.
[Default: 0]
Returns:
list of registered devices
Raises:
RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call('GET', '/admin/v1/registered_devices', {'limit': limit, 'offset': offset})
return list(self.get_registered_devices_generator())
def get_registered_device_by_id(self, registered_device_id):
"""
Returns a Duo Desktop registered device specified by registered_device_id (compkey).
Args:
registered_device_id - Duo Desktop registered device compkey
Returns:
registered device object.
Raises:
RuntimeError on error.
"""
path = '/admin/v1/registered_devices/' + registered_device_id
response = self.json_api_call('GET', path, {})
return response
def delete_registered_device(self, registered_device_id):
"""
Deletes a Duo Desktop registered device. If the registered device has already been deleted,
does nothing.
Args:
registered_device_id - Duo Desktop registered device ID (compkey).
Returns:
None
Raises:
RuntimeError on error.
"""
path = '/admin/v1/registered_devices/' + urllib.parse.quote_plus(registered_device_id)
params = {}
return self.json_api_call('DELETE', path, params)
def get_secret_key(self, integration_key):
"""Returns the secret key of the specified integration.
integration_key - The ikey of the secret key to get.
Returns the skey
Raises RuntimeError on error.
"""
params = {}
response = self.json_api_call(
'GET',
'/admin/v1/integrations/' + integration_key + '/skey',
params,
)
return response
def delete_integration(self, integration_key):
"""Deletes an integration.
integration_key - The integration key of the integration to delete.
Raises RuntimeError on error.
"""
integration_key = urllib.parse.quote_plus(str(integration_key))
path = '/admin/v3/integrations/%s' % integration_key
return self.json_api_call(
'DELETE',
path,
{},
)
def update_integration(self,
integration_key,
name=None,
visual_style=None,
greeting=None,
notes=None,
enroll_policy=None,
username_normalization_policy=None,
adminapi_admins=None,
adminapi_info=None,
adminapi_integrations=None,
adminapi_read_log=None,
adminapi_read_resource=None,
adminapi_settings=None,
adminapi_write_resource=None,
reset_secret_key=None,
trusted_device_days=None,
ip_whitelist=None,
ip_whitelist_enroll_policy=None,
groups_allowed=None,
self_service_allowed=None,
sso=None,
user_access=None
):
"""Updates an integration.
integration_key - The key of the integration to update. (required)
name - The name of the integration (optional)
visual_style - Deprecated; ignored if specified.
greeting - Voice greeting (optional, default '')
notes - internal use (optional, uses default setting)
enroll_policy - <'enroll'|'allow'|'deny'> (optional, default 'enroll')
trusted_device_days - <int: days>|None
ip_whitelist - <str: CSV list of IP/Ranges>|None
See adminapi docs for more details.
ip_whitelist_enroll_policy - <bool: policy>
See adminapi docs for more details.
adminapi_admins - <int:0|1>|None
adminapi_info - True|False|None
adminapi_integrations - True|False|None
adminapi_read_log - True|False|None
adminapi_read_resource - True|False|None
adminapi_settings - True|False|None
adminapi_write_resource - True|False|None
reset_secret_key - <any value>|None
groups_allowed - <str: CSV list of gkeys of groups allowed to auth>
self_service_allowed - True|False|None
sso - <dict: parameters for generic single sign-on> (optional)
New argument for unreleased feature. Will return an error if used.
Client will be updated again in the future when feature is released.
If any value other than None is provided for 'reset_secret_key'
(for example, 1), then a new secret key will be generated for the
integration.
Returns the created integration.
Raises RuntimeError on error.
"""
integration_key = urllib.parse.quote_plus(str(integration_key))
path = '/admin/v3/integrations/%s' % integration_key
params = {}
if name is not None:
params['name'] = name
if visual_style is not None:
params['visual_style'] = visual_style
if greeting is not None:
params['greeting'] = greeting
if notes is not None:
params['notes'] = notes
if enroll_policy is not None:
params['enroll_policy'] = enroll_policy
if username_normalization_policy is not None:
params['username_normalization_policy'] = username_normalization_policy
if trusted_device_days is not None:
params['trusted_device_days'] = str(trusted_device_days)
if ip_whitelist is not None:
params['ip_whitelist'] = self._canonicalize_ip_whitelist(ip_whitelist)
if ip_whitelist_enroll_policy is not None:
params['ip_whitelist_enroll_policy'] = ip_whitelist_enroll_policy
if adminapi_admins is not None:
params['adminapi_admins'] = '1' if adminapi_admins else '0'
if adminapi_info is not None:
params['adminapi_info'] = '1' if adminapi_info else '0'
if adminapi_integrations is not None:
params['adminapi_integrations'] = '1' if adminapi_integrations else '0'
if adminapi_read_log is not None:
params['adminapi_read_log'] = '1' if adminapi_read_log else '0'
if adminapi_read_resource is not None:
params['adminapi_read_resource'] = (
'1' if adminapi_read_resource else '0')
if adminapi_settings is not None:
params['adminapi_settings'] = '1' if adminapi_settings else '0'
if adminapi_write_resource is not None:
params['adminapi_write_resource'] = (
'1' if adminapi_write_resource else '0')
if reset_secret_key is not None:
params['reset_secret_key'] = '1'
if groups_allowed is not None:
params['groups_allowed'] = groups_allowed
if self_service_allowed is not None:
params['self_service_allowed'] = '1' if self_service_allowed else '0'
if sso is not None:
params['sso'] = sso
if user_access is not None:
params['user_access'] = user_access
if not params:
raise TypeError("No new values were provided")
response = self.json_api_call(
'POST',
path,
params,
)
return response
def get_admins(self, limit=None, offset=0):
"""
Retrieves a list of administrators.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of administrators. See the adminapi docs.
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/admins',
{'limit': limit, 'offset': offset}
)
iterator = self.get_admins_iterator()
return list(iterator)
def get_admins_iterator(self):
"""
Provides a generator which produces admins. Under the hood, this
generator uses pagination, so it will only store one page of admins at a
time in memory.
Returns: A generator which produces admins.
Raises RuntimeError on error.
"""
return self.json_paging_api_call('GET', '/admin/v1/admins', {})
def get_admin(self, admin_id):
"""
Returns an administrator.
admin_id - The id of the administrator.
Returns an administrator. See the adminapi docs.
Raises RuntimeError on error.
"""
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/%s' % admin_id
response = self.json_api_call('GET', path, {})
return response
def add_admin(self, name, email, phone, password, role=None, subaccount_role=None):
"""
Create an administrator and adds it to a customer.
name - <str:the name of the administrator>
email - <str:email address>
phone - <str:phone number>
password - Deprecated; ignored if specified.
role - <str|None:role>
subaccount_role - <str|None:role>
Returns the added administrator. See the adminapi docs.
Raises RuntimeError on error.
"""
params = {}
if name is not None:
params['name'] = name
if email is not None:
params['email'] = email
if phone is not None:
params['phone'] = phone
if role is not None:
params['role'] = role
if subaccount_role is not None:
params['subaccount_role'] = subaccount_role
response = self.json_api_call('POST', '/admin/v1/admins', params)
return response
def update_admin(self, admin_id,
name=None,
phone=None,
password=None,
password_change_required=None,
status=None,
role=None,
subaccount_role=None
):
"""
Update one or more attributes of an administrator.
admin_id - The id of the administrator.
name - <str:the name of the administrator> (optional)
phone - <str:phone number> (optional)
password - Deprecated; ignored if specified.
password_change_required - <bool|None:Whether admin is required to change their password at next login> (optional)
status - the status of the administrator (optional) - NOTE: Valid values are "Active" and "Disabled" - "Disabled" NOT valid for administrators with role - Owner
role - <str|None:role> (optional)
subaccount_role - <str|None:role> (optional)
Returns the updated administrator. See the adminapi docs.
Raises RuntimeError on error.
"""
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/%s' % admin_id
params = {}
if name is not None:
params['name'] = name
if phone is not None:
params['phone'] = phone
if password_change_required is not None:
params['password_change_required'] = password_change_required
if status is not None:
params['status'] = status
if role is not None:
params['role'] = role
if subaccount_role is not None:
params['subaccount_role'] = subaccount_role
response = self.json_api_call('POST', path, params)
return response
def delete_admin(self, admin_id):
"""
Deletes an administrator.
admin_id - The id of the administrator.
Raises RuntimeError on error.
"""
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/%s' % admin_id
return self.json_api_call('DELETE', path, {})
def reset_admin(self, admin_id):
"""
Resets the admin lockout.
admin_id - <int:admin id>
Raises RuntimeError on error.
"""
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/%s/reset' % admin_id
return self.json_api_call('POST', path, {})
def activate_admin(self, email,
send_email=False,
valid_days=None,
admin_role=None):
"""
Generates an activation code for an administrator and optionally
emails the administrator.
email - <str:email address of administrator>
valid_days - <int:number of days> (optional)
send_email - <bool:True if email should be sent> (optional)
admin_role - <str: Role assigned to new admin> (optional)
Returns {
"admin_activation_id": <str:ID of the administrator activation link>
"admin_role": <str:administrator role assigned to the new admin>
"code": <str:activation code>
"email": <str:email for admin/message>
"email_sent": <string:true if email was sent, false otherwise>
"expires": <int:timestamp of expiration>
"link": <str:activation link>
"message": <str:message in email body>
"subject": <str:email subject line>
"valid_days": <int:valid days>
}
See the adminapi docs for updated return values.
Raises RuntimeError on error.
"""
params = {}
if email is not None:
params['email'] = email
if send_email is not None:
params['send_email'] = str(send_email)
if valid_days is not None:
params['valid_days'] = str(valid_days)
if admin_role is not None:
params['admin_role'] = str(admin_role)
response = self.json_api_call('POST',
'/admin/v1/admins/activations',
params)
return response
def get_external_password_mgmt_statuses(self, limit=None, offset=0):
"""
Returns a paged list of administrators indicating whether they
have been configured for external password management.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns a list of administrators' external password management
statuses. See the adminapi docs.
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/admins/password_mgmt',
{'limit': limit, 'offset': offset}
)
iterator = self.json_paging_api_call(
'GET', '/admin/v1/admins/password_mgmt', {})
return list(iterator)
def get_external_password_mgmt_status_for_admin(self, admin_id):
"""
Returns the external password management status for an admin
admin_id - The id of the admin.
Returns an external password management status. See the
adminapi docs.
Raises RuntimeError on error.
"""
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/{}/password_mgmt'.format(admin_id)
response = self.json_api_call('GET', path, {})
return response
def update_admin_password_mgmt_status(
self, admin_id, has_external_password_mgmt=None,
password=None):
"""
Enable or disable an admin for external password management,
and optionally set the password for an admin
admin_id - The id of the admin.
has_external_password_mgmt - whether or not this admin's password
is managed via API.
password - New password for the admin.
Returns an external password management status. See the
adminapi docs.
Raises RuntimeError on error.
"""
params = {}
if password is not None:
params['password'] = password
if has_external_password_mgmt is not None:
params['has_external_password_mgmt'] = str(has_external_password_mgmt)
admin_id = urllib.parse.quote_plus(str(admin_id))
path = '/admin/v1/admins/{}/password_mgmt'.format(admin_id)
response = self.json_api_call('POST', path, params)
return response
def get_logo(self):
"""
Returns current logo's PNG data or raises an error if none is set.
Raises RuntimeError on error.
"""
response, data = self.api_call('GET',
'/admin/v1/logo',
params={})
content_type = response.getheader('Content-Type')
if content_type and content_type.startswith('image/'):
return data
else:
return self.parse_json_response(response, data)
def update_logo(self, logo):
"""
Set a logo that will appear in future Duo Mobile activations.
logo - <str:PNG byte sequence>
Raises RuntimeError on error.
"""
params = {
'logo': base64.b64encode(logo).decode(),
}
return self.json_api_call('POST', '/admin/v1/logo', params)
def delete_logo(self):
return self.json_api_call('DELETE', '/admin/v1/logo', params={})
def get_u2ftokens(self, limit=None, offset=0):
"""
Retrieves a list of u2ftokens
Args:
limit: The max number of u2ftokens to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: A list of u2ftokens
Notes: Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call('GET',
'/admin/v1/u2ftokens',
{'limit': limit, 'offset': offset})
iterator = self.get_u2ftokens_iterator()
return list(iterator)
def get_u2ftokens_iterator(self):
"""
Provides a generator which u2ftokens. Under the hood, this generator
uses pagination, so it will only store one page of administrative_units
at a time in memory.
Returns: A generator which produces u2ftokens.
Raises RuntimeError on error.
"""
return self.json_paging_api_call('GET', '/admin/v1/u2ftokens', {})
def get_webauthncredentials(self, limit=None, offset=0):
"""
Retrieves a list of webauthn credentials
Args:
limit: The max number of webauthn credentials to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: A list of webauthn credentials
Notes: Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call('GET',
'/admin/v1/webauthncredentials',
{'limit': limit, 'offset': offset})
iterator = self.get_webauthncredentials_iterator()
return list(iterator)
def get_webauthncredentials_iterator(self):
"""
Provides a generator which webauthn credentials. Under the hood, this generator
uses pagination, so it will only store one page of administrative_units
at a time in memory.
Returns: A generator which produces webauthn credentials.
Raises RuntimeError on error.
"""
return self.json_paging_api_call('GET', '/admin/v1/webauthncredentials', {})
def get_u2ftoken_by_id(self, registration_id):
""" Returns u2ftoken specified by registration_id.
Params:
registration_id (str): The registration id of the
u2ftoken to fetch.
Returns:
A u2ftoken dict.
Notes:
Raises RuntimeError on error.
"""
registration_id = urllib.parse.quote_plus(str(registration_id))
path = '/admin/v1/u2ftokens/' + registration_id
response = self.json_api_call('GET', path, {})
return response
def delete_u2ftoken(self, registration_id):
""" Deletes a u2ftoken. If the u2ftoken is already
deleted, does nothing.
Params:
registration_id (str): The registration id of the u2f token.
Notes:
Raises RuntimeError on error.
"""
registration_id = \
urllib.parse.quote_plus(str(registration_id))
path = '/admin/v1/u2ftokens/' + registration_id
return self.json_api_call('DELETE', path, {})
def get_webauthncredential_by_id(self, webauthnkey):
""" Returns webauthn credentials specified by webauthnkey.
Params:
webauthnkey (str): The registration id of the
webauthn credentials to fetch.
Returns:
Returns a webauthn credentials dict.
Notes:
Raises RuntimeError on error.
"""
webauthnkey = \
urllib.parse.quote_plus(str(webauthnkey))
path = '/admin/v1/webauthncredentials/' + webauthnkey
response = self.json_api_call('GET', path, {})
return response
def delete_webauthncredential(self, webauthnkey):
""" Deletes a webauthn credentials. If the webauthn credentials is already
deleted, does nothing.
Params:
webauthnkey (str): The registration id of the
webauthn credentials.
Notes:
Raises RuntimeError on error.
"""
webauthnkey = \
urllib.parse.quote_plus(str(webauthnkey))
path = '/admin/v1/webauthncredentials/' + webauthnkey
response = self.json_api_call('DELETE', path, {})
return response
def get_bypass_codes_generator(self):
"""
Returns a generator yielding bypass codes.
"""
return self.json_paging_api_call(
'GET',
'/admin/v1/bypass_codes',
{}
)
def get_bypass_codes(self, limit=None, offset=0):
"""
Retrieves a list of bypass codes.
Args:
limit: The max number of admins to fetch at once. Default None
offset: If a limit is passed, the offset to start retrieval.
Default 0
Returns: list of bypass codes
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
'GET',
'/admin/v1/bypass_codes',
{'limit': limit, 'offset': offset}
)
return list(self.get_bypass_codes_generator())
def delete_bypass_code_by_id(self, bypass_code_id):
""" Deletes a bypass code. If the bypass code is already
deleted, does nothing.
Params:
bypass_code_id (str): The id of the bypass code.
Notes:
Raises RuntimeError on error.
"""
bypass_code_id = \
urllib.parse.quote_plus(str(bypass_code_id))
path = '/admin/v1/bypass_codes/' + bypass_code_id
response = self.json_api_call('DELETE', path, {})
return response
def sync_user(self, username, directory_key):
""" Syncronize a single user immediately with a specified directory.
Params:
username (str) - The username of the user to be synchronized.
directory_key (str) - The unique id of the directory.
Notes:
Raises RuntimeError on error.
"""
params = {
'username': username,
}
directory_key = urllib.parse.quote_plus(directory_key)
path = (
'/admin/v1/users/directorysync/{directory_key}/syncuser').format(
directory_key=directory_key)
return self.json_api_call('POST', path, params)
def send_verification_push(self, user_id, phone_id):
return self.json_api_call(
'POST',
f'/admin/v1/users/{user_id}/send_verification_push',
{'phone_id': phone_id}
)
def get_verification_push_response(self, user_id, push_id):
return self.json_api_call(
'GET',
f'/admin/v1/users/{user_id}/verification_push_response',
{'push_id': push_id},
)
def get_trust_monitor_events_iterator(
self,
mintime,
maxtime,
event_type=None,
):
"""
Returns a generator which yields trust monitor events.
Params:
mintime (int) - Return events that have a surfaced timestamp greater
than or equal to mintime. Timestamp is represented as
a unix timestamp in milliseconds.
maxtime (int) - Return events that have a surfaced timestamp less
than or equal to maxtime. Timestamp is represented as
a unix timestamp in milliseconds.
event_type (str, optional) - Limit the events returned by a supplied
event type represented as a string. If
not supplied, the caller will recieve all
event types. Check the Duo Admin API
documentation for expected values for
this parameter.
Returns: Generator which yields trust monitor events.
"""
params = {
"mintime": "{}".format(mintime),
"maxtime": "{}".format(maxtime),
}
if event_type is not None:
params["type"] = event_type
return self.json_cursor_api_call(
"GET",
"/admin/v1/trust_monitor/events",
params,
lambda resp: resp["events"],
)
def get_trust_monitor_events_by_offset(
self,
mintime,
maxtime,
limit=None,
offset=None,
event_type=None,
):
"""
Fetch Duo Trust Monitor Events from the Admin API.
Params:
mintime (int) - Return events that have a surfaced timestamp greater
than or equal to mintime. Timestamp is represented as
a unix timestamp in milliseconds.
maxtime (int) - Return events that have a surfaced timestamp less
than or equal to maxtime. Timestamp is represented as
a unix timestamp in milliseconds.
limit (int, optional) - Limit the number of events returned.
offset (str, optional) - Provide an offset from a previous request's
response metadata["next_offset"].
event_type (str, optional) - Limit the events returned by a supplied
event type represented as a string. If
not supplied, the caller will recieve all
event types. Check the Duo Admin API
documentation for expected values for
this parameter.
Returns: response containing a list of Duo Trust Monitor Events.
"""
params = {
"mintime": "{}".format(mintime),
"maxtime": "{}".format(maxtime),
}
if limit is not None:
params["limit"] = "{}".format(limit)
if offset is not None:
params["offset"] = "{}".format(offset)
if event_type is not None:
params["type"] = event_type
return self.json_api_call(
"GET",
"/admin/v1/trust_monitor/events",
params,
)
def _quote_policy_id(self, policy_key):
return urllib.parse.quote_plus("{}".format(policy_key))
def get_policies_v2_iterator(self):
"""
Obtain an iterator for retrieving all the policies. The order isn't defined.
Returns: Iterator of dict elements. Each element contains the policy content.
"""
return self.json_paging_api_call(
"GET",
"/admin/v2/policies",
{},
)
def get_policies_v2(self, limit=None, offset=0):
"""
Retrieves a list of policies. The order isn't defined.
Args:
limit (int, optional): The max number of policies to fetch at once.
offset (int, optional): If a limit is passed, the offset to start retrieval.
Default 0
Raises RuntimeError on error.
"""
(limit, offset) = self.normalize_paging_args(limit, offset)
if limit:
return self.json_api_call(
"GET",
"/admin/v2/policies",
{"limit": limit, "offset": offset},
)
return list(self.get_policies_v2_iterator())
def delete_policy_v2(self, policy_key):
"""
Deletes a policy.
Params:
policy_key (str) - Unique id of the policy
Notes:
Raises RuntimeError on error.
"""
path = "/admin/v2/policies/" + self._quote_policy_id(policy_key)
return self.json_api_call("DELETE", path, {})
def update_policy_v2(self, policy_key, json_request):
"""
Update the content of a single policy
Args:
policy_key (str) - Unique id of the policy
json_request (dict) - policy content to update.
Returns (dict) - policy after updates have been made.
"""
path = "/admin/v2/policies/" + self._quote_policy_id(policy_key)
response = self.json_api_call("PUT", path, json_request)
return response
def update_policies_v2(self, sections, sections_to_delete,
edit_list, edit_all_policies=False):
"""
Update the contents of multiple policies.
Args:
sections (dict): policy content to update
sections_to_delete (list): List of section names to delete
edit_list (list): List of new policy keys to apply the changes to.
Ignored if edit_all_policies is True.
edit_all_policies (bool, optional): Apply changes to all policies.
Defaults to False.
Returns (list): all updated policies
"""
path = "/admin/v2/policies/update"
params = {
"policies_to_update": {
"edit_all_policies": edit_all_policies,
"edit_list": edit_list,
},
"policy_changes": {
"sections": sections,
"sections_to_delete": sections_to_delete,
},
}
response = self.json_api_call("PUT", path, params)
return response
def create_policy_v2(self, json_request):
"""
Args:
json_request (dict) - policy content to create.
Returns (dict) - newly created policy
"""
path = "/admin/v2/policies"
response = self.json_api_call("POST", path, json_request)
return response
def copy_policy_v2(self, policy_key, new_policy_names_list):
"""
Copy policy to multiple new policies.
Args:
policy_key (str): Unique id of the policy to copy from
new_policy_names_list (array): The policy specified by policy_key
will be copied once for each name
in the list
Returns (list): all new policies
"""
path = "/admin/v2/policies/copy"
params = {
"policy_key": policy_key,
"new_policy_names_list": new_policy_names_list
}
response = self.json_api_call("POST", path, params)
return response
def get_policy_v2(self, policy_key):
"""
Args:
policy_key: policy_key (str) - Unique id of the policy
Returns (dict) - policy content
"""
path = "/admin/v2/policies/" + self._quote_policy_id(policy_key)
response = self.json_api_call("GET", path, {})
return response
def get_policy_summary_v2(self):
"""
Returns (dict) - summary of all policies and the applications
and groups to which they are applied.
"""
path = "/admin/v2/policies/summary"
response = self.json_api_call("GET", path, {})
return response
def calculate_policy(self, integration_key, user_id):
"""
Args:
integration_key - The integration_key of the application to evaluate. (required)
user_id - The user_id of the user to evaluate (required)
Returns (dict) - Dictionary containing "policy_elements" and "sections"
"""
path = "/admin/v2/policies/calculate"
response = self.json_api_call(
"GET",
path,
{"integration_key": integration_key, "user_id": user_id},
)
return response
def get_passport_config(self):
"""
Retrieve the current Passport configuration.
Returns (dict):
{
"enabled_status": string,
"enabled_groups": [
{
"group_id": user group ID,
"group_name": descriptive user group name,
...
},
...
]
"disabled_groups": [
{
"group_id": user group ID,
"group_name": descriptive user group name,
...
},
...
]
}
"""
path = "/admin/v2/passport/config"
response = self.json_api_call("GET", path, {})
return response
def update_passport_config(self, enabled_status, enabled_groups: Optional[List[str]]=None, disabled_groups: Optional[List[str]]=None, custom_supported_browsers=None):
"""
Update the current Passport configuration.
Args:
enabled_status (str) - one of "disabled", "enabled", "enabled-for-groups",
or "enabled-with-exceptions"
enabled_groups (list[str]) - if enabled_status is "enabled-for-groups", a
list of user group IDs for whom Passport should be enabled
disabled_groups (list[str]) - if enabled_status is "enabled-with-exceptions",
a list of user group IDs for whom Passport should be disabled
custom_supported_browsers (dict) - a dict of criteria that determines whether
a Windows or macOS browsers should be supported by Passport
"""
if custom_supported_browsers is None:
custom_supported_browsers = {"macos": [], "windows": [],}
path = "/admin/v2/passport/config"
response = self.json_api_call(
"POST",
path,
{
"enabled_status": enabled_status,
"enabled_groups": enabled_groups,
"disabled_groups": disabled_groups,
"custom_supported_browsers": custom_supported_browsers,
},
)
return response
class AccountAdmin(Admin):
"""AccountAdmin manages a child account using an Accounts API integration."""
def __init__(self, account_id, child_api_host=None, **kwargs):
"""Initializes an AccountAdmin for administering a child account.
account_id is the account id of the child account.
child_api_host is the api hostname of the child account.
If this is not provided, this value will be calculated for correct API usage.
See the Client base class for other parameters.
"""
if not child_api_host:
child_api_host = Accounts.child_map.get(account_id, None)
if child_api_host is None:
child_api_host = kwargs.get('host')
try:
child_api_host = self.get_child_api_host(account_id, **kwargs)
except RuntimeError:
pass
kwargs['host'] = child_api_host
super(AccountAdmin, self).__init__(**kwargs)
self.account_id = account_id
def get_child_api_host(self, account_id, **kwargs):
accounts_api = Accounts(**kwargs)
accounts_api.get_child_accounts()
return Accounts.child_map.get(account_id, kwargs['host'])
def get_edition(self):
"""
Returns the edition of the account {
"edition" : <str:edition name>
}
Raises RuntimeError on error.
"""
response = self.json_api_call('GET',
'/admin/v1/billing/edition',
params={})
return response
def set_edition(self, edition):
"""
Set the edition of the child account.
edition - The edition string to set on the child account.
Should be 'ENTERPRISE' (MFA), 'PLATFORM' (Access), or 'BEYOND'.
Raises RuntimeError on error.
"""
params = {
'edition': edition,
}
return self.json_api_call('POST',
'/admin/v1/billing/edition',
params)
def get_telephony_credits(self):
"""
Returns the telephony credits of the account {
"credits" : <int>
}
Raises RuntimeError on error.
"""
return self.json_api_call('GET',
'/admin/v1/billing/telephony_credits',
params={})
def set_telephony_credits(self, credits):
"""
Set the telephony credits of the child account.
credits - The telephony credits to set on the child account.
The ammount added to the child account,
(credits - child's current telephony credits), will be
deducted from the parent account's telephony credits.
Raises RuntimeError on error.
"""
params = {
'credits': str(credits),
}
return self.json_api_call('POST',
'/admin/v1/billing/telephony_credits',
params)
|