Skip to content

Commit fb909a5

Browse files
committed
Added support for custom auth tokens
1 parent 1614f78 commit fb909a5

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

neo4j/v1/session.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
instances that are in turn used for managing sessions.
2525
"""
2626

27-
2827
from __future__ import division
2928

3029
from collections import deque
@@ -39,7 +38,6 @@
3938
from .summary import ResultSummary
4039
from .types import hydrated
4140

42-
4341
DEFAULT_MAX_POOL_SIZE = 50
4442

4543
localhost = re.compile(r"^(localhost|127(\.\d+){3})$", re.IGNORECASE)
@@ -49,10 +47,14 @@ class AuthToken(object):
4947
""" Container for auth information
5048
"""
5149

52-
def __init__(self, scheme, principal, credentials):
50+
def __init__(self, scheme, principal, credentials, realm=None, parameters=None):
5351
self.scheme = scheme
5452
self.principal = principal
5553
self.credentials = credentials
54+
if realm:
55+
self.realm = realm
56+
if parameters:
57+
self.parameters = parameters
5658

5759

5860
class GraphDatabase(object):
@@ -135,7 +137,7 @@ def __init__(self, address, **config):
135137
self.encrypted = encrypted
136138
self.trust = trust = config.get("trust", TRUST_DEFAULT)
137139
if encrypted == ENCRYPTION_ON or \
138-
encrypted == ENCRYPTION_NON_LOCAL and not localhost.match(host):
140+
encrypted == ENCRYPTION_NON_LOCAL and not localhost.match(host):
139141
if not SSL_AVAILABLE:
140142
raise RuntimeError("Bolt over TLS is only available in Python 2.7.9+ and Python 3.3+")
141143
ssl_context = SSLContext(PROTOCOL_SSLv23)
@@ -510,14 +512,28 @@ def __ne__(self, other):
510512
return not self.__eq__(other)
511513

512514

513-
def basic_auth(user, password):
515+
def basic_auth(user, password, realm=None):
514516
""" Generate a basic auth token for a given user and password.
515517
516518
:param user: user name
517519
:param password: current password
520+
:param realm: specifies the authentication provider
521+
:return: auth token for use with :meth:`GraphDatabase.driver`
522+
"""
523+
return AuthToken("basic", user, password, realm)
524+
525+
526+
def custom_auth(principal, credentials, realm=None, scheme=None, parameters=None):
527+
""" Generate a basic auth token for a given user and password.
528+
529+
:param principal: specifies who is being authenticated
530+
:param credentials: authenticates the principal
531+
:param realm: specifies the authentication provider
532+
:param scheme: specifies the type of authentication
533+
:param parameters: parameters passed along to the authenticatin provider
518534
:return: auth token for use with :meth:`GraphDatabase.driver`
519535
"""
520-
return AuthToken("basic", user, password)
536+
return AuthToken(scheme, principal, credentials, realm, parameters)
521537

522538

523539
def run(connection, statement, parameters=None):

test/test_session.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from neo4j.v1.constants import TRUST_ON_FIRST_USE
2929
from neo4j.v1.exceptions import CypherError, ProtocolError, ResultError
30-
from neo4j.v1.session import GraphDatabase, basic_auth, Record, SSL_AVAILABLE
30+
from neo4j.v1.session import GraphDatabase, basic_auth, custom_auth, Record, SSL_AVAILABLE
3131
from neo4j.v1.types import Node, Relationship, Path
3232

3333
from test.util import ServerTestCase
@@ -97,6 +97,21 @@ def test_fail_nicely_when_connecting_to_http_port(self):
9797
assert str(context.exception) == "Server responded HTTP. Make sure you are not trying to connect to the http " \
9898
"endpoint (HTTP defaults to port 7474 whereas BOLT defaults to port 7687)"
9999

100+
def test_can_create_custom_auth_token(self):
101+
token = custom_auth("neo4j", "neo4j", "native", "basic")
102+
driver = GraphDatabase.driver("bolt://localhost", auth=token)
103+
session = driver.session()
104+
result = session.run("RETURN 1").consume()
105+
session.close()
106+
assert result is not None
107+
108+
def test_can_create_custom_auth_token_with_additional_parameters(self):
109+
token = custom_auth("neo4j", "neo4j", "native", "basic", {secret: 42})
110+
driver = GraphDatabase.driver("bolt://localhost", auth=token)
111+
session = driver.session()
112+
result = session.run("RETURN 1").consume()
113+
session.close()
114+
assert result is not None
100115

101116

102117
class SecurityTestCase(ServerTestCase):

0 commit comments

Comments
 (0)