| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import spnego |
| 8 |
import spnego._gss |
| 9 |
from spnego._context import GSSMech |
| 10 |
from spnego._negotiate import NegotiateProxy |
| 11 |
from spnego._spnego import NegState, NegTokenInit, NegTokenResp |
| 12 |
from spnego.exceptions import BadMechanismError, InvalidTokenError |
| 13 |
|
| 14 |
|
| 15 |
def test_token_rejected(ntlm_cred): |
| 16 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], options=spnego.NegotiateOptions.use_negotiate) |
| 17 |
|
| 18 |
c.step() |
| 19 |
token_resp = NegTokenResp(neg_state=NegState.reject).pack() |
| 20 |
|
| 21 |
with pytest.raises(InvalidTokenError, match="Received SPNEGO rejection with no token error message"): |
| 22 |
c.step(token_resp) |
| 23 |
|
| 24 |
|
| 25 |
def test_token_invalid_input(ntlm_cred): |
| 26 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], options=spnego.NegotiateOptions.use_negotiate) |
| 27 |
|
| 28 |
c.step() |
| 29 |
with pytest.raises(InvalidTokenError, match="Failed to unpack input token"): |
| 30 |
c.step(b"\x00") |
| 31 |
|
| 32 |
|
| 33 |
def test_token_no_common_mechs(ntlm_cred): |
| 34 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], options=spnego.NegotiateOptions.use_negotiate) |
| 35 |
|
| 36 |
with pytest.raises(BadMechanismError, match="Unable to negotiate common mechanism"): |
| 37 |
c.step(NegTokenInit(mech_types=["1.2.3.4"]).pack()) |
| 38 |
|
| 39 |
|
| 40 |
def test_token_acceptor_first(ntlm_cred): |
| 41 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], options=spnego.NegotiateOptions.use_negotiate) |
| 42 |
s = spnego.server(options=spnego.NegotiateOptions.use_negotiate) |
| 43 |
|
| 44 |
assert getattr(c, "_mech_list", None) == [] |
| 45 |
assert getattr(s, "_mech_list", None) == [] |
| 46 |
|
| 47 |
token1 = s.step() |
| 48 |
assert isinstance(token1, bytes) |
| 49 |
assert not c.complete |
| 50 |
assert not s.complete |
| 51 |
assert getattr(c, "_mech_list", None) == [] |
| 52 |
assert GSSMech.ntlm.value in getattr(s, "_mech_list", []) |
| 53 |
|
| 54 |
negotiate = c.step(token1) |
| 55 |
assert isinstance(negotiate, bytes) |
| 56 |
assert not c.complete |
| 57 |
assert not s.complete |
| 58 |
assert getattr(c, "_mech_list", None) == [GSSMech.ntlm.value] |
| 59 |
assert GSSMech.ntlm.value in getattr(s, "_mech_list", []) |
| 60 |
|
| 61 |
challenge = s.step(negotiate) |
| 62 |
assert isinstance(challenge, bytes) |
| 63 |
assert not c.complete |
| 64 |
assert not s.complete |
| 65 |
|
| 66 |
authenticate = c.step(challenge) |
| 67 |
assert isinstance(authenticate, bytes) |
| 68 |
assert not c.complete |
| 69 |
assert not s.complete |
| 70 |
|
| 71 |
mech_list_mic = s.step(authenticate) |
| 72 |
assert isinstance(mech_list_mic, bytes) |
| 73 |
assert not c.complete |
| 74 |
assert s.complete |
| 75 |
|
| 76 |
final_token = c.step(mech_list_mic) |
| 77 |
assert final_token is None |
| 78 |
assert c.complete |
| 79 |
assert s.complete |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 83 |
def test_iov_available_sspi(): |
| 84 |
assert NegotiateProxy.iov_available() |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.skipif(not spnego._gss.HAS_GSSAPI, reason="Requires the gssapi library to be installed for testing") |
| 88 |
def test_iov_available_gssapi(): |
| 89 |
assert NegotiateProxy.iov_available() == spnego._gss.GSSAPIProxy.iov_available() |