| 1 |
import contextlib |
| 2 |
import hmac |
| 3 |
import unittest |
| 4 |
import urllib.error |
| 5 |
|
| 6 |
from websockets.headers import build_authorization_basic |
| 7 |
from websockets.legacy.auth import * |
| 8 |
from websockets.legacy.auth import is_credentials |
| 9 |
from websockets.legacy.exceptions import InvalidStatusCode |
| 10 |
|
| 11 |
from .test_client_server import ClientServerTestsMixin, with_client, with_server |
| 12 |
from .utils import AsyncioTestCase |
| 13 |
|
| 14 |
|
| 15 |
class AuthTests(unittest.TestCase): |
| 16 |
def test_is_credentials(self): |
| 17 |
self.assertTrue(is_credentials(("username", "password"))) |
| 18 |
|
| 19 |
def test_is_not_credentials(self): |
| 20 |
self.assertFalse(is_credentials(None)) |
| 21 |
self.assertFalse(is_credentials("username")) |
| 22 |
|
| 23 |
|
| 24 |
class CustomWebSocketServerProtocol(BasicAuthWebSocketServerProtocol): |
| 25 |
async def process_request(self, path, request_headers): |
| 26 |
type(self).used = True |
| 27 |
return await super().process_request(path, request_headers) |
| 28 |
|
| 29 |
|
| 30 |
class CheckWebSocketServerProtocol(BasicAuthWebSocketServerProtocol): |
| 31 |
async def check_credentials(self, username, password): |
| 32 |
return hmac.compare_digest(password, "letmein") |
| 33 |
|
| 34 |
|
| 35 |
class AuthClientServerTests(ClientServerTestsMixin, AsyncioTestCase): |
| 36 |
create_protocol = basic_auth_protocol_factory( |
| 37 |
realm="auth-tests", credentials=("hello", "iloveyou") |
| 38 |
) |
| 39 |
|
| 40 |
@with_server(create_protocol=create_protocol) |
| 41 |
@with_client(user_info=("hello", "iloveyou")) |
| 42 |
def test_basic_auth(self): |
| 43 |
req_headers = self.client.request_headers |
| 44 |
resp_headers = self.client.response_headers |
| 45 |
self.assertEqual(req_headers["Authorization"], "Basic aGVsbG86aWxvdmV5b3U=") |
| 46 |
self.assertNotIn("WWW-Authenticate", resp_headers) |
| 47 |
|
| 48 |
self.loop.run_until_complete(self.client.send("Hello!")) |
| 49 |
self.loop.run_until_complete(self.client.recv()) |
| 50 |
|
| 51 |
def test_basic_auth_server_no_credentials(self): |
| 52 |
with self.assertRaises(TypeError) as raised: |
| 53 |
basic_auth_protocol_factory(realm="auth-tests", credentials=None) |
| 54 |
self.assertEqual( |
| 55 |
str(raised.exception), "provide either credentials or check_credentials" |
| 56 |
) |
| 57 |
|
| 58 |
def test_basic_auth_server_bad_credentials(self): |
| 59 |
with self.assertRaises(TypeError) as raised: |
| 60 |
basic_auth_protocol_factory(realm="auth-tests", credentials=42) |
| 61 |
self.assertEqual(str(raised.exception), "invalid credentials argument: 42") |
| 62 |
|
| 63 |
create_protocol_multiple_credentials = basic_auth_protocol_factory( |
| 64 |
realm="auth-tests", |
| 65 |
credentials=[("hello", "iloveyou"), ("goodbye", "stillloveu")], |
| 66 |
) |
| 67 |
|
| 68 |
@with_server(create_protocol=create_protocol_multiple_credentials) |
| 69 |
@with_client(user_info=("hello", "iloveyou")) |
| 70 |
def test_basic_auth_server_multiple_credentials(self): |
| 71 |
self.loop.run_until_complete(self.client.send("Hello!")) |
| 72 |
self.loop.run_until_complete(self.client.recv()) |
| 73 |
|
| 74 |
def test_basic_auth_bad_multiple_credentials(self): |
| 75 |
with self.assertRaises(TypeError) as raised: |
| 76 |
basic_auth_protocol_factory( |
| 77 |
realm="auth-tests", credentials=[("hello", "iloveyou"), 42] |
| 78 |
) |
| 79 |
self.assertEqual( |
| 80 |
str(raised.exception), |
| 81 |
"invalid credentials argument: [('hello', 'iloveyou'), 42]", |
| 82 |
) |
| 83 |
|
| 84 |
async def check_credentials(username, password): |
| 85 |
return hmac.compare_digest(password, "iloveyou") |
| 86 |
|
| 87 |
create_protocol_check_credentials = basic_auth_protocol_factory( |
| 88 |
realm="auth-tests", |
| 89 |
check_credentials=check_credentials, |
| 90 |
) |
| 91 |
|
| 92 |
@with_server(create_protocol=create_protocol_check_credentials) |
| 93 |
@with_client(user_info=("hello", "iloveyou")) |
| 94 |
def test_basic_auth_check_credentials(self): |
| 95 |
self.loop.run_until_complete(self.client.send("Hello!")) |
| 96 |
self.loop.run_until_complete(self.client.recv()) |
| 97 |
|
| 98 |
create_protocol_custom_protocol = basic_auth_protocol_factory( |
| 99 |
realm="auth-tests", |
| 100 |
credentials=[("hello", "iloveyou")], |
| 101 |
create_protocol=CustomWebSocketServerProtocol, |
| 102 |
) |
| 103 |
|
| 104 |
@with_server(create_protocol=create_protocol_custom_protocol) |
| 105 |
@with_client(user_info=("hello", "iloveyou")) |
| 106 |
def test_basic_auth_custom_protocol(self): |
| 107 |
self.assertTrue(CustomWebSocketServerProtocol.used) |
| 108 |
del CustomWebSocketServerProtocol.used |
| 109 |
self.loop.run_until_complete(self.client.send("Hello!")) |
| 110 |
self.loop.run_until_complete(self.client.recv()) |
| 111 |
|
| 112 |
@with_server(create_protocol=CheckWebSocketServerProtocol) |
| 113 |
@with_client(user_info=("hello", "letmein")) |
| 114 |
def test_basic_auth_custom_protocol_subclass(self): |
| 115 |
self.loop.run_until_complete(self.client.send("Hello!")) |
| 116 |
self.loop.run_until_complete(self.client.recv()) |
| 117 |
|
| 118 |
# CustomWebSocketServerProtocol doesn't override check_credentials |
| 119 |
@with_server(create_protocol=CustomWebSocketServerProtocol) |
| 120 |
def test_basic_auth_defaults_to_deny_all(self): |
| 121 |
with self.assertRaises(InvalidStatusCode) as raised: |
| 122 |
self.start_client(user_info=("hello", "iloveyou")) |
| 123 |
self.assertEqual(raised.exception.status_code, 401) |
| 124 |
|
| 125 |
@with_server(create_protocol=create_protocol) |
| 126 |
def test_basic_auth_missing_credentials(self): |
| 127 |
with self.assertRaises(InvalidStatusCode) as raised: |
| 128 |
self.start_client() |
| 129 |
self.assertEqual(raised.exception.status_code, 401) |
| 130 |
|
| 131 |
@with_server(create_protocol=create_protocol) |
| 132 |
def test_basic_auth_missing_credentials_details(self): |
| 133 |
with self.assertRaises(urllib.error.HTTPError) as raised: |
| 134 |
self.loop.run_until_complete(self.make_http_request()) |
| 135 |
with contextlib.closing(raised.exception): |
| 136 |
self.assertEqual(raised.exception.code, 401) |
| 137 |
self.assertEqual( |
| 138 |
raised.exception.headers["WWW-Authenticate"], |
| 139 |
'Basic realm="auth-tests", charset="UTF-8"', |
| 140 |
) |
| 141 |
self.assertEqual(raised.exception.read().decode(), "Missing credentials\n") |
| 142 |
|
| 143 |
@with_server(create_protocol=create_protocol) |
| 144 |
def test_basic_auth_unsupported_credentials(self): |
| 145 |
with self.assertRaises(InvalidStatusCode) as raised: |
| 146 |
self.start_client(extra_headers={"Authorization": "Digest ..."}) |
| 147 |
self.assertEqual(raised.exception.status_code, 401) |
| 148 |
|
| 149 |
@with_server(create_protocol=create_protocol) |
| 150 |
def test_basic_auth_unsupported_credentials_details(self): |
| 151 |
with self.assertRaises(urllib.error.HTTPError) as raised: |
| 152 |
self.loop.run_until_complete( |
| 153 |
self.make_http_request(headers={"Authorization": "Digest ..."}) |
| 154 |
) |
| 155 |
with contextlib.closing(raised.exception): |
| 156 |
self.assertEqual(raised.exception.code, 401) |
| 157 |
self.assertEqual( |
| 158 |
raised.exception.headers["WWW-Authenticate"], |
| 159 |
'Basic realm="auth-tests", charset="UTF-8"', |
| 160 |
) |
| 161 |
self.assertEqual( |
| 162 |
raised.exception.read().decode(), "Unsupported credentials\n" |
| 163 |
) |
| 164 |
|
| 165 |
@with_server(create_protocol=create_protocol) |
| 166 |
def test_basic_auth_invalid_username(self): |
| 167 |
with self.assertRaises(InvalidStatusCode) as raised: |
| 168 |
self.start_client(user_info=("goodbye", "iloveyou")) |
| 169 |
self.assertEqual(raised.exception.status_code, 401) |
| 170 |
|
| 171 |
@with_server(create_protocol=create_protocol) |
| 172 |
def test_basic_auth_invalid_password(self): |
| 173 |
with self.assertRaises(InvalidStatusCode) as raised: |
| 174 |
self.start_client(user_info=("hello", "ihateyou")) |
| 175 |
self.assertEqual(raised.exception.status_code, 401) |
| 176 |
|
| 177 |
@with_server(create_protocol=create_protocol) |
| 178 |
def test_basic_auth_invalid_credentials_details(self): |
| 179 |
with self.assertRaises(urllib.error.HTTPError) as raised: |
| 180 |
authorization = build_authorization_basic("hello", "ihateyou") |
| 181 |
self.loop.run_until_complete( |
| 182 |
self.make_http_request(headers={"Authorization": authorization}) |
| 183 |
) |
| 184 |
with contextlib.closing(raised.exception): |
| 185 |
self.assertEqual(raised.exception.code, 401) |
| 186 |
self.assertEqual( |
| 187 |
raised.exception.headers["WWW-Authenticate"], |
| 188 |
'Basic realm="auth-tests", charset="UTF-8"', |
| 189 |
) |
| 190 |
self.assertEqual(raised.exception.read().decode(), "Invalid credentials\n") |