Skip to content

Commit

Permalink
lint + remove unnecessary get_auth_password
Browse files Browse the repository at this point in the history
  • Loading branch information
mattproetsch committed Aug 3, 2024
1 parent 98ec906 commit bfaccfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/flask_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ def ensure_sync(self, f):

class HTTPCookieAuth(HTTPAuth):
def __init__(self, scheme=None, realm=None, cookie_name=None):
super(HTTPCookieAuth, self).__init__(scheme or 'Bearer', realm, cookie_name)
super(HTTPCookieAuth, self).__init__(
scheme or 'Bearer',
realm,
cookie_name
)

self.verify_cookie_callback = None
self.cookie_name = cookie_name
Expand All @@ -216,7 +220,8 @@ def get_auth(self):
cookie_val = request.cookies.get(expected_cookie_name, '')
token = ''
if self.scheme != 'ApiKey':
# if scheme is Bearer or anything else besides ApiKey, split on scheme name
# if scheme is Bearer or anything else besides ApiKey,
# split on scheme name
if isinstance(cookie_val, str) and len(cookie_val) > 0:
try:
scheme, token = cookie_val.split(' ')
Expand All @@ -232,12 +237,6 @@ def get_auth(self):
auth = Authorization(self.scheme, token=token)
return auth

def get_auth_password(self, auth):
try:
return getattr(auth, 'token', '')
except KeyError:
return ""


class HTTPBasicAuth(HTTPAuth):
def __init__(self, scheme=None, realm=None):
Expand Down
9 changes: 5 additions & 4 deletions tests/test_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def cookie_default_auth_route():
self.app = app
self.cookie_auth = cookie_auth
self.client = app.test_client()

def tearDown(self) -> None:
self.client._cookies.clear()

Expand Down Expand Up @@ -96,7 +96,8 @@ def test_cookie_auth_login_optional(self):
self.assertEqual(response.data.decode('utf-8'), 'cookie_auth:None')

def test_cookie_auth_login_invalid_token(self):
self.client.set_cookie("Authorization", "MyToken this-is-not-the-token!")
self.client.set_cookie("Authorization",
"MyToken this-is-not-the-token!")
response = self.client.get('/protected')
self.assertEqual(response.status_code, 401)
self.assertTrue('WWW-Authenticate' in response.headers)
Expand Down Expand Up @@ -151,7 +152,7 @@ def test_cookie_auth_header_precedence(self):
self.client.set_cookie("X-API-Key", "this-is-the-token!")
basic_creds = base64.b64encode(b'susan:bye').decode('utf-8')
response = self.client.get(
'/protected3', headers={'Authorization': 'Basic ' + basic_creds,})
'/protected3', headers={'Authorization': 'Basic ' + basic_creds})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode('utf-8'), 'cookie_auth3:user')

Expand Down Expand Up @@ -189,4 +190,4 @@ def test_cookie_auth_default_bearer_missing_cookie(self):
self.assertEqual(response.status_code, 401)
self.assertTrue('WWW-Authenticate' in response.headers)
self.assertEqual(response.headers['WWW-Authenticate'],
'Bearer realm="Authentication Required"')
'Bearer realm="Authentication Required"')
9 changes: 5 additions & 4 deletions tests/test_cookie_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def cookie_default_auth_route():
self.app = app
self.cookie_auth = cookie_auth
self.client = app.test_client()

def tearDown(self) -> None:
self.client._cookies.clear()

Expand Down Expand Up @@ -96,7 +96,8 @@ def test_cookie_auth_login_optional(self):
self.assertEqual(response.data.decode('utf-8'), 'cookie_auth:None')

def test_cookie_auth_login_invalid_token(self):
self.client.set_cookie("Authorization", "MyToken this-is-not-the-token!")
self.client.set_cookie("Authorization",
"MyToken this-is-not-the-token!")
response = self.client.get('/protected')
self.assertEqual(response.status_code, 401)
self.assertTrue('WWW-Authenticate' in response.headers)
Expand Down Expand Up @@ -151,7 +152,7 @@ def test_cookie_auth_header_precedence(self):
self.client.set_cookie("X-API-Key", "this-is-the-token!")
basic_creds = base64.b64encode(b'susan:bye').decode('utf-8')
response = self.client.get(
'/protected3', headers={'Authorization': 'Basic ' + basic_creds,})
'/protected3', headers={'Authorization': 'Basic ' + basic_creds})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode('utf-8'), 'cookie_auth3:user')

Expand Down Expand Up @@ -189,4 +190,4 @@ def test_cookie_auth_default_bearer_missing_cookie(self):
self.assertEqual(response.status_code, 401)
self.assertTrue('WWW-Authenticate' in response.headers)
self.assertEqual(response.headers['WWW-Authenticate'],
'Bearer realm="Authentication Required"')
'Bearer realm="Authentication Required"')

0 comments on commit bfaccfe

Please sign in to comment.