| 1 |
""" |
| 2 |
Tests for authentication encoding utils. |
| 3 |
""" |
| 4 |
|
| 5 |
import base64 |
| 6 |
import hmac |
| 7 |
|
| 8 |
import pytest |
| 9 |
from hypothesis import given |
| 10 |
from hypothesis.strategies import binary, text |
| 11 |
|
| 12 |
from aiosmtplib.auth import ( |
| 13 |
auth_crammd5_verify, |
| 14 |
auth_login_encode, |
| 15 |
auth_plain_encode, |
| 16 |
auth_xoauth2_encode, |
| 17 |
) |
| 18 |
from aiosmtplib.errors import SMTPAuthenticationError |
| 19 |
from aiosmtplib.typing import SMTPStatus |
| 20 |
|
| 21 |
|
| 22 |
@given(binary(), binary(), binary()) |
| 23 |
def test_auth_crammd5_verify_bytes( |
| 24 |
username: bytes, |
| 25 |
password: bytes, |
| 26 |
challenge: bytes, |
| 27 |
) -> None: |
| 28 |
encoded_challenge = base64.b64encode(challenge) |
| 29 |
|
| 30 |
# Basically a re-implementation of the function being tested :( |
| 31 |
md5_digest = hmac.new(password, msg=challenge, digestmod="md5") |
| 32 |
verification = username + b" " + md5_digest.hexdigest().encode("ascii") |
| 33 |
encoded_verification = base64.b64encode(verification) |
| 34 |
|
| 35 |
result = auth_crammd5_verify(username, password, encoded_challenge) |
| 36 |
|
| 37 |
assert result == encoded_verification |
| 38 |
|
| 39 |
|
| 40 |
@given(text(), text(), text()) |
| 41 |
def test_auth_crammd5_verify_str( |
| 42 |
username: str, |
| 43 |
password: str, |
| 44 |
challenge: str, |
| 45 |
) -> None: |
| 46 |
username_bytes = username.encode("utf-8") |
| 47 |
password_bytes = password.encode("utf-8") |
| 48 |
challenge_bytes = challenge.encode("utf-8") |
| 49 |
encoded_challenge = base64.b64encode(challenge_bytes) |
| 50 |
|
| 51 |
# Basically a re-implementation of the function being tested :( |
| 52 |
md5_digest = hmac.new(password_bytes, msg=challenge_bytes, digestmod="md5") |
| 53 |
verification = username_bytes + b" " + md5_digest.hexdigest().encode("ascii") |
| 54 |
encoded_verification = base64.b64encode(verification) |
| 55 |
|
| 56 |
result = auth_crammd5_verify(username, password, encoded_challenge) |
| 57 |
|
| 58 |
assert result == encoded_verification |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize( |
| 62 |
"challenge", |
| 63 |
["abc", "not base64!!", b"abc"], |
| 64 |
ids=["bad padding", "bad length", "bytes"], |
| 65 |
) |
| 66 |
def test_auth_crammd5_verify_invalid_challenge(challenge: str | bytes) -> None: |
| 67 |
with pytest.raises(SMTPAuthenticationError) as excinfo: |
| 68 |
auth_crammd5_verify("username", "password", challenge) |
| 69 |
|
| 70 |
assert excinfo.value.code == SMTPStatus.auth_continue |
| 71 |
assert "CRAM-MD5" in excinfo.value.message |
| 72 |
|
| 73 |
|
| 74 |
@given(binary(), binary()) |
| 75 |
def test_auth_plain_encode_bytes( |
| 76 |
username: bytes, |
| 77 |
password: bytes, |
| 78 |
) -> None: |
| 79 |
assert auth_plain_encode(username, password) == base64.b64encode( |
| 80 |
b"\0" + username + b"\0" + password |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
@given(text(), text()) |
| 85 |
def test_auth_plain_encode_str( |
| 86 |
username: str, |
| 87 |
password: str, |
| 88 |
) -> None: |
| 89 |
username_bytes = username.encode("utf-8") |
| 90 |
password_bytes = password.encode("utf-8") |
| 91 |
|
| 92 |
assert auth_plain_encode(username, password) == base64.b64encode( |
| 93 |
b"\0" + username_bytes + b"\0" + password_bytes |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
@given(binary(), binary()) |
| 98 |
def test_auth_login_encode_bytes( |
| 99 |
username: bytes, |
| 100 |
password: bytes, |
| 101 |
) -> None: |
| 102 |
assert auth_login_encode(username, password) == ( |
| 103 |
base64.b64encode(username), |
| 104 |
base64.b64encode(password), |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
@given(text(), text()) |
| 109 |
def test_auth_login_encode_str( |
| 110 |
username: str, |
| 111 |
password: str, |
| 112 |
) -> None: |
| 113 |
username_bytes = username.encode("utf-8") |
| 114 |
password_bytes = password.encode("utf-8") |
| 115 |
|
| 116 |
assert auth_login_encode(username, password) == ( |
| 117 |
base64.b64encode(username_bytes), |
| 118 |
base64.b64encode(password_bytes), |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
@given(binary(), binary()) |
| 123 |
def test_auth_xoauth2_encode_bytes( |
| 124 |
username: bytes, |
| 125 |
access_token: bytes, |
| 126 |
) -> None: |
| 127 |
auth_string = b"user=" + username + b"\x01auth=Bearer " + access_token + b"\x01\x01" |
| 128 |
assert auth_xoauth2_encode(username, access_token) == base64.b64encode(auth_string) |
| 129 |
|
| 130 |
|
| 131 |
@given(text(), text()) |
| 132 |
def test_auth_xoauth2_encode_str( |
| 133 |
username: str, |
| 134 |
access_token: str, |
| 135 |
) -> None: |
| 136 |
username_bytes = username.encode("utf-8") |
| 137 |
token_bytes = access_token.encode("utf-8") |
| 138 |
|
| 139 |
auth_string = ( |
| 140 |
b"user=" + username_bytes + b"\x01auth=Bearer " + token_bytes + b"\x01\x01" |
| 141 |
) |
| 142 |
assert auth_xoauth2_encode(username, access_token) == base64.b64encode(auth_string) |
| 143 |
|
| 144 |
|
| 145 |
def test_auth_xoauth2_encode_known_value() -> None: |
| 146 |
"""Test against the example from Google's XOAUTH2 documentation.""" |
| 147 |
username = "[email protected]" |
| 148 |
token = "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg" |
| 149 |
|
| 150 |
result = auth_xoauth2_encode(username, token) |
| 151 |
|
| 152 |
# Decode and verify the structure |
| 153 |
decoded = base64.b64decode(result) |
| 154 |
assert decoded == ( |
| 155 |
b"[email protected]\x01" |
| 156 |
b"auth=Bearer ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg\x01\x01" |
| 157 |
) |