File tree 3 files changed +16
-8
lines changed
3 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -28,3 +28,4 @@ Joel Stevenson
28
28
Brendan McCollam
29
29
Jonathan Huot
30
30
Pieter Ennes
31
+ Olaf Conradi
Original file line number Diff line number Diff line change @@ -16,11 +16,13 @@ A few important facts regarding OAuth security
16
16
17
17
* **Tokens must be random **, OAuthLib provides a method for generating
18
18
secure tokens and it's packed into ``oauthlib.common.generate_token ``,
19
- use it. If you decide to roll your own, use ``random.SystemRandom ``
20
- which is based on ``os.urandom `` rather than the default ``random ``
21
- based on the effecient but not truly random Mersenne Twister.
22
- Predictable tokens allow attackers to bypass virtually all defences
23
- OAuth provides.
19
+ use it. If you decide to roll your own, use ``secrets.SystemRandom ``
20
+ for Python 3.6 and later. The ``secrets `` module is designed for
21
+ generating cryptographically strong random numbers. For earlier versions
22
+ of Python, use ``random.SystemRandom `` which is based on ``os.urandom ``
23
+ rather than the default ``random `` based on the effecient but not truly
24
+ random Mersenne Twister. Predictable tokens allow attackers to bypass
25
+ virtually all defences OAuth provides.
24
26
25
27
* **Timing attacks are real ** and more than possible if you host your
26
28
application inside a shared datacenter. Ensure all ``validate_ `` methods
Original file line number Diff line number Diff line change 11
11
import collections
12
12
import datetime
13
13
import logging
14
- import random
15
14
import re
16
15
import sys
17
16
import time
18
17
18
+ try :
19
+ from secrets import randbits
20
+ from secrets import SystemRandom
21
+ except ImportError :
22
+ from random import getrandbits as randbits
23
+ from random import SystemRandom
19
24
try :
20
25
from urllib import quote as _quote
21
26
from urllib import unquote as _unquote
@@ -202,7 +207,7 @@ def generate_nonce():
202
207
.. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
203
208
.. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
204
209
"""
205
- return unicode_type (unicode_type (random . getrandbits (64 )) + generate_timestamp ())
210
+ return unicode_type (unicode_type (randbits (64 )) + generate_timestamp ())
206
211
207
212
208
213
def generate_timestamp ():
@@ -225,7 +230,7 @@ def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
225
230
and entropy when generating the random characters is important. Which is
226
231
why SystemRandom is used instead of the default random.choice method.
227
232
"""
228
- rand = random . SystemRandom ()
233
+ rand = SystemRandom ()
229
234
return '' .join (rand .choice (chars ) for x in range (length ))
230
235
231
236
You can’t perform that action at this time.
0 commit comments