Skip to content

Commit

Permalink
Re auth (#689)
Browse files Browse the repository at this point in the history
* Implement Reauth
---------

Co-authored-by: grant lodge <[email protected]>
  • Loading branch information
RichardIrons-neo4j and thelonelyvulpes authored May 30, 2023
1 parent 9519cf0 commit 7a1d600
Show file tree
Hide file tree
Showing 137 changed files with 2,954 additions and 839 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,6 @@ testkit/CAs/*
testkit/CustomCAs/customRoot.crt

testkit/CustomCAs/customRoot2.crt

# generated docs
Docs/
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Internals;
using Neo4j.Driver.Internal;
using Neo4j.Driver.Internal.Auth;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -67,7 +69,7 @@ public async Task ShouldCreateCustomAuthToken()
oldAuthToken["principal"].As<string>(),
oldAuthToken["credentials"].As<string>(),
"native",
"basic");
AuthSchemes.Basic);

await VerifyConnectivity(ServerEndPoint, newAuthToken);
}
Expand All @@ -80,7 +82,7 @@ public async Task ShouldCreateCustomAuthTokenWithAdditionalParameters()
oldAuthToken["principal"].As<string>(),
oldAuthToken["credentials"].As<string>(),
"native",
"basic",
AuthSchemes.Basic,
new Dictionary<string, object> { { "secret", 42 } });

await VerifyConnectivity(ServerEndPoint, newAuthToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Internals;
using Neo4j.Driver.Internal;
using Neo4j.Driver.Internal.Auth;
using Neo4j.Driver.Internal.Connector;
using Neo4j.Driver.Internal.Connector.Trust;
using Org.BouncyCastle.Pkcs;
Expand Down Expand Up @@ -167,15 +169,15 @@ private IDriver SetupWithCustomResolver(Uri overridenUri, Config config)
{
var connectionSettings = new ConnectionSettings(
overridenUri,
Server.AuthToken,
AuthTokenManagers.Static(Server.AuthToken),
config,
new CustomHostResolver(
Server.BoltUri,
new SystemNetCoreHostResolver(new SystemHostResolver())));

var bufferSettings = new BufferSettings(config);
var connectionFactory =
new PooledConnectionFactory(connectionSettings, bufferSettings, config.Logger);
new PooledConnectionFactory(bufferSettings, config.Logger);

return GraphDatabase.CreateDriver(overridenUri, config, connectionFactory, connectionSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Internals;
using Xunit;
using Xunit.Abstractions;
Expand Down
3 changes: 2 additions & 1 deletion Neo4j.Driver/Neo4j.Driver.Tests.Integration/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using FluentAssertions;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Internals;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -317,7 +318,7 @@ public IDriver CreateDriverWithCustomizedAuth(
public void TestCustomAuthExample()
{
// Given
using var driver = CreateDriverWithCustomizedAuth(Uri, User, Password, "native", "basic", null);
using var driver = CreateDriverWithCustomizedAuth(Uri, User, Password, "native", AuthSchemes.Basic, null);
using var session = driver.Session();
// When & Then
session.Run("RETURN 1").Single()[0].As<int>().Should().Be(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public IDriver CreateDriverWithCustomizedAuth(
public async Task TestCustomAuthExample()
{
// Given
var driver = CreateDriverWithCustomizedAuth(Uri, User, Password, "native", "basic", null);
var driver = CreateDriverWithCustomizedAuth(Uri, User, Password, "native", AuthSchemes.Basic, null);
await using var session = driver.AsyncSession();
// When & Then
var result = await session.RunAsync("RETURN 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Neo4j.Driver.Auth;

namespace Neo4j.Driver.IntegrationTests.Internals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using Castle.Core.Internal;
using Neo4j.Driver.Auth;

namespace Neo4j.Driver.IntegrationTests.Internals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using Neo4j.Driver.Auth;

namespace Neo4j.Driver.IntegrationTests.Internals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.IO;
using Neo4j.Driver.Auth;

namespace Neo4j.Driver.IntegrationTests.Internals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using Neo4j.Driver.Auth;

namespace Neo4j.Driver.IntegrationTests.Internals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Diagnostics;
using Neo4j.Driver.Auth;
using Neo4j.Driver.TestUtil;

namespace Neo4j.Driver.IntegrationTests.Internals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Linq;
using Neo4j.Driver.Auth;
using Org.BouncyCastle.Pkcs;

namespace Neo4j.Driver.IntegrationTests.Internals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

using System;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Internals;
using Neo4j.Driver.TestUtil;
using Xunit;
Expand Down
15 changes: 14 additions & 1 deletion Neo4j.Driver/Neo4j.Driver.Tests.Integration/Stress/StressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Neo4j.Driver.Auth;
using Neo4j.Driver.IntegrationTests.Extensions;
using Neo4j.Driver.IntegrationTests.Internals;
using Neo4j.Driver.Internal;
Expand Down Expand Up @@ -841,9 +842,21 @@ public MonitoredPooledConnectionFactory(IPooledConnectionFactory factory)
public IPooledConnection Create(
Uri uri,
IConnectionReleaseManager releaseManager,
SocketSettings socketSettings,
IAuthToken authToken,
IAuthTokenManager authTokenManager,
string userAgent,
IDictionary<string, string> routingContext)
{
var pooledConnection = _delegate.Create(uri, releaseManager, routingContext);
var pooledConnection = _delegate.Create(
uri,
releaseManager,
socketSettings,
authToken,
authTokenManager,
userAgent,
routingContext);

Connections.Enqueue(pooledConnection);
return pooledConnection;
}
Expand Down
6 changes: 3 additions & 3 deletions Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public async Task Process(bool restartInitialState, Func<Exception, bool> loopCo
}
catch (TestKitProtocolException ex)
{
Trace.WriteLine($"TestKit protocol exception detected: {ex.Message}");
Trace.WriteLine($"TestKit protocol exception detected: {ex}");
await ResponseWriter.WriteResponseAsync(ExceptionManager.GenerateExceptionResponse(ex));
storedException = ex;
restartConnection = true;
Expand All @@ -160,14 +160,14 @@ public async Task Process(bool restartInitialState, Func<Exception, bool> loopCo
catch (IOException ex)
{
//Handled outside of the exception manager because there is no connection to reply on.
Trace.WriteLine($"Socket exception detected: {ex.Message}");
Trace.WriteLine($"Socket exception detected: {ex}");

storedException = ex;
restartConnection = true;
}
catch (Exception ex)
{
Trace.WriteLine($"General exception detected, restarting connection: {ex.Message}");
Trace.WriteLine($"General exception detected, restarting connection: {ex}");
storedException = ex;
restartConnection = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Neo4j.Driver.Internal.Connector;

namespace Neo4j.Driver.Tests.TestBackend;
//TransientException = DriverError
Expand Down Expand Up @@ -69,7 +70,8 @@ internal static class ExceptionManager
{ typeof(ArgumentErrorException), "ArgumentError" },
{ typeof(TypeException), "TypeError" },
{ typeof(ForbiddenException), "ForbiddenError" },
{ typeof(UnknownSecurityException), "OtherSecurityException" }
{ typeof(UnknownSecurityException), "OtherSecurityException" },
{ typeof(ReauthException), "UnsupportedFeatureException"}
};

internal static ProtocolResponse GenerateExceptionResponse(Exception ex)
Expand Down Expand Up @@ -104,7 +106,7 @@ internal static ProtocolResponse GenerateExceptionResponse(Exception ex)
new
{
id = newError.uniqueId,
errorType = ex.InnerException.GetType().Name,
errorType = ex.InnerException?.GetType().Name ?? ex.GetType().Name,
msg = exceptionMessage
});
}
Expand All @@ -118,9 +120,8 @@ internal static ProtocolResponse GenerateExceptionResponse(Exception ex)
msg = ex.Message
});
}

Trace.WriteLine(
$"Exception thrown {outerExceptionMessage}\n which contained -- {exceptionMessage}\n{ex.StackTrace}");

Trace.WriteLine($"Unhandled exception thrown {ex}");

return new ProtocolResponse("BackendError", new { msg = exceptionMessage });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<bool> ParseNextRequest()
;
}

Trace.WriteLine($"\nRequest recieved: {CurrentObjectData}");
Trace.WriteLine($"\nRequest received: {CurrentObjectData}");

return !string.IsNullOrEmpty(CurrentObjectData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props"/>
<Import Project="..\common.props" />
<PropertyGroup>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Neo4j.Driver\Neo4j.Driver.csproj"/>
<ProjectReference Include="..\Neo4j.Driver\Neo4j.Driver.csproj" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [http://neo4j.com]
//
// This file is part of Neo4j.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;

namespace Neo4j.Driver.Tests.TestBackend;

internal class AuthTokenManagerClose : IProtocolObject
{
public AuthTokenManagerCloseType data { get; set; } = new();

public override string Respond()
{
return new ProtocolResponse("AuthTokenManager", uniqueId).Encode();
}

public class AuthTokenManagerCloseType
{
public string id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [http://neo4j.com]
//
// This file is part of Neo4j.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Neo4j.Driver.Tests.TestBackend;

internal class AuthTokenManagerGetAuthCompleted : IProtocolObject
{
public AuthTokenManagerGetAuthCompletedDto data { get; set; }

public class AuthTokenManagerGetAuthCompletedDto
{
public string requestId { get; set; }
public AuthorizationToken auth { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [http://neo4j.com]
//
// This file is part of Neo4j.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Neo4j.Driver.Tests.TestBackend;

internal class AuthTokenManagerOnAuthExpiredCompleted : IProtocolObject
{
public AuthTokenManagerOnExpiredCompletedDto data { get; set; }

public class AuthTokenManagerOnExpiredCompletedDto
{
public string requestId { get; set; }
}
}
Loading

0 comments on commit 7a1d600

Please sign in to comment.