Skip to content

Commit 132cb45

Browse files
updated api documentation with better information about the auth token (#457)
* updated api documentation with better information about the auth token * better description of the auth token * fixed creation example * added with block context example for driver creation
1 parent 4032706 commit 132cb45

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

docs/source/api.rst

+46-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,38 @@ The :class:`neo4j.Driver` construction is via a `classmethod` on the :class:`neo
1717
:members: driver
1818

1919

20+
Example, driver creation:
21+
2022
.. code-block:: python
2123
22-
from neo4j import GraphDatabase
24+
from neo4j import GraphDatabase
25+
26+
uri = neo4j://example.com:7687
27+
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"), max_connection_lifetime=1000)
28+
29+
driver.close() # close the driver object
30+
31+
32+
For basic auth, this can be a simple tuple, for example:
2333

24-
driver = GraphDatabase.driver(uri=neo4j://example.com:7687, auth=("neo4j", "password"), max_connection_lifetime=1000)
34+
.. code-block:: python
35+
36+
auth = ("neo4j", "password")
37+
38+
This will implicitly create a :class:`neo4j.Auth` with a ``scheme="basic"``
39+
40+
41+
Example, with block context:
42+
43+
.. code-block:: python
44+
45+
from neo4j import GraphDatabase
46+
47+
uri = neo4j://example.com:7687
48+
49+
with GraphDatabase.driver(uri, auth=("neo4j", "password")) as driver:
50+
# use the driver
2551
26-
driver.close()
2752
2853
2954
.. _uri-ref:
@@ -78,18 +103,32 @@ Each supported scheme maps to a particular :class:`neo4j.Driver` subclass that i
78103
Auth
79104
====
80105

81-
An authentication token for the server.
106+
To authenticate with Neo4j the authentication details are supplied at driver creation.
82107

83-
For basic auth, this can be a simple tuple, for example:
108+
The auth token is an object of the class :class:`neo4j.Auth` containing the details.
109+
110+
.. autoclass:: neo4j.Auth
111+
112+
113+
114+
Example:
84115

85116
.. code-block:: python
86117
87-
auth = ("neo4j", "password")
118+
import neo4j
119+
120+
auth = neo4j.Auth(scheme="basic", principal="neo4j", credentials="password")
88121
89-
Alternatively, one of the auth token functions can be used.
122+
123+
Auth Token Helper Functions
124+
---------------------------
125+
126+
Alternatively, one of the auth token helper functions can be used.
90127

91128
.. autofunction:: neo4j.basic_auth
92129

130+
.. autofunction:: neo4j.kerberos_auth
131+
93132
.. autofunction:: neo4j.custom_auth
94133

95134

neo4j/api.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@
6060
# TODO: This class is not tested
6161
class Auth:
6262
""" Container for auth details.
63+
64+
:param scheme: specifies the type of authentication, examples: "basic", "kerberos"
65+
:type scheme: str
66+
:param principal: specifies who is being authenticated
67+
:type principal: str
68+
:param credentials: authenticates the principal
69+
:type credentials: str
70+
:param realm: specifies the authentication provider
71+
:type realm: str
72+
:param parameters: extra key word parameters passed along to the authentication provider
73+
:type parameters: str
6374
"""
6475

6576
#: By default we should not send any realm
@@ -82,32 +93,42 @@ def __init__(self, scheme, principal, credentials, realm=None, **parameters):
8293
def basic_auth(user, password, realm=None):
8394
""" Generate a basic auth token for a given user and password.
8495
85-
:param user: user name
86-
:param password: current password
96+
This will set the scheme to "basic" for the auth token.
97+
98+
:param user: user name, this will set the principal
99+
:param password: current password, this will set the credentials
87100
:param realm: specifies the authentication provider
101+
88102
:return: auth token for use with :meth:`GraphDatabase.driver`
103+
:rtype: :class:`neo4j.Auth`
89104
"""
90105
return Auth("basic", user, password, realm)
91106

92107

93108
def kerberos_auth(base64_encoded_ticket):
94109
""" Generate a kerberos auth token with the base64 encoded ticket
95110
96-
:param base64_encoded_ticket: a base64 encoded service ticket
97-
:return: an authentication token that can be used to connect to Neo4j
111+
This will set the scheme to "kerberos" for the auth token.
112+
113+
:param base64_encoded_ticket: a base64 encoded service ticket, this will set the credentials
114+
115+
:return: auth token for use with :meth:`GraphDatabase.driver`
116+
:rtype: :class:`neo4j.Auth`
98117
"""
99118
return Auth("kerberos", "", base64_encoded_ticket)
100119

101120

102121
def custom_auth(principal, credentials, realm, scheme, **parameters):
103-
""" Generate a basic auth token for a given user and password.
122+
""" Generate a custom auth token.
104123
105124
:param principal: specifies who is being authenticated
106125
:param credentials: authenticates the principal
107126
:param realm: specifies the authentication provider
108127
:param scheme: specifies the type of authentication
109-
:param parameters: parameters passed along to the authentication provider
128+
:param parameters: extra key word parameters passed along to the authentication provider
129+
110130
:return: auth token for use with :meth:`GraphDatabase.driver`
131+
:rtype: :class:`neo4j.Auth`
111132
"""
112133
return Auth(scheme, principal, credentials, realm, **parameters)
113134

0 commit comments

Comments
 (0)