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

Commit 6e63048

Browse files
authored
Merge pull request #76 from nijel/patch-1
Add support for API Tokens
2 parents cf084ee + 09d0605 commit 6e63048

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

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.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ if __name__ == '__main__':
172172

173173
When you create a **CloudFlare** class you can pass up to four parameters.
174174

175-
* Account email
176-
* Account API key
175+
* API Token or API Key
176+
* Account email (only if an API Key is being used)
177177
* Optional Origin-CA Certificate Token
178178
* Optional Debug flag (True/False)
179179

@@ -186,20 +186,25 @@ import CloudFlare
186186
# A minimal call with debug enabled
187187
cf = CloudFlare.CloudFlare(debug=True))
188188

189-
# A full blown call with passed basic account information
189+
# An authenticated call using an API Token (note the missing email)
190+
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000')
191+
192+
# An authenticated call using an API Key
190193
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000')
191194

192-
# A full blown call with passed basic account information and CA-Origin info
195+
# An authenticated call using an API Key and CA-Origin info
193196
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000', certtoken='v1.0-...')
194197
```
195198

196199
If the account email and API key are not passed when you create the class, then they are retrieved from either the users exported shell environment variables or the .cloudflare.cfg or ~/.cloudflare.cfg or ~/.cloudflare/cloudflare.cfg files, in that order.
197200

201+
If you're using an API Token, any `cloudflare.cfg` file must not contain an `email` attribute and the `CF_API_EMAIL` environment variable must be unset, otherwise the token will be treated as a key and will throw an error.
202+
198203
There is one call that presently doesn't need any email or token certification (the */ips* call); hence you can test without any values saved away.
199204

200205
### Using shell environment variables
201206
```bash
202-
$ export CF_API_EMAIL='user@example.com'
207+
$ export CF_API_EMAIL='user@example.com' # Do not set if using an API Token
203208
$ export CF_API_KEY='00000000000000000000000000000000'
204209
$ export CF_API_CERTKEY='v1.0-...'
205210
$
@@ -212,7 +217,7 @@ These are optional environment variables; however, they do override the values s
212217
```bash
213218
$ cat ~/.cloudflare/cloudflare.cfg
214219
[CloudFlare]
215-
email = user@example.com
220+
email = user@example.com # Do not set if using an API Token
216221
token = 00000000000000000000000000000000
217222
certtoken = v1.0-...
218223
extras =

README.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ Providing Cloudflare Username and API Key
196196
When you create a **CloudFlare** class you can pass up to four
197197
parameters.
198198

199-
- Account email
200-
- Account API key
199+
- API Token or API Key
200+
- Account email (only if an API Key is being used)
201201
- Optional Origin-CA Certificate Token
202202
- Optional Debug flag (True/False)
203203

@@ -211,17 +211,25 @@ parameters.
211211
# A minimal call with debug enabled
212212
cf = CloudFlare.CloudFlare(debug=True))
213213
214-
# A full blown call with passed basic account information
214+
# An authenticated call using an API Token (note the missing email)
215+
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000')
216+
217+
# An authenticated call using an API Key
215218
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000')
216219
217-
# A full blown call with passed basic account information and CA-Origin info
220+
# An authenticated call using an API Key and CA-Origin info
218221
cf = CloudFlare.CloudFlare(email='user@example.com', token='00000000000000000000000000000000', certtoken='v1.0-...')
219222
220223
If the account email and API key are not passed when you create the
221224
class, then they are retrieved from either the users exported shell
222225
environment variables or the .cloudflare.cfg or ~/.cloudflare.cfg or
223226
~/.cloudflare/cloudflare.cfg files, in that order.
224227

228+
If you're using an API Token, any ``cloudflare.cfg`` file must not
229+
contain an ``email`` attribute and the ``CF_API_EMAIL`` environment
230+
variable must be unset, otherwise the token will be treated as a key
231+
and will throw an error.
232+
225233
There is one call that presently doesn't need any email or token
226234
certification (the */ips* call); hence you can test without any values
227235
saved away.
@@ -231,7 +239,7 @@ Using shell environment variables
231239

232240
.. code:: bash
233241
234-
$ export CF_API_EMAIL='user@example.com'
242+
$ export CF_API_EMAIL='user@example.com' # Do not set if using an API Token
235243
$ export CF_API_KEY='00000000000000000000000000000000'
236244
$ export CF_API_CERTKEY='v1.0-...'
237245
$
@@ -246,7 +254,7 @@ Using configuration file to store email and keys
246254
247255
$ cat ~/.cloudflare/cloudflare.cfg
248256
[CloudFlare]
249-
email = user@example.com
257+
email = user@example.com # Do not set if using an API Token
250258
token = 00000000000000000000000000000000
251259
certtoken = v1.0-...
252260
extras =

0 commit comments

Comments
 (0)