Skip to content

Commit 4db2e1a

Browse files
committed
Add bearer auth token for SSO
1 parent c311a2c commit 4db2e1a

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

neo4j/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"AuthToken",
3030
"basic_auth",
3131
"kerberos_auth",
32+
"bearer_auth",
3233
"custom_auth",
3334
"Bookmark",
3435
"ServerInfo",
@@ -69,6 +70,7 @@
6970
AuthToken,
7071
basic_auth,
7172
kerberos_auth,
73+
bearer_auth,
7274
custom_auth,
7375
Bookmark,
7476
ServerInfo,

neo4j/api.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class Auth:
8080

8181
def __init__(self, scheme, principal, credentials, realm=None, **parameters):
8282
self.scheme = scheme
83-
self.principal = principal
83+
if principal is not None:
84+
self.principal = principal
8485
self.credentials = credentials
8586
if realm:
8687
self.realm = realm
@@ -108,7 +109,7 @@ def basic_auth(user, password, realm=None):
108109

109110

110111
def kerberos_auth(base64_encoded_ticket):
111-
""" Generate a kerberos auth token with the base64 encoded ticket
112+
""" Generate a kerberos auth token with the base64 encoded ticket.
112113
113114
This will set the scheme to "kerberos" for the auth token.
114115
@@ -120,6 +121,20 @@ def kerberos_auth(base64_encoded_ticket):
120121
return Auth("kerberos", "", base64_encoded_ticket)
121122

122123

124+
def bearer_auth(base64_encoded_token):
125+
""" Generate an auth token for Single-Sign-On providers.
126+
127+
This will set the scheme to "bearer" for the auth token.
128+
129+
:param base64_encoded_token: a base64 encoded authentication token generated
130+
by a Single-Sign-On provider.
131+
132+
:return: auth token for use with :meth:`GraphDatabase.driver`
133+
:rtype: :class:`neo4j.Auth`
134+
"""
135+
return Auth("bearer", None, base64_encoded_token)
136+
137+
123138
def custom_auth(principal, credentials, realm, scheme, **parameters):
124139
""" Generate a custom auth token.
125140

testkitbackend/requests.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,23 @@ def NewDriver(backend, data):
5454
data["authorizationToken"].mark_item_as_read_if_equals(
5555
"name", "AuthorizationToken"
5656
)
57-
auth = neo4j.Auth(
57+
scheme = auth_token["scheme"]
58+
auth_token.mark_item_as_read("principal")
59+
auth_token.mark_item_as_read("realm")
60+
if scheme == "basic":
61+
auth = neo4j.basic_auth(
62+
auth_token["principal"], auth_token["credentials"],
63+
realm=auth_token["realm"]
64+
)
65+
elif scheme == "kerberos":
66+
auth = neo4j.kerberos_auth(auth_token["credentials"])
67+
elif scheme == "bearer":
68+
auth = neo4j.bearer_auth(auth_token["credentials"])
69+
else:
70+
auth = neo4j.Auth(
5871
auth_token["scheme"], auth_token["principal"],
59-
auth_token["credentials"], realm=auth_token["realm"])
72+
auth_token["credentials"], realm=auth_token["realm"]
73+
)
6074
auth_token.mark_item_as_read_if_equals("ticket", "")
6175
resolver = None
6276
if data["resolverRegistered"] or data["domainNameResolverRegistered"]:

0 commit comments

Comments
 (0)