Skip to content

Commit

Permalink
Support for auth in driver
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Feb 25, 2016
1 parent 0a704bf commit 95ff470
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 55 deletions.
3 changes: 2 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
# limitations under the License.

from neo4j.v1.session import GraphDatabase
from neo4j.v1.auth import basic_auth

driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()

session.run("MERGE (a:Person {name:'Alice'})")
Expand Down
26 changes: 13 additions & 13 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
from test.util import ServerTestCase

# tag::minimal-example-import[]
from neo4j.v1 import GraphDatabase
from neo4j.v1 import GraphDatabase, basic_auth
# end::minimal-example-import[]


class FreshDatabaseTestCase(ServerTestCase):

def setUp(self):
ServerTestCase.setUp(self)
session = GraphDatabase.driver("bolt://localhost").session()
session = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j")).session()
session.run("MATCH (n) DETACH DELETE n")
session.close()

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

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

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

def test_construct_driver(self):
# tag::construct-driver[]
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
# end::construct-driver[]
return driver

Expand Down Expand Up @@ -85,23 +85,23 @@ def test_tls_signed(self):
# end::tls-signed[]

def test_statement(self):
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
# tag::statement[]
session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close()
# end::statement[]
session.close()

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

def test_result_cursor(self):
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
# tag::result-cursor[]
search_term = "hammer"
Expand All @@ -114,7 +114,7 @@ def test_result_cursor(self):
session.close()

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

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

def test_transaction_commit(self):
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
# tag::transaction-commit[]
tx = session.begin_transaction()
Expand All @@ -156,7 +156,7 @@ def test_transaction_commit(self):
session.close()

def test_transaction_rollback(self):
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
# tag::transaction-rollback[]
tx = session.begin_transaction()
Expand All @@ -170,7 +170,7 @@ def test_transaction_rollback(self):
session.close()

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

def test_result_summary_notifications(self):
driver = GraphDatabase.driver("bolt://localhost")
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
# tag::result-summary-notifications[]
result = session.run("EXPLAIN MATCH (a), (b) RETURN a,b")
Expand Down
1 change: 1 addition & 0 deletions neo4j/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
from .constants import *
from .session import *
from .typesystem import *
from .auth import *
27 changes: 27 additions & 0 deletions neo4j/v1/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2016 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from collections import namedtuple

AuthToken = namedtuple("AuthToken", ("scheme", "principal", "credentials"))


def basic_auth(user, password):
return AuthToken("basic", user, password)
9 changes: 8 additions & 1 deletion neo4j/v1/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,24 @@ def __init__(self, sock, **config):
def on_failure(metadata):
raise ProtocolError("Initialisation failed")

self.auth_token = config.get("auth")
response = Response(self)
response.on_failure = on_failure

self.append(INIT, (self.user_agent,), response=response)
self.append(INIT, (self.user_agent,self._auth_token_dict(),), response=response)
self.send()
while not response.complete:
self.fetch()

def __del__(self):
self.close()

def _auth_token_dict(self):
if self.auth_token:
return self.auth_token._asdict()
else:
return {}

def append(self, signature, fields=(), response=None):
""" Add a message to the outgoing queue.
Expand Down
2 changes: 2 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ fi
echo "Running tests with $(python --version)"
pip install --upgrade -r ${DRIVER_HOME}/test_requirements.txt
echo ""
python -c 'from test.util import *; change_password("neo4j", "neo4j", "tmp")'
python -c 'from test.util import *; change_password("neo4j", "tmp", "neo4j")'
TEST_RUNNER="coverage run -m ${UNITTEST} discover -vfs ${TEST}"
EXAMPLES_RUNNER="coverage run -m ${UNITTEST} discover -vfs examples"
BEHAVE_RUNNER="behave --tags=-db --tags=-in_dev test/tck"
Expand Down
5 changes: 2 additions & 3 deletions test/tck/tck_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE
from neo4j.v1 import GraphDatabase, Relationship, Node, Path, SECURITY_NONE, basic_auth
from neo4j.v1.compat import string


driver = GraphDatabase.driver("bolt://localhost", security=SECURITY_NONE)

driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"), security=SECURITY_NONE)

def send_string(text):
session = driver.session()
Expand Down
Loading

0 comments on commit 95ff470

Please sign in to comment.