Skip to content

Commit

Permalink
Merge pull request #370 from mgenereu/master
Browse files Browse the repository at this point in the history
Add HTTP Bearer Auth
  • Loading branch information
sigmavirus24 committed Oct 12, 2023
2 parents c5ac5f3 + 3e0fbab commit c62d977
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev-requirements.txt
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
22 changes: 22 additions & 0 deletions requests_toolbelt/auth/http_bearer.py
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
61 changes: 61 additions & 0 deletions tests/cassettes/httpbin_bearer_auth.json
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}"
}
30 changes: 30 additions & 0 deletions tests/test_auth_bearer.py
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'}

0 comments on commit c62d977

Please sign in to comment.