Skip to content

Introduce auth rotation and session auth support #890

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

Merged
merged 27 commits into from
Apr 12, 2023

Conversation

robsdedude
Copy link
Member

@robsdedude robsdedude commented Jan 13, 2023

ADR 012: re-authentication

This PR introduces two new auth mechanics for different use-cases

  1. Auth Rotation
  2. Session Auth (a.k.a. user switching)

Note that all APIs introduced in this PR are previews
See https://github.com/neo4j/neo4j-python-driver/wiki/preview-features

1) Auth Rotation

This is used for auth tokens that is expected to expire (e.g., SSO).

A neo4j.auth_management.AuthManager instance
(or neo4j.auth_management.AsyncAuthManager for async driver) may be passed
to the driver instead of a static auth token.

import neo4j
from neo4j.auth_management import AuthManager


class MyManager(AuthManager):
    ...  # see API dos for details


with neo4j.GraphDatabase.driver(
    "neo4j://example.com:7687", 
    auth=MyManager(),
) as driver:
    ...

The easiest way to get started is using the provided AuthManager
implementation. For example:

import neo4j
from neo4j.auth_management import (
    AuthManagers,
    ExpiringAuth,
)


def auth_provider():
    # some way to getting a token
    sso_token = get_sso_token()
    # assume we know our tokens expire every 60 seconds
    expires_in = 60
    return ExpiringAuth(
        auth=neo4j.bearer_auth(sso_token),
        # Include a little buffer so that we fetch a new token
        # *before* the old one expires
        expires_in=expires_in - 10
    )


with neo4j.GraphDatabase.driver(
    "neo4j://example.com:7687",
    auth=AuthManagers.expiration_based(auth_provider)
) as driver:
    ... 

Note
This API is explicitly not designed for switching users.
In fact, the token returned by each manager must always belong to the same
identity. Switching identities using the AuthManager is undefined behavior.

2) Session Auth

For the purpose of switching users, Sessions can be configured with a static
auth token. This is very similar to impersonation in that all work in the
session will be executed in the security context of the user associated with
the auth token. The major difference is that impersonation does not require or
verify authentication information of the target user, however it requires
the impersonating user to have the permission to impersonate.

Note
This requires Bolt protocol version 5.3 or higher (Neo4j DBMS 5.8+).

import neo4j


with neo4j.GraphDatabase.driver(
    "neo4j://example.com:7687",
    auth=("user1", "password1"),
) as driver:
    with driver.session(database="neo4j") as session:
        ...  # do some work as user1
    with driver.session(database="neo4j",
                        auth=("user2", "password2")) as session:
        ...  # do some work as user2

References

Depends on:

Related PRs:

Issues:

Update according ADR change + add TestKit backend support and unit tests
 * move backwards compat option to driver level config
 * adjust `verify_authentication` and its backwards compat mode
 * more tests
 * Extended TestKit support
 * Fixed bugs found through TestKit
Implement change to ADR neo-technology/drivers-adr#60
The auth token provider function has been replaced by a more general-purpose
auth token manager interface.
Token expired code should be marked retryable iff the driver was configured
with a (non-static) auth token manager.

neo-technology/drivers-adr#64
AuthManagers.temporal -> AuthManagers.expiration_based
TemporalAuth-> ExpiringAuth
Copy link
Contributor

@AndyHeap-NeoTech AndyHeap-NeoTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good, just those experimental tags that needed swapping to preview.

@robsdedude robsdedude changed the title ADR 012: re-authentication Introduce auth rotation and session auth support Apr 12, 2023
@robsdedude robsdedude merged commit 6002a8c into neo4j:5.0 Apr 12, 2023
@robsdedude robsdedude deleted the reauth-poc branch April 12, 2023 16:52
robsdedude added a commit to robsdedude/neo4j-python-driver that referenced this pull request Aug 11, 2023
This PR updates the preview feature "re-auth" (introduced in
[PR neo4j#890](neo4j#890)) significantly.
The changes allow for catering to a wider range of use cases including simple
password rotation.

Changes (⚠️ marks breaking changes):

 * Removed `TokenExpiredRetryable` exception.
   Even though it wasn't marked preview, it was introduced with and only used
   for re-auth. It now longer serves any purpose.
 * The `AuthManager` and `AsyncAuthManager` abstract classes were changed.
   The method `on_auth_expired(self, auth: _TAuth) -> None` was removed in
   favor of `def handle_security_exception(self, auth: _TAuth, error: Neo4jError) -> bool`.
   See the API docs for more details.
 * The factories in `AsyncAuthManagers`a nd `AuthManagers` were changed.
   * `expiration_based` was renamed to `bearer`.
   * `basic` was added to cater for password rotation.
robsdedude added a commit to robsdedude/neo4j-python-driver that referenced this pull request Aug 11, 2023
This PR updates the preview feature "re-auth" (introduced in
[PR neo4j#890](neo4j#890)) significantly.
The changes allow for catering to a wider range of use cases including simple
password rotation.

(⚠️ breaking) changes:
 * Removed `TokenExpiredRetryable` exception.
   Even though it wasn't marked preview, it was introduced with and only used
   for re-auth. It now longer serves any purpose.
 * The `AuthManager` and `AsyncAuthManager` abstract classes were changed.
   The method `on_auth_expired(self, auth: _TAuth) -> None` was removed in
   favor of `def handle_security_exception(self, auth: _TAuth, error: Neo4jError) -> bool`.
   See the API docs for more details.
 * The factories in `AsyncAuthManagers`a nd `AuthManagers` were changed.
   * `expiration_based` was renamed to `bearer`.
   * `basic` was added to cater for password rotation.
robsdedude added a commit to robsdedude/neo4j-python-driver that referenced this pull request Aug 11, 2023
This PR updates the preview feature "re-auth" (introduced in
[PR neo4j#890](neo4j#890)) significantly.
The changes allow for catering to a wider range of use cases including simple
password rotation.

(⚠️ Breaking) changes:
 * Removed `TokenExpiredRetryable` exception.
   Even though it wasn't marked preview, it was introduced with and only used
   for re-auth. It now longer serves any purpose.
 * The `AuthManager` and `AsyncAuthManager` abstract classes were changed.
   The method `on_auth_expired(self, auth: _TAuth) -> None` was removed in
   favor of `def handle_security_exception(self, auth: _TAuth, error: Neo4jError) -> bool`.
   See the API docs for more details.
 * The factories in `AsyncAuthManagers`a nd `AuthManagers` were changed.
   * `expiration_based` was renamed to `bearer`.
   * `basic` was added to cater for password rotation.
robsdedude added a commit that referenced this pull request Aug 22, 2023
This PR updates the preview feature "re-auth" (introduced in
[PR #890](#890)) significantly.
The changes allow for catering to a wider range of use cases including simple
password rotation.

(⚠️ Breaking) changes:
 * Removed `TokenExpiredRetryable` exception.
   Even though it wasn't marked preview, it was introduced with and only used
   for re-auth. It now longer serves any purpose.
 * The `AuthManager` and `AsyncAuthManager` abstract classes were changed.
   The method `on_auth_expired(self, auth: _TAuth) -> None` was removed in
   favor of `def handle_security_exception(self, auth: _TAuth, error: Neo4jError) -> bool`.
   See the API docs for more details.
 * The factories in `AsyncAuthManagers`a nd `AuthManagers` were changed.
   * `expiration_based` was renamed to `bearer`.
   * `basic` was added to cater for password rotation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Ability to refresh auth token
3 participants