Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ History
UNRELEASED
++++++++++

nothing yet
- Add initial support for OAuth Mutual TLS (draft-ietf-oauth-mtls)

v1.3.0 (6 November 2019)
++++++++++++++++++++++++
Expand Down
12 changes: 12 additions & 0 deletions docs/oauth2_workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,16 @@ however that you still need to update ``expires_in`` to trigger the refresh.
... auto_refresh_kwargs=extra, token_updater=token_saver)
>>> r = client.get(protected_url)

TLS Client Authentication
-------------------------

To use TLS Client Authentication (draft-ietf-oauth-mtls) via a
self-signed or CA-issued certificate, pass the certificate in the
token request and ensure that the client id is sent in the request:

.. code-block:: pycon

>>> oauth.fetch_token(token_url='https://somesite.com/oauth2/token',
... include_client_id=True, cert=('test-client.pem', 'test-client-key.pem'))

.. _write this section: https://github.com/requests/requests-oauthlib/issues/48
6 changes: 6 additions & 0 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def fetch_token(
proxies=None,
include_client_id=None,
client_secret=None,
cert=None,
**kwargs
):
"""Generic method for fetching an access token from the token endpoint.
Expand Down Expand Up @@ -229,6 +230,10 @@ def fetch_token(
`auth` tuple. If the value is `None`, it will be
omitted from the request, however if the value is
an empty string, an empty string will be sent.
:param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client
Authentication (draft-ietf-oauth-mtls). Can either be the
path of a file containing the private key and certificate or
a tuple of two filenames for certificate and key.
:param kwargs: Extra parameters to include in the token request.
:return: A token dict
"""
Expand Down Expand Up @@ -341,6 +346,7 @@ def fetch_token(
auth=auth,
verify=verify,
proxies=proxies,
cert=cert,
**request_kwargs
)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ def verifier(r, **kwargs):
sess.send = verifier
sess.get("https://i.b")

def test_mtls(self):
cert = (
"testsomething.example-client.pem",
"testsomething.example-client-key.pem",
)

def verifier(r, **kwargs):
self.assertIn("cert", kwargs)
self.assertEqual(cert, kwargs["cert"])
self.assertIn("client_id=" + self.client_id, r.body)
resp = mock.MagicMock()
resp.text = json.dumps(self.token)
return resp

for client in self.clients:
sess = OAuth2Session(client=client)
sess.send = verifier

if isinstance(client, LegacyApplicationClient):
sess.fetch_token(
"https://i.b",
include_client_id=True,
cert=cert,
username="username1",
password="password1",
)
else:
sess.fetch_token("https://i.b", include_client_id=True, cert=cert)

def test_authorization_url(self):
url = "https://example.com/authorize?foo=bar"

Expand Down