Skip to content

Commit

Permalink
Add session auth to executeQuery (#774)
Browse files Browse the repository at this point in the history
* Add session auth to executeQuery

* rename `auth` to `authorizationToken`
  • Loading branch information
RichardIrons-neo4j authored Feb 2, 2024
1 parent eb868dc commit b144ce4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ private QueryConfig BuildConfig()
Metadata = data.config.txMeta != null ? CypherToNativeObject.ConvertDictionaryToNative(data.config.txMeta) : new Dictionary<string, object>()
};

var authToken = data.config.authorizationToken switch
{
null => null,
not null => data.config.authorizationToken.AsToken()
};

return new QueryConfig(
routingControl,
data.config.database,
data.config.impersonatedUser,
bookmarkManager,
enableBookmarkManager,
transactionConfig);
transactionConfig,
authToken);
}

public override string Respond()
Expand Down Expand Up @@ -132,5 +139,6 @@ public class ExecuteQueryConfigDto
public int? timeout { get; set; }
[JsonConverter(typeof(QueryParameterConverter))]
public Dictionary<string, CypherToNativeObject> txMeta { get; set; }
public AuthorizationToken authorizationToken { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static SupportedFeatures()
"Feature:API:BookmarkManager",
"Feature:API:ConnectionAcquisitionTimeout",
"Feature:API:Driver.ExecuteQuery",
"Feature:API:Driver.ExecuteQuery:WithAuth",
"Feature:API:Driver:GetServerInfo",
"Feature:API:Driver.IsEncrypted",
"Feature:API:Driver:NotificationsConfig",
Expand Down
5 changes: 5 additions & 0 deletions Neo4j.Driver/Neo4j.Driver/Internal/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ private void ApplyConfig(QueryConfig config, SessionConfigBuilder sessionConfigB
sessionConfigBuilder.WithBookmarkManager(config.BookmarkManager ?? _bookmarkManager);
}

if(config.AuthToken != null)
{
sessionConfigBuilder.WithAuthToken(config.AuthToken);
}

sessionConfigBuilder.WithDefaultAccessMode(
config.Routing switch
{
Expand Down
25 changes: 22 additions & 3 deletions Neo4j.Driver/Neo4j.Driver/Public/ExecuteQuery/QueryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Neo4j.Driver.Internal.Auth;

namespace Neo4j.Driver;

Expand All @@ -36,21 +37,25 @@ public class QueryConfig
/// remove any usage or modification of <see cref="IBookmarkManager"/>.
/// </param>
/// <param name="transactionConfig">Transaction configuration.</param>
/// <param name="authToken">Auth token to use for the session executing the query.</param>
public QueryConfig(
RoutingControl routing = RoutingControl.Writers,
string database = null,
string impersonatedUser = null,
IBookmarkManager bookmarkManager = null,
bool enableBookmarkManager = true,
TransactionConfig transactionConfig = null)
TransactionConfig transactionConfig = null,
IAuthToken authToken = null)
{
Routing = routing;
Database = database;
ImpersonatedUser = impersonatedUser;
BookmarkManager = bookmarkManager;
EnableBookmarkManager = enableBookmarkManager;
TransactionConfig = transactionConfig ?? TransactionConfig.Default;
AuthToken = authToken;
}

/// <summary>Config for the transaction that will be used to execute the query.</summary>
public TransactionConfig TransactionConfig { get; }

Expand All @@ -71,6 +76,11 @@ public QueryConfig(

/// <summary>Enables or disables the use of an <see cref="IBookmarkManager"/> for causal chaining.</summary>
public bool EnableBookmarkManager { get; }

/// <summary>
/// Auth token to use for the session executing the query.
/// </summary>
public IAuthToken AuthToken { get; }
}

/// <summary>Configuration for running queries using the simplified API.</summary>
Expand All @@ -93,6 +103,7 @@ public class QueryConfig<T> : QueryConfig
/// remove any usage or modification of <see cref="IBookmarkManager"/>.
/// </param>
/// <param name="transactionConfig">Transaction configuration.</param>
/// <param name="authToken">Auth token to use for the session executing the query.</param>
/// <exception cref="ArgumentNullException"></exception>
public QueryConfig(
Func<IResultCursor, CancellationToken, Task<T>> cursorProcessor,
Expand All @@ -101,8 +112,16 @@ public QueryConfig(
string impersonatedUser = null,
IBookmarkManager bookmarkManager = null,
bool enableBookmarkManager = true,
TransactionConfig transactionConfig = null)
: base(routing, database, impersonatedUser, bookmarkManager, enableBookmarkManager, transactionConfig)
TransactionConfig transactionConfig = null,
IAuthToken authToken = null)
: base(
routing,
database,
impersonatedUser,
bookmarkManager,
enableBookmarkManager,
transactionConfig,
authToken)
{
CursorProcessor = cursorProcessor ?? throw new ArgumentNullException(nameof(cursorProcessor));
}
Expand Down

0 comments on commit b144ce4

Please sign in to comment.