Skip to content

Commit

Permalink
changed auth.username to auth.username()
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Sep 26, 2013
1 parent c468e1c commit 5168a5f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
22 changes: 16 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
Copyright (c) 2013, Miguel Grinberg All rights reserved.
The MIT License (MIT)

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Copyright (c) 2013 Miguel Grinberg

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the Flask-HTTPAuth Project.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 3 additions & 3 deletions flask_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def default_auth_error():
self.realm = "Authentication Required"
self.get_password(default_get_password)
self.error_handler(default_auth_error)
self.username = None

def get_password(self, f):
self.get_password_callback = f
Expand All @@ -44,7 +43,6 @@ def decorated(*args, **kwargs):
def login_required(self, f):
@wraps(f)
def decorated(*args, **kwargs):
self.username = None
auth = request.authorization
if not auth:
return self.auth_error_callback()
Expand All @@ -53,10 +51,12 @@ def decorated(*args, **kwargs):
return self.auth_error_callback()
if not self.authenticate(auth, password):
return self.auth_error_callback()
self.username = auth.username
return f(*args, **kwargs)
return decorated

def username(self):
return request.authorization.username

class HTTPBasicAuth(HTTPAuth):
def __init__(self):
super(HTTPBasicAuth, self).__init__()
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

setup(
name='Flask-HTTPAuth',
version='1.1.0',
version='2.0.0',
url='http://github.com/miguelgrinberg/flask-httpauth/',
license='BSD',
license='MIT',
author='Miguel Grinberg',
author_email='[email protected]',
description='Basic and Digest HTTP authentication for Flask routes',
Expand Down
19 changes: 8 additions & 11 deletions test_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,27 @@ def index():
@app.route('/basic')
@basic_auth.login_required
def basic_auth_route():
return "basic_auth"
return "basic_auth:" + basic_auth.username()

@app.route('/basic-with-realm')
@basic_auth_my_realm.login_required
def basic_auth_my_realm_route():
return "basic_auth_my_realm"
return "basic_auth_my_realm:" + basic_auth_my_real.username()

@app.route('/basic-custom')
@basic_custom_auth.login_required
def basic_custom_auth_route():
return "basic_custom_auth"
return "basic_custom_auth:" + basic_custom_auth.username()

@app.route('/digest')
@digest_auth.login_required
def digest_auth_route():
return "digest_auth"
return "digest_auth:" + digest_auth.username()

@app.route('/digest-with-realm')
@digest_auth_my_realm.login_required
def digest_auth_my_realm_route():
return "digest_auth_my_realm"
return "digest_auth_my_realm:" + digest_auth_my_real.username()

self.app = app
self.basic_auth = basic_auth
Expand Down Expand Up @@ -133,8 +133,7 @@ def test_basic_auth_prompt_with_custom_realm(self):
def test_basic_auth_login_valid(self):
response = self.client.get('/basic',
headers = { "Authorization": "Basic " + base64.b64encode(b'john:hello').decode('utf-8').strip("\r\n") })
self.assertTrue(response.data.decode('utf-8') == "basic_auth")
self.assertTrue(self.basic_auth.username == "john")
self.assertTrue(response.data.decode('utf-8') == "basic_auth:john")

def test_basic_auth_login_invalid(self):
response = self.client.get('/basic-with-realm',
Expand All @@ -146,8 +145,7 @@ def test_basic_auth_login_invalid(self):
def test_basic_custom_auth_login_valid(self):
response = self.client.get('/basic-custom',
headers = { "Authorization": "Basic " + base64.b64encode(b'john:hello').decode('utf-8').strip("\r\n") })
self.assertTrue(response.data.decode('utf-8') == "basic_custom_auth")
self.assertTrue(self.basic_custom_auth.username == "john")
self.assertTrue(response.data.decode('utf-8') == "basic_custom_auth:john")

def test_basic_custom_auth_login_invalid(self):
response = self.client.get('/basic-custom',
Expand Down Expand Up @@ -183,8 +181,7 @@ def test_digest_auth_login_valid(self):

response = self.client.get('/digest',
headers = { "Authorization": 'Digest username="john",realm="' + d['realm'] + '",nonce="' + d['nonce'] + '",uri="/digest",response="' + auth_response + '",opaque="' + d['opaque'] + '"' })
self.assertTrue(response.data.decode('utf-8') == "digest_auth")
self.assertTrue(self.digest_auth.username == "john")
self.assertTrue(response.data.decode('utf-8') == "digest_auth:john")

def test_digest_auth_login_bad_realm(self):
response = self.client.get('/digest')
Expand Down

0 comments on commit 5168a5f

Please sign in to comment.