@@ -570,7 +570,8 @@ def __init__(self, host, port,
570
570
ssl_ca_file = DEFAULT_SSL_CA_FILE ,
571
571
ssl_ciphers = DEFAULT_SSL_CIPHERS ,
572
572
packer_factory = default_packer_factory ,
573
- unpacker_factory = default_unpacker_factory ):
573
+ unpacker_factory = default_unpacker_factory ,
574
+ auth_type = DEFAULT_AUTH_TYPE ):
574
575
"""
575
576
:param host: Server hostname or IP address. Use ``None`` for
576
577
Unix sockets.
@@ -710,6 +711,14 @@ def __init__(self, host, port,
710
711
callable[[:obj:`~tarantool.Connection`], :obj:`~msgpack.Unpacker`],
711
712
optional
712
713
714
+ :param auth_type: Authentication method: ``"chap-sha1"`` (supported in
715
+ Tarantool CE and EE) or ``"pap-sha256"`` (supported in Tarantool EE,
716
+ ``"ssl"`` :paramref:`~tarantool.Connection.transport` must be used).
717
+ If `None`, use authentication method provided by server in IPROTO_ID
718
+ exchange. If server does not provide an authentication method, use
719
+ ``"chap-sha1"``.
720
+ :type auth_type: :obj:`None` or :obj:`str`, optional
721
+
713
722
:raise: :exc:`~tarantool.error.ConfigurationError`,
714
723
:meth:`~tarantool.Connection.connect` exceptions
715
724
@@ -763,6 +772,8 @@ def __init__(self, host, port,
763
772
}
764
773
self ._packer_factory_impl = packer_factory
765
774
self ._unpacker_factory_impl = unpacker_factory
775
+ self ._client_auth_type = auth_type
776
+ self ._server_auth_type = DEFAULT_AUTH_TYPE
766
777
767
778
if connect_now :
768
779
self .connect ()
@@ -1335,8 +1346,11 @@ def authenticate(self, user, password):
1335
1346
if not self ._socket :
1336
1347
return self ._opt_reconnect ()
1337
1348
1338
- request = RequestAuthenticate (self , self ._salt , self .user ,
1339
- self .password )
1349
+ request = RequestAuthenticate (self ,
1350
+ salt = self ._salt ,
1351
+ user = self .user ,
1352
+ password = self .password ,
1353
+ type = self ._get_auth_type ())
1340
1354
auth_response = self ._send_request_wo_reconnect (request )
1341
1355
if auth_response .return_code == 0 :
1342
1356
self .flush_schema ()
@@ -1982,11 +1996,13 @@ def _check_features(self):
1982
1996
response = self ._send_request (request )
1983
1997
server_protocol_version = response .protocol_version
1984
1998
server_features = response .features
1999
+ server_auth_type = response .auth_type
1985
2000
except DatabaseError as exc :
1986
2001
ER_UNKNOWN_REQUEST_TYPE = 48
1987
2002
if exc .code == ER_UNKNOWN_REQUEST_TYPE :
1988
2003
server_protocol_version = None
1989
2004
server_features = []
2005
+ server_auth_type = None
1990
2006
else :
1991
2007
raise exc
1992
2008
@@ -1999,6 +2015,8 @@ def _check_features(self):
1999
2015
for val in features_list :
2000
2016
self ._features [val ] = True
2001
2017
2018
+ self ._server_auth_type = server_auth_type
2019
+
2002
2020
def _packer_factory (self ):
2003
2021
return self ._packer_factory_impl (self )
2004
2022
@@ -2775,3 +2793,28 @@ def crud_unflatten_rows(self, rows: list, metadata: list) -> list:
2775
2793
res .append (row_res )
2776
2794
2777
2795
return res
2796
+
2797
+ def _get_auth_type (self ):
2798
+ """
2799
+ Get authentication method based on client and server settings.
2800
+
2801
+ :rtype: :obj:`str`
2802
+
2803
+ :raise: :exc:`~tarantool.error.DatabaseError`
2804
+ """
2805
+ if self ._client_auth_type == AUTH_TYPE_DEFAULT :
2806
+ if self ._server_auth_type == AUTH_TYPE_DEFAULT :
2807
+ auth_type = AUTH_TYPE_CHAP_SHA1
2808
+ else :
2809
+ if self ._server_auth_type not in AUTH_TYPES :
2810
+ raise ConfigurationError (f'Unknown server authentication type { self ._server_auth_type } ' )
2811
+ auth_type = self ._server_auth_type
2812
+ else :
2813
+ if self ._client_auth_type not in AUTH_TYPES :
2814
+ raise ConfigurationError (f'Unknown client authentication type { self ._server_auth_type } ' )
2815
+ auth_type = self ._client_auth_type
2816
+
2817
+ if auth_type == AUTH_TYPE_PAP_SHA256 and self .transport != SSL_TRANSPORT :
2818
+ raise ConfigurationError ('Use PAP-SHA256 only with ssl transport' )
2819
+
2820
+ return auth_type
0 commit comments