-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from mgenereu/master
Add HTTP Bearer Auth
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pytest | ||
mock;python_version<"3.3" | ||
pyopenssl | ||
git+git://github.com/sigmavirus24/betamax | ||
git+https://github.com/betamaxpy/betamax | ||
trustme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The module containing HTTPBearerAuth.""" | ||
|
||
from requests.auth import AuthBase | ||
|
||
|
||
class HTTPBearerAuth(AuthBase): | ||
"""HTTP Bearer Token Authentication | ||
""" | ||
|
||
def __init__(self, token): | ||
self.token = token | ||
|
||
def __eq__(self, other): | ||
return self.token == getattr(other, 'token', None) | ||
|
||
def __ne__(self, other): | ||
return not self == other | ||
|
||
def __call__(self, r): | ||
r.headers['Authorization'] = 'Bearer ' + self.token | ||
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"http_interactions": [ | ||
{ | ||
"request": { | ||
"body": { | ||
"string": "", | ||
"encoding": "utf-8" | ||
}, | ||
"headers": { | ||
"Accept": [ | ||
"*/*" | ||
], | ||
"Accept-Encoding": [ | ||
"gzip, deflate, compress" | ||
], | ||
"Authorization": [ | ||
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c=" | ||
], | ||
"User-Agent": [ | ||
"python-requests/2.2.1 CPython/2.7.6 Linux/3.14.1-1-ARCH" | ||
] | ||
}, | ||
"method": "GET", | ||
"uri": "http://httpbin.org/bearer-auth/" | ||
}, | ||
"response": { | ||
"body": { | ||
"string": "{\n \"user\": \"user\",\n \"authenticated\": true\n}", | ||
"encoding": null | ||
}, | ||
"headers": { | ||
"content-length": [ | ||
"45" | ||
], | ||
"server": [ | ||
"gunicorn/0.17.4" | ||
], | ||
"connection": [ | ||
"keep-alive" | ||
], | ||
"date": [ | ||
"Sat, 03 May 2014 17:23:06 GMT" | ||
], | ||
"access-control-allow-origin": [ | ||
"*" | ||
], | ||
"content-type": [ | ||
"application/json" | ||
] | ||
}, | ||
"status": { | ||
"message": "OK", | ||
"code": 200 | ||
}, | ||
"url": "http://httpbin.org/bearer-auth/" | ||
}, | ||
"recorded_at": "2014-05-03T17:23:06" | ||
} | ||
], | ||
"recorded_with": "betamax/{version}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
import requests | ||
import unittest | ||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
from requests_toolbelt.auth.http_bearer import HTTPBearerAuth | ||
from . import get_betamax | ||
|
||
|
||
class TestBearerAuth(unittest.TestCase): | ||
def setUp(self): | ||
self.session = requests.Session() | ||
self.recorder = get_betamax(self.session) | ||
|
||
def cassette(self): | ||
return self.recorder.use_cassette( | ||
'httpbin_bearer_auth', | ||
match_requests_on=['method', 'uri'] | ||
) | ||
|
||
def test_bearer(self): | ||
with self.cassette(): | ||
r = self.session.request( | ||
'GET', 'http://httpbin.org/bearer-auth/', | ||
auth=HTTPBearerAuth('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c')) | ||
|
||
assert r.json() == {'authenticated': True, 'user': 'user'} |