6161
6262# TODO: This class is not tested
6363class Auth :
64- """ Container for auth details.
64+ """Container for auth details.
6565
66- :param scheme: specifies the type of authentication, examples: "basic", "kerberos"
66+ :param scheme: specifies the type of authentication, examples: "basic",
67+ "kerberos"
6768 :type scheme: str
6869 :param principal: specifies who is being authenticated
69- :type principal: str
70+ :type principal: str or None
7071 :param credentials: authenticates the principal
71- :type credentials: str
72+ :type credentials: str or None
7273 :param realm: specifies the authentication provider
73- :type realm: str
74- :param parameters: extra key word parameters passed along to the authentication provider
75- :type parameters: str
74+ :type realm: str or None
75+ :param parameters: extra key word parameters passed along to the
76+ authentication provider
77+ :type parameters: Dict[str, Any]
7678 """
7779
78- #: By default we should not send any realm
79- realm = None
80-
8180 def __init__ (self , scheme , principal , credentials , realm = None , ** parameters ):
8281 self .scheme = scheme
83- self .principal = principal
84- self .credentials = credentials
82+ # Neo4j servers pre 4.4 require the principal field to always be
83+ # present. Therefore, we transmit it even if it's an empty sting.
84+ if principal is not None :
85+ self .principal = principal
86+ if credentials :
87+ self .credentials = credentials
8588 if realm :
8689 self .realm = realm
8790 if parameters :
@@ -93,13 +96,16 @@ def __init__(self, scheme, principal, credentials, realm=None, **parameters):
9396
9497
9598def basic_auth (user , password , realm = None ):
96- """ Generate a basic auth token for a given user and password.
99+ """Generate a basic auth token for a given user and password.
97100
98101 This will set the scheme to "basic" for the auth token.
99102
100- :param user: user name, this will set the principal
103+ :param user: user name, this will set the
104+ :type user: str
101105 :param password: current password, this will set the credentials
106+ :type password: str
102107 :param realm: specifies the authentication provider
108+ :type realm: str or None
103109
104110 :return: auth token for use with :meth:`GraphDatabase.driver`
105111 :rtype: :class:`neo4j.Auth`
@@ -108,26 +114,49 @@ def basic_auth(user, password, realm=None):
108114
109115
110116def kerberos_auth (base64_encoded_ticket ):
111- """ Generate a kerberos auth token with the base64 encoded ticket
117+ """Generate a kerberos auth token with the base64 encoded ticket.
112118
113119 This will set the scheme to "kerberos" for the auth token.
114120
115- :param base64_encoded_ticket: a base64 encoded service ticket, this will set the credentials
121+ :param base64_encoded_ticket: a base64 encoded service ticket, this will set
122+ the credentials
123+ :type base64_encoded_ticket: str
116124
117125 :return: auth token for use with :meth:`GraphDatabase.driver`
118126 :rtype: :class:`neo4j.Auth`
119127 """
120128 return Auth ("kerberos" , "" , base64_encoded_ticket )
121129
122130
131+ def bearer_auth (base64_encoded_token ):
132+ """Generate an auth token for Single-Sign-On providers.
133+
134+ This will set the scheme to "bearer" for the auth token.
135+
136+ :param base64_encoded_token: a base64 encoded authentication token generated
137+ by a Single-Sign-On provider.
138+ :type base64_encoded_token: str
139+
140+ :return: auth token for use with :meth:`GraphDatabase.driver`
141+ :rtype: :class:`neo4j.Auth`
142+ """
143+ return Auth ("bearer" , None , base64_encoded_token )
144+
145+
123146def custom_auth (principal , credentials , realm , scheme , ** parameters ):
124- """ Generate a custom auth token.
147+ """Generate a custom auth token.
125148
126149 :param principal: specifies who is being authenticated
150+ :type principal: str or None
127151 :param credentials: authenticates the principal
152+ :type credentials: str or None
128153 :param realm: specifies the authentication provider
154+ :type realm: str or None
129155 :param scheme: specifies the type of authentication
130- :param parameters: extra key word parameters passed along to the authentication provider
156+ :type scheme: str or None
157+ :param parameters: extra key word parameters passed along to the
158+ authentication provider
159+ :type parameters: Dict[str, Any]
131160
132161 :return: auth token for use with :meth:`GraphDatabase.driver`
133162 :rtype: :class:`neo4j.Auth`
0 commit comments