Skip to content

Commit 3e0371a

Browse files
razzzpRazif PRAMUDA
and
Razif PRAMUDA
authored
fix #181 add password param to constructor (#182)
* fix #181 split principal into user@realm and password * add test for principal with password * added password to constructor instead * update test to handle password --------- Co-authored-by: Razif PRAMUDA <[email protected]>
1 parent 3f672cf commit 3e0371a

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ env/
44
build/
55
dist/
66
requests_kerberos.egg-info/
7-
7+
.venv
8+
.vscode

requests_kerberos/kerberos_.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def __init__(
169169
self, mutual_authentication=REQUIRED,
170170
service="HTTP", delegate=False, force_preemptive=False,
171171
principal=None, hostname_override=None,
172-
sanitize_mutual_error_response=True, send_cbt=True):
172+
sanitize_mutual_error_response=True, send_cbt=True, password=None):
173173
self._context = {}
174174
self.mutual_authentication = mutual_authentication
175175
self.delegate = delegate
@@ -180,6 +180,7 @@ def __init__(
180180
self.hostname_override = hostname_override
181181
self.sanitize_mutual_error_response = sanitize_mutual_error_response
182182
self.auth_done = False
183+
self.password = password
183184

184185
# Set the CBT values populated after the first response
185186
self.send_cbt = send_cbt
@@ -211,13 +212,15 @@ def generate_request_header(self, response, host, is_preemptive=False):
211212

212213
self._context[host] = ctx = spnego.client(
213214
username=self.principal,
215+
password=self.password,
214216
hostname=kerb_host,
215217
service=self.service,
216218
channel_bindings=self._cbts.get(host, None),
217219
context_req=gssflags,
218220
protocol="kerberos",
219221
)
220222

223+
221224
# if we have a previous response from the server, use it to continue
222225
# the auth process, otherwise use an empty value
223226
negotiate_resp_value = None if is_preemptive else _negotiate_value(response)

tests/test_requests_kerberos.py

+32
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def test_generate_request_header(mock_client):
7272
assert mock_client.call_count == 1
7373
assert mock_client.call_args[1] == {
7474
"username": None,
75+
"password": None,
7576
"hostname": "www.example.org",
7677
"service": "HTTP",
7778
"channel_bindings": None,
@@ -99,6 +100,7 @@ def test_generate_request_header_init_error(mock_client):
99100
assert mock_client.call_count == 1
100101
assert mock_client.call_args[1] == {
101102
"username": None,
103+
"password": None,
102104
"hostname": "www.example.org",
103105
"service": "HTTP",
104106
"channel_bindings": None,
@@ -123,6 +125,7 @@ def test_generate_request_header_step_error(mock_client):
123125
assert mock_client.call_count == 1
124126
assert mock_client.call_args[1] == {
125127
"username": None,
128+
"password": None,
126129
"hostname": "www.example.org",
127130
"service": "HTTP",
128131
"channel_bindings": None,
@@ -169,6 +172,7 @@ def test_authenticate_user(mock_client, mocker):
169172
assert mock_client.call_count == 1
170173
assert mock_client.call_args[1] == {
171174
"username": None,
175+
"password": None,
172176
"hostname": "www.example.org",
173177
"service": "HTTP",
174178
"channel_bindings": None,
@@ -217,6 +221,7 @@ def test_authenticate_user2(mock_client, mocker):
217221
assert mock_client.call_count == 1
218222
assert mock_client.call_args[1] == {
219223
"username": None,
224+
"password": None,
220225
"hostname": "www.example.org",
221226
"service": "HTTP",
222227
"channel_bindings": None,
@@ -262,6 +267,7 @@ def test_handle_401(mock_client, mocker):
262267
assert mock_client.call_count == 1
263268
assert mock_client.call_args[1] == {
264269
"username": None,
270+
"password": None,
265271
"hostname": "www.example.org",
266272
"service": "HTTP",
267273
"channel_bindings": None,
@@ -310,6 +316,7 @@ def test_handle_407(mock_client, mocker):
310316
assert mock_client.call_count == 1
311317
assert mock_client.call_args[1] == {
312318
"username": None,
319+
"password": None,
313320
"hostname": "www.example.org",
314321
"service": "HTTP",
315322
"channel_bindings": None,
@@ -553,6 +560,7 @@ def test_handle_response_401(mock_client, mocker):
553560
assert mock_client.call_count == 1
554561
assert mock_client.call_args[1] == {
555562
"username": None,
563+
"password": None,
556564
"hostname": "www.example.org",
557565
"service": "HTTP",
558566
"channel_bindings": None,
@@ -606,6 +614,7 @@ def connection_send(self, *args, **kwargs):
606614
assert mock_client.call_count == 1
607615
assert mock_client.call_args[1] == {
608616
"username": None,
617+
"password": None,
609618
"hostname": "www.example.org",
610619
"service": "HTTP",
611620
"channel_bindings": None,
@@ -630,6 +639,7 @@ def test_generate_request_header_custom_service(mock_client):
630639
assert mock_client.call_count == 1
631640
assert mock_client.call_args[1] == {
632641
"username": None,
642+
"password": None,
633643
"hostname": "www.example.org",
634644
"service": "barfoo",
635645
"channel_bindings": None,
@@ -669,6 +679,7 @@ def test_delegation(mock_client, mocker):
669679
assert mock_client.call_count == 1
670680
assert mock_client.call_args[1] == {
671681
"username": None,
682+
"password": None,
672683
"hostname": "www.example.org",
673684
"service": "HTTP",
674685
"channel_bindings": None,
@@ -693,6 +704,26 @@ def test_principal_override(mock_client):
693704
assert mock_client.call_count == 1
694705
assert mock_client.call_args[1] == {
695706
"username": "user@REALM",
707+
"password": None,
708+
"hostname": "www.example.org",
709+
"service": "HTTP",
710+
"channel_bindings": None,
711+
"context_req": spnego.ContextReq.sequence_detect | spnego.ContextReq.mutual_auth,
712+
"protocol": "kerberos",
713+
}
714+
715+
def test_principal_override_with_pass(mock_client):
716+
response = requests.Response()
717+
response.url = "http://www.example.org/"
718+
response.headers = {'www-authenticate': 'negotiate dG9rZW4='}
719+
host = urlparse(response.url).hostname
720+
auth = requests_kerberos.HTTPKerberosAuth(principal="user@REALM",password="password")
721+
auth.generate_request_header(response, host),
722+
723+
assert mock_client.call_count == 1
724+
assert mock_client.call_args[1] == {
725+
"username": "user@REALM",
726+
"password": "password",
696727
"hostname": "www.example.org",
697728
"service": "HTTP",
698729
"channel_bindings": None,
@@ -712,6 +743,7 @@ def test_realm_override(mock_client):
712743
assert mock_client.call_count == 1
713744
assert mock_client.call_args[1] == {
714745
"username": None,
746+
"password": None,
715747
"hostname": "otherhost.otherdomain.org",
716748
"service": "HTTP",
717749
"channel_bindings": None,

0 commit comments

Comments
 (0)