Skip to content

Commit d21fd53

Browse files
oohlafskion
authored andcommittedApr 13, 2018
Use secrets module in Python 3.6 and later (oauthlib#533)
The secrets module should be used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. In particularly, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.
1 parent d49b9f0 commit d21fd53

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed
 

‎AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ Joel Stevenson
2828
Brendan McCollam
2929
Jonathan Huot
3030
Pieter Ennes
31+
Olaf Conradi

‎docs/oauth1/security.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ A few important facts regarding OAuth security
1616

1717
* **Tokens must be random**, OAuthLib provides a method for generating
1818
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.
2426

2527
* **Timing attacks are real** and more than possible if you host your
2628
application inside a shared datacenter. Ensure all ``validate_`` methods

‎oauthlib/common.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
import collections
1212
import datetime
1313
import logging
14-
import random
1514
import re
1615
import sys
1716
import time
1817

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
1924
try:
2025
from urllib import quote as _quote
2126
from urllib import unquote as _unquote
@@ -202,7 +207,7 @@ def generate_nonce():
202207
.. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
203208
.. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
204209
"""
205-
return unicode_type(unicode_type(random.getrandbits(64)) + generate_timestamp())
210+
return unicode_type(unicode_type(randbits(64)) + generate_timestamp())
206211

207212

208213
def generate_timestamp():
@@ -225,7 +230,7 @@ def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
225230
and entropy when generating the random characters is important. Which is
226231
why SystemRandom is used instead of the default random.choice method.
227232
"""
228-
rand = random.SystemRandom()
233+
rand = SystemRandom()
229234
return ''.join(rand.choice(chars) for x in range(length))
230235

231236

0 commit comments

Comments
 (0)
Please sign in to comment.