Skip to content

Commit

Permalink
Merge 9728216 into db6fe59
Browse files Browse the repository at this point in the history
  • Loading branch information
carsongee committed Feb 10, 2020
2 parents db6fe59 + 9728216 commit 4f6b69c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
8 changes: 5 additions & 3 deletions flask_htpasswd.py
Expand Up @@ -17,7 +17,7 @@
log = logging.getLogger(__name__) # pylint: disable=invalid-name


class HtPasswdAuth(object):
class HtPasswdAuth:
"""Configure htpasswd based basic and token authentication."""

def __init__(self, app=None):
Expand All @@ -31,6 +31,7 @@ def init_app(self, app):
"""
Find and configure the user database from specified file
"""
# pylint: disable=inconsistent-return-statements
app.config.setdefault('FLASK_AUTH_ALL', False)
app.config.setdefault('FLASK_AUTH_REALM', 'Login Required')
# Default set to bad file to trigger IOError
Expand All @@ -48,10 +49,11 @@ def init_app(self, app):

# Allow requiring auth for entire app, with pre request method
@app.before_request
def require_auth(): # pylint: disable=unused-variable
def require_auth():
# pylint: disable=unused-variable
"""Pre request processing for enabling full app authentication."""
if not current_app.config['FLASK_AUTH_ALL']:
return
return None
is_valid, user = self.authenticate()
if not is_valid:
return self.auth_failed()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -45,6 +45,7 @@ def finalize_options(self):

def run_tests(self):
# Import here, cause outside the eggs aren't loaded
# pylint: disable=import-outside-toplevel
import tox # pylint: disable=import-error
import shlex
args = self.tox_args
Expand Down
26 changes: 13 additions & 13 deletions tests/test_htpasswd.py
Expand Up @@ -7,11 +7,11 @@
import unittest

from flask import request, Flask, g
# pylint: disable=no-name-in-module,import-error
from flask.ext.htpasswd import HtPasswdAuth
from itsdangerous import JSONWebSignatureSerializer as Serializer
import mock

from flask_htpasswd import HtPasswdAuth


class TestAuth(unittest.TestCase):
"""
Expand Down Expand Up @@ -189,13 +189,13 @@ def test_requires_auth(self):
self._setup_normal_extension()
# Test successful basic auth
with self.app.test_request_context(headers={
'Authorization': 'Basic {0}'.format(
base64.b64encode(
'{0}:{1}'.format(
self.TEST_USER, self.TEST_PASS
).encode('ascii')
).decode('ascii')
)
'Authorization': 'Basic {0}'.format(
base64.b64encode(
'{0}:{1}'.format(
self.TEST_USER, self.TEST_PASS
).encode('ascii')
).decode('ascii')
)
}):
wrapped, decorated = self._get_requires_auth_decorator()
decorated()
Expand All @@ -204,9 +204,9 @@ def test_requires_auth(self):
# Test successful token header auth
with self.app.app_context():
with self.app.test_request_context(headers={
'Authorization': 'token {0}'.format(
self.htpasswd.generate_token(self.TEST_USER)
)
'Authorization': 'token {0}'.format(
self.htpasswd.generate_token(self.TEST_USER)
)
}):
wrapped, decorated = self._get_requires_auth_decorator()
decorated()
Expand All @@ -227,7 +227,7 @@ def test_requires_auth(self):

# Test unsuccessful auth
with self.app.test_request_context(headers={
'Authorization': 'token blah blah'
'Authorization': 'token blah blah'
}):
wrapped, decorated = self._get_requires_auth_decorator()
response = decorated()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py{27,33,34,35,36}
envlist = py{35,36,37,38}
skip_missing_interpreters = True

[testenv]
Expand Down

0 comments on commit 4f6b69c

Please sign in to comment.