Skip to content

Commit

Permalink
Add support for API Tokens
Browse files Browse the repository at this point in the history
API Tokens (currently in beta) allow more secure usage as they can limit
scope.

Fixes cloudflare#74
  • Loading branch information
nijel committed Aug 23, 2019
1 parent cf084ee commit 9f28c65
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 10 additions & 4 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def call_with_no_auth(self, method, parts,
identifier1, identifier2, identifier3,
params, data, files)

def _add_auth_headers(self, headers):
""" Add authentication headers """
if self.email:
headers['X-Auth-Email'] = self.email
headers['X-Auth-Key'] = self.token
else:
headers['Authorization'] = 'Bearer {}'.format(self.token)

def call_with_auth(self, method, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
Expand All @@ -58,10 +66,9 @@ def call_with_auth(self, method, parts,
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-Email': self.email,
'X-Auth-Key': self.token,
'Content-Type': 'application/json'
}
self._add_auth_headers(headers)
if type(data) == str:
# passing javascript vs JSON
headers['Content-Type'] = 'application/javascript'
Expand All @@ -83,10 +90,9 @@ def call_with_auth_unwrapped(self, method, parts,
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-Email': self.email,
'X-Auth-Key': self.token,
'Content-Type': 'application/json'
}
self._add_auth_headers(headers)
if type(data) == str:
# passing javascript vs JSON
headers['Content-Type'] = 'application/javascript'
Expand Down
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ parameters.
# A minimal call with debug enabled
cf = CloudFlare.CloudFlare(debug=True))
# Using API Token (note missing email)
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000')
# A full blown call with passed basic account information
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000')
Expand Down

0 comments on commit 9f28c65

Please sign in to comment.