Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify username/password with BasicAuth plugin #201

Merged
merged 1 commit into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/test_auth_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

""" Unit tests for Authentication plugins"""

from websockify.auth_plugins import BasicHTTPAuth, AuthenticationError
import unittest


class BasicHTTPAuthTestCase(unittest.TestCase):

def setUp(self):
self.plugin = BasicHTTPAuth('Aladdin:open sesame')

def test_no_auth(self):
headers = {}
self.assertRaises(AuthenticationError, self.plugin.authenticate, headers, 'localhost', '1234')

def test_invalid_password(self):
headers = {'Authorization': 'Basic QWxhZGRpbjpzZXNhbWUgc3RyZWV0'}
self.assertRaises(AuthenticationError, self.plugin.authenticate, headers, 'localhost', '1234')

def test_valid_password(self):
headers = {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
self.plugin.authenticate(headers, 'localhost', '1234')

def test_garbage_auth(self):
headers = {'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
self.assertRaises(AuthenticationError, self.plugin.authenticate, headers, 'localhost', '1234')
15 changes: 11 additions & 4 deletions websockify/auth_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def __init__(self, expected, actual):


class BasicHTTPAuth(object):
"""Verifies Basic Auth headers. Specify src as username:password"""

def __init__(self, src=None):
self.src = src

def authenticate(self, headers, target_host, target_port):
import base64

auth_header = headers.get('Authorization')
if auth_header:
if not auth_header.startswith('Basic '):
Expand All @@ -46,18 +47,24 @@ def authenticate(self, headers, target_host, target_port):
except TypeError:
raise AuthenticationError(response_code=403)

user_pass = user_pass_raw.split(':', 1)
try:
# http://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication
user_pass_as_text = user_pass_raw.decode('ISO-8859-1')
except UnicodeDecodeError:
raise AuthenticationError(response_code=403)

user_pass = user_pass_as_text.split(':', 1)
if len(user_pass) != 2:
raise AuthenticationError(response_code=403)

if not self.validate_creds:
if not self.validate_creds(*user_pass):
raise AuthenticationError(response_code=403)

else:
raise AuthenticationError(response_code=401,
response_headers={'WWW-Authenticate': 'Basic realm="Websockify"'})

def validate_creds(username, password):
def validate_creds(self, username, password):
if '%s:%s' % (username, password) == self.src:
return True
else:
Expand Down