Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit ec9970d

Browse files
committed
user token checking example added
1 parent fafa4e4 commit ec9970d

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

examples/example_user_tokens.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
"""Cloudflare API code - example"""
3+
4+
import os
5+
import sys
6+
7+
sys.path.insert(0, os.path.abspath('..'))
8+
import CloudFlare
9+
10+
def main():
11+
"""Cloudflare API code - example"""
12+
13+
#
14+
# Usage: examples/example_user_tokens.py [config file profile name]
15+
#
16+
# Store your access token in the config file as-per the README ("Cloudflare" is the default).
17+
#
18+
# $ cat ~/.cloudflare/cloudflare.cfg
19+
# [Work]
20+
# token = 00000000000000000000000000000000
21+
# [Home]
22+
# email = home@example.com
23+
# token = 00000000000000000000000000000000
24+
# $
25+
#
26+
27+
try:
28+
profile_id = sys.argv[1]
29+
except IndexError:
30+
profile_id = None
31+
32+
cf = CloudFlare.CloudFlare(profile=profile_id)
33+
34+
# display all the users tokens
35+
try:
36+
v = cf.user.tokens()
37+
except CloudFlare.exceptions.CloudFlareAPIError as e:
38+
print('/user.tokens.get %d %s - api call failed' % (e, e))
39+
v = None
40+
except Exception as e:
41+
exit('/user.tokens.get - %s - api call failed' % (e))
42+
43+
if v:
44+
print('TOKENS:')
45+
for t in v:
46+
print(' %s %s [%-20s %-20s %-20s] %d %s' % (t['id'], t['status'], t['issued_on'], t['modified_on'], t['last_used_on'], len(t['policies']), t['name']))
47+
print('')
48+
49+
# verify the user token being used (vs. email/key - this will throw an exception if it's not valid
50+
try:
51+
v = cf.user.tokens.verify()
52+
except CloudFlare.exceptions.CloudFlareAPIError as e:
53+
# exit('/user.tokens.verify.get %d %s - api call failed' % (e, e))
54+
v = None
55+
except Exception as e:
56+
exit('/user.tokens.verify.get - %s - api call failed' % (e))
57+
58+
if v:
59+
print('VERIFYED TOKENS')
60+
print(' %s %-10s [%-20s %-20s]' % (
61+
v['id'],
62+
v['status'],
63+
v['not_before'] if 'not_before' in v else '',
64+
v['expires_on'] if 'expires_on' in v else ''
65+
))
66+
else:
67+
print('User token not verified - i.e invalid (or not used)')
68+
69+
exit(0)
70+
71+
if __name__ == '__main__':
72+
main()
73+

0 commit comments

Comments
 (0)