Skip to content

Add the ability to Refresh Auth Token Credentials #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ neo4j-enterprise-*

testkit/CAs
testkit/CustomCAs
/.vs
3 changes: 2 additions & 1 deletion src/neo4j/_async/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
AsyncInbox,
AsyncOutbox,
CommitResponse,
auth_to_dict,
)


Expand Down Expand Up @@ -142,7 +143,7 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *,
self.auth_dict = vars(Auth("basic", *auth))
else:
try:
self.auth_dict = vars(auth)
self.auth_dict = auth_to_dict(auth)
except (KeyError, TypeError):
raise AuthError("Cannot determine auth details from %r" % auth)

Expand Down
9 changes: 9 additions & 0 deletions src/neo4j/_async/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,12 @@ async def receive_into_buffer(sock, buffer, n_bytes):
if n == 0:
raise OSError("No data")
buffer.used += n


def auth_to_dict(auth):
auth_dict = vars(auth).copy()
if "credentials_refresher" in auth_dict:
if auth_dict["credentials_refresher"] is not None:
auth_dict["credentials"] = auth_dict["credentials_refresher"]()
auth_dict.pop("credentials_refresher")
return auth_dict
3 changes: 2 additions & 1 deletion src/neo4j/_sync/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from logging import getLogger
from time import perf_counter

from ..._async.io._common import auth_to_dict
from ..._async_compat.network import BoltSocket
from ..._codec.hydration import v1 as hydration_v1
from ..._codec.packstream import v1 as packstream_v1
Expand Down Expand Up @@ -142,7 +143,7 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *,
self.auth_dict = vars(Auth("basic", *auth))
else:
try:
self.auth_dict = vars(auth)
self.auth_dict = auth_to_dict(auth)
except (KeyError, TypeError):
raise AuthError("Cannot determine auth details from %r" % auth)

Expand Down
4 changes: 4 additions & 0 deletions src/neo4j/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def __init__(
self.realm = realm
if parameters:
self.parameters = parameters
self.credentials_refresher = None

def add_credentials_refresher(self, credentials_refresher: t.Any):
self.credentials_refresher = credentials_refresher


# For backwards compatibility
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/async_/io/test__common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import pytest

from neo4j._async.io._common import AsyncOutbox
from neo4j import basic_auth
from neo4j._async.io._common import AsyncOutbox, auth_to_dict
from neo4j._codec.packstream.v1 import PackableBuffer

from ...._async_compat import mark_async_test
Expand Down Expand Up @@ -58,3 +59,27 @@ async def test_async_outbox_chunking(chunk_size, data, result, mocker):

assert not await outbox.flush()
socket_mock.sendall.assert_awaited_once()


def test_auth_to_dict_without_refresher():
auth = basic_auth("some_login", "some_password")
result = auth_to_dict(auth)
assert result == {"principal": "some_login",
"credentials": "some_password",
"scheme": "basic"}


def test_auth_to_dict_with_refresher():
creds = "old credentials"
auth = basic_auth("some_login", "ignored")
auth.add_credentials_refresher(lambda: creds)
result = auth_to_dict(auth)
assert result == {"principal": "some_login",
"credentials": "old credentials",
"scheme": "basic"}
# run again and should get new credentials
creds = "new credentials"
result = auth_to_dict(auth)
assert result == {"principal": "some_login",
"credentials": "new credentials",
"scheme": "basic"}