Skip to content

Commit f75d9c0

Browse files
authored
Merge pull request #103 from neo4j/1.1-custom-auth
Custom auth (replaces Pontus' original work in #86)
2 parents bd8f611 + a04024c commit f75d9c0

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

neo4j/v1/session.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ class AuthToken(object):
5757
""" Container for auth information
5858
"""
5959

60-
def __init__(self, scheme, principal, credentials):
60+
#: By default we should not send any realm
61+
realm = None
62+
63+
def __init__(self, scheme, principal, credentials, realm=None, **parameters):
6164
self.scheme = scheme
6265
self.principal = principal
6366
self.credentials = credentials
67+
if realm:
68+
self.realm = realm
69+
if parameters:
70+
self.parameters = parameters
6471

6572

6673
class GraphDatabase(object):
@@ -665,14 +672,28 @@ def __ne__(self, other):
665672
return not self.__eq__(other)
666673

667674

668-
def basic_auth(user, password):
675+
def basic_auth(user, password, realm=None):
669676
""" Generate a basic auth token for a given user and password.
670677
671678
:param user: user name
672679
:param password: current password
680+
:param realm: specifies the authentication provider
681+
:return: auth token for use with :meth:`GraphDatabase.driver`
682+
"""
683+
return AuthToken("basic", user, password, realm)
684+
685+
686+
def custom_auth(principal, credentials, realm, scheme, **parameters):
687+
""" Generate a basic auth token for a given user and password.
688+
689+
:param principal: specifies who is being authenticated
690+
:param credentials: authenticates the principal
691+
:param realm: specifies the authentication provider
692+
:param scheme: specifies the type of authentication
693+
:param parameters: parameters passed along to the authenticatin provider
673694
:return: auth token for use with :meth:`GraphDatabase.driver`
674695
"""
675-
return AuthToken("basic", user, password)
696+
return AuthToken(scheme, principal, credentials, realm, **parameters)
676697

677698

678699
def parse_address(address):

test/test_driver.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from neo4j.v1 import ServiceUnavailable, ProtocolError, READ_ACCESS, WRITE_ACCESS, \
2727
TRUST_ON_FIRST_USE, TRUST_CUSTOM_CA_SIGNED_CERTIFICATES, GraphDatabase, basic_auth, \
28-
SSL_AVAILABLE, SessionExpired, DirectDriver
28+
custom_auth, SSL_AVAILABLE, SessionExpired, DirectDriver
2929
from test.util import ServerTestCase
3030

3131
BOLT_URI = "bolt://localhost:7687"
@@ -68,6 +68,30 @@ def test_fail_nicely_when_connecting_to_http_port(self):
6868
with self.assertRaises(ServiceUnavailable) as context:
6969
driver.session()
7070

71+
def test_can_provide_realm_with_basic_auth_token(self):
72+
token = basic_auth("neotest", "neotest", "native")
73+
driver = GraphDatabase.driver("bolt://localhost", auth=token)
74+
session = driver.session()
75+
result = session.run("RETURN 1").consume()
76+
session.close()
77+
assert result is not None
78+
79+
def test_can_create_custom_auth_token(self):
80+
token = custom_auth("neotest", "neotest", "native", "basic")
81+
driver = GraphDatabase.driver("bolt://localhost", auth=token)
82+
session = driver.session()
83+
result = session.run("RETURN 1").consume()
84+
session.close()
85+
assert result is not None
86+
87+
def test_can_create_custom_auth_token_with_additional_parameters(self):
88+
token = custom_auth("neotest", "neotest", "native", "basic", secret=42)
89+
driver = GraphDatabase.driver("bolt://localhost", auth=token)
90+
session = driver.session()
91+
result = session.run("RETURN 1").consume()
92+
session.close()
93+
assert result is not None
94+
7195

7296
class DirectDriverTestCase(ServerTestCase):
7397

0 commit comments

Comments
 (0)