Skip to content

Commit 95ff470

Browse files
committed
Support for auth in driver
1 parent 0a704bf commit 95ff470

File tree

9 files changed

+120
-55
lines changed

9 files changed

+120
-55
lines changed

example.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
# limitations under the License.
2020

2121
from neo4j.v1.session import GraphDatabase
22+
from neo4j.v1.auth import basic_auth
2223

23-
driver = GraphDatabase.driver("bolt://localhost")
24+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
2425
session = driver.session()
2526

2627
session.run("MERGE (a:Person {name:'Alice'})")

examples/test_examples.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
from test.util import ServerTestCase
2323

2424
# tag::minimal-example-import[]
25-
from neo4j.v1 import GraphDatabase
25+
from neo4j.v1 import GraphDatabase, basic_auth
2626
# end::minimal-example-import[]
2727

2828

2929
class FreshDatabaseTestCase(ServerTestCase):
3030

3131
def setUp(self):
3232
ServerTestCase.setUp(self)
33-
session = GraphDatabase.driver("bolt://localhost").session()
33+
session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session()
3434
session.run("MATCH (n) DETACH DELETE n")
3535
session.close()
3636

@@ -39,7 +39,7 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase):
3939

4040
def test_minimal_working_example(self):
4141
# tag::minimal-example[]
42-
driver = GraphDatabase.driver("bolt://localhost")
42+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
4343
session = driver.session()
4444

4545
session.run("CREATE (neo:Person {name:'Neo', age:23})")
@@ -56,7 +56,7 @@ class ExamplesTestCase(FreshDatabaseTestCase):
5656

5757
def test_construct_driver(self):
5858
# tag::construct-driver[]
59-
driver = GraphDatabase.driver("bolt://localhost")
59+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
6060
# end::construct-driver[]
6161
return driver
6262

@@ -85,23 +85,23 @@ def test_tls_signed(self):
8585
# end::tls-signed[]
8686

8787
def test_statement(self):
88-
driver = GraphDatabase.driver("bolt://localhost")
88+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
8989
session = driver.session()
9090
# tag::statement[]
9191
session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close()
9292
# end::statement[]
9393
session.close()
9494

9595
def test_statement_without_parameters(self):
96-
driver = GraphDatabase.driver("bolt://localhost")
96+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
9797
session = driver.session()
9898
# tag::statement-without-parameters[]
9999
session.run("CREATE (person:Person {name: 'Neo'})").close()
100100
# end::statement-without-parameters[]
101101
session.close()
102102

103103
def test_result_cursor(self):
104-
driver = GraphDatabase.driver("bolt://localhost")
104+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
105105
session = driver.session()
106106
# tag::result-cursor[]
107107
search_term = "hammer"
@@ -114,7 +114,7 @@ def test_result_cursor(self):
114114
session.close()
115115

116116
def test_cursor_nesting(self):
117-
driver = GraphDatabase.driver("bolt://localhost")
117+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
118118
session = driver.session()
119119
# tag::retain-result-query[]
120120
result = session.run("MATCH (person:Person) WHERE person.dept = {dept} "
@@ -127,7 +127,7 @@ def test_cursor_nesting(self):
127127
session.close()
128128

129129
def test_result_retention(self):
130-
driver = GraphDatabase.driver("bolt://localhost")
130+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
131131
session = driver.session()
132132
# tag::retain-result-process[]
133133
result = session.run("MATCH (person:Person) WHERE person.dept = {dept} "
@@ -142,7 +142,7 @@ def test_result_retention(self):
142142
session.close()
143143

144144
def test_transaction_commit(self):
145-
driver = GraphDatabase.driver("bolt://localhost")
145+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
146146
session = driver.session()
147147
# tag::transaction-commit[]
148148
tx = session.begin_transaction()
@@ -156,7 +156,7 @@ def test_transaction_commit(self):
156156
session.close()
157157

158158
def test_transaction_rollback(self):
159-
driver = GraphDatabase.driver("bolt://localhost")
159+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
160160
session = driver.session()
161161
# tag::transaction-rollback[]
162162
tx = session.begin_transaction()
@@ -170,7 +170,7 @@ def test_transaction_rollback(self):
170170
session.close()
171171

172172
def test_result_summary_query_profile(self):
173-
driver = GraphDatabase.driver("bolt://localhost")
173+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
174174
session = driver.session()
175175
# tag::result-summary-query-profile[]
176176
result = session.run("PROFILE MATCH (p:Person {name: {name}}) "
@@ -183,7 +183,7 @@ def test_result_summary_query_profile(self):
183183
session.close()
184184

185185
def test_result_summary_notifications(self):
186-
driver = GraphDatabase.driver("bolt://localhost")
186+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
187187
session = driver.session()
188188
# tag::result-summary-notifications[]
189189
result = session.run("EXPLAIN MATCH (a), (b) RETURN a,b")

neo4j/v1/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
from .constants import *
2222
from .session import *
2323
from .typesystem import *
24+
from .auth import *

neo4j/v1/auth.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2016 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
from collections import namedtuple
22+
23+
AuthToken = namedtuple("AuthToken", ("scheme", "principal", "credentials"))
24+
25+
26+
def basic_auth(user, password):
27+
return AuthToken("basic", user, password)

neo4j/v1/connection.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,24 @@ def __init__(self, sock, **config):
218218
def on_failure(metadata):
219219
raise ProtocolError("Initialisation failed")
220220

221+
self.auth_token = config.get("auth")
221222
response = Response(self)
222223
response.on_failure = on_failure
223224

224-
self.append(INIT, (self.user_agent,), response=response)
225+
self.append(INIT, (self.user_agent,self._auth_token_dict(),), response=response)
225226
self.send()
226227
while not response.complete:
227228
self.fetch()
228229

229230
def __del__(self):
230231
self.close()
231232

233+
def _auth_token_dict(self):
234+
if self.auth_token:
235+
return self.auth_token._asdict()
236+
else:
237+
return {}
238+
232239
def append(self, signature, fields=(), response=None):
233240
""" Add a message to the outgoing queue.
234241

runtests.sh

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ fi
6161
echo "Running tests with $(python --version)"
6262
pip install --upgrade -r ${DRIVER_HOME}/test_requirements.txt
6363
echo ""
64+
python -c 'from test.util import *; change_password("neo4j", "neo4j", "tmp")'
65+
python -c 'from test.util import *; change_password("neo4j", "tmp", "neo4j")'
6466
TEST_RUNNER="coverage run -m ${UNITTEST} discover -vfs ${TEST}"
6567
EXAMPLES_RUNNER="coverage run -m ${UNITTEST} discover -vfs examples"
6668
BEHAVE_RUNNER="behave --tags=-db --tags=-in_dev test/tck"

test/tck/tck_util.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE
21+
from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE, basic_auth
2222
from neo4j.v1.compat import string
2323

2424

25-
driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_NONE)
26-
25+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_NONE)
2726

2827
def send_string(text):
2928
session = driver.session()

0 commit comments

Comments
 (0)