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

Commit 9f28c65

Browse files
committed
Add support for API Tokens
API Tokens (currently in beta) allow more secure usage as they can limit scope. Fixes #74
1 parent cf084ee commit 9f28c65

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

CloudFlare/cloudflare.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def call_with_no_auth(self, method, parts,
4949
identifier1, identifier2, identifier3,
5050
params, data, files)
5151

52+
def _add_auth_headers(self, headers):
53+
""" Add authentication headers """
54+
if self.email:
55+
headers['X-Auth-Email'] = self.email
56+
headers['X-Auth-Key'] = self.token
57+
else:
58+
headers['Authorization'] = 'Bearer {}'.format(self.token)
59+
5260
def call_with_auth(self, method, parts,
5361
identifier1=None, identifier2=None, identifier3=None,
5462
params=None, data=None, files=None):
@@ -58,10 +66,9 @@ def call_with_auth(self, method, parts,
5866
raise CloudFlareAPIError(0, 'no email and/or token defined')
5967
headers = {
6068
'User-Agent': self.user_agent,
61-
'X-Auth-Email': self.email,
62-
'X-Auth-Key': self.token,
6369
'Content-Type': 'application/json'
6470
}
71+
self._add_auth_headers(headers)
6572
if type(data) == str:
6673
# passing javascript vs JSON
6774
headers['Content-Type'] = 'application/javascript'
@@ -83,10 +90,9 @@ def call_with_auth_unwrapped(self, method, parts,
8390
raise CloudFlareAPIError(0, 'no email and/or token defined')
8491
headers = {
8592
'User-Agent': self.user_agent,
86-
'X-Auth-Email': self.email,
87-
'X-Auth-Key': self.token,
8893
'Content-Type': 'application/json'
8994
}
95+
self._add_auth_headers(headers)
9096
if type(data) == str:
9197
# passing javascript vs JSON
9298
headers['Content-Type'] = 'application/javascript'

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ parameters.
211211
# A minimal call with debug enabled
212212
cf = CloudFlare.CloudFlare(debug=True))
213213
214+
# Using API Token (note missing email)
215+
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000')
216+
214217
# A full blown call with passed basic account information
215218
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000')
216219

0 commit comments

Comments
 (0)