Skip to content

Commit e21fe99

Browse files
authored
Send final error byte x01 on Sasl OAuth failure (#2572)
1 parent d39dd3b commit e21fe99

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

kafka/sasl/oauth.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
from __future__ import absolute_import
22

33
import abc
4+
import logging
45

56
from kafka.sasl.abc import SaslMechanism
67

78

9+
log = logging.getLogger(__name__)
10+
11+
812
class SaslMechanismOAuth(SaslMechanism):
913

1014
def __init__(self, **config):
1115
assert 'sasl_oauth_token_provider' in config, 'sasl_oauth_token_provider required for OAUTHBEARER sasl'
1216
assert isinstance(config['sasl_oauth_token_provider'], AbstractTokenProvider), \
1317
'sasl_oauth_token_provider must implement kafka.sasl.oauth.AbstractTokenProvider'
1418
self.token_provider = config['sasl_oauth_token_provider']
19+
self._error = None
1520
self._is_done = False
1621
self._is_authenticated = False
1722

1823
def auth_bytes(self):
24+
if self._error:
25+
# Server should respond to this with SaslAuthenticate failure, which ends the auth process
26+
return self._error
1927
token = self.token_provider.token()
2028
extensions = self._token_extensions()
2129
return "n,,\x01auth=Bearer {}{}\x01\x01".format(token, extensions).encode('utf-8')
2230

2331
def receive(self, auth_bytes):
24-
self._is_done = True
25-
self._is_authenticated = auth_bytes == b''
32+
if auth_bytes != b'':
33+
error = auth_bytes.decode('utf-8')
34+
log.debug("Sending x01 response to server after receiving SASL OAuth error: %s", error)
35+
self._error = b'\x01'
36+
else:
37+
self._is_done = True
38+
self._is_authenticated = True
2639

2740
def is_done(self):
2841
return self._is_done

0 commit comments

Comments
 (0)