-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HTTP basic authentication support
- Loading branch information
Showing
10 changed files
with
225 additions
and
30 deletions.
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
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
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
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
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,39 @@ | ||
""" | ||
Security-related functions. | ||
""" | ||
|
||
from flask import g | ||
from flask.ext.login import ( | ||
LoginManager, | ||
login_user, | ||
) | ||
|
||
from .user import User | ||
from .exceptions import AuthenticationRequired | ||
|
||
LOGIN_MANAGER = LoginManager() | ||
|
||
|
||
@LOGIN_MANAGER.user_loader | ||
def load_user(username): | ||
return User(username=username) | ||
|
||
|
||
@LOGIN_MANAGER.unauthorized_handler | ||
def unauthorized(): | ||
raise AuthenticationRequired | ||
|
||
|
||
@LOGIN_MANAGER.request_loader | ||
def load_user_from_request(request): | ||
if request.authorization: | ||
username = request.authorization.username | ||
password = request.authorization.password | ||
|
||
if g.http_server.callbacks['authenticate_user'](username, password): | ||
user = User( | ||
username=request.authorization.username, | ||
) | ||
login_user(user) | ||
|
||
return user |
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,23 @@ | ||
""" | ||
A user class, as required by flask_login. | ||
""" | ||
|
||
from flask.ext.login import UserMixin | ||
|
||
class User(UserMixin): | ||
""" | ||
Represents a user. | ||
""" | ||
|
||
def __init__(self, username): | ||
""" | ||
Initialize a user. | ||
:param username: The username. | ||
""" | ||
super(User, self).__init__() | ||
|
||
self.username = username | ||
|
||
def get_id(self): | ||
return unicode(self.username) |
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
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,39 @@ | ||
""" | ||
Test the exceptions. | ||
""" | ||
|
||
import json | ||
|
||
from unittest import TestCase | ||
|
||
from pyfreelan.server.application import APP | ||
from pyfreelan.server.application.exceptions import HTTPException | ||
|
||
|
||
class ExceptionTests(TestCase): | ||
def setUp(self): | ||
self.app = APP | ||
|
||
def test_http_exception_translates_to_response(self): | ||
message = 'Some error' | ||
status_code = 404 | ||
headers = {'a': '1', 'b': '2'} | ||
|
||
ex = HTTPException( | ||
message=message, | ||
status_code=status_code, | ||
headers=headers, | ||
) | ||
|
||
self.assertEqual(message, ex.message) | ||
self.assertEqual(status_code, ex.status_code) | ||
self.assertEqual(headers, ex.headers) | ||
|
||
with self.app.test_request_context(): | ||
response = ex.to_response() | ||
|
||
self.assertEqual({'message': message}, json.loads(response.data)) | ||
self.assertEqual(status_code, response.status_code) | ||
|
||
for key, value in headers.iteritems(): | ||
self.assertEqual(value, response.headers.get(key)) |
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
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