Skip to content

Commit 073d827

Browse files
committed
Support for custom auth-tokens
1 parent 58b63e7 commit 073d827

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

driver/src/main/java/org/neo4j/driver/v1/AuthTokens.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver.v1;
2020

21+
import java.util.Map;
22+
2123
import org.neo4j.driver.internal.security.InternalAuthToken;
2224

2325
import static org.neo4j.driver.v1.Values.parameters;
@@ -46,6 +48,62 @@ public static AuthToken basic( String username, String password )
4648
"credentials", password ).asMap( Values.ofValue() ) );
4749
}
4850

51+
/**
52+
* The basic authentication scheme, using a username and a password.
53+
* @param username this is the "principal", identifying who this token represents
54+
* @param password this is the "credential", proving the identity of the user
55+
* @param realm this is the "realm", specifies the authentication provider
56+
* @return an authentication token that can be used to connect to Neo4j
57+
* @see GraphDatabase#driver(String, AuthToken)
58+
*/
59+
public static AuthToken basic( String username, String password, String realm )
60+
{
61+
return new InternalAuthToken( parameters(
62+
"scheme", "basic",
63+
"principal", username,
64+
"credentials", password,
65+
"realm", realm).asMap( Values.ofValue() ) );
66+
}
67+
68+
69+
/**
70+
* A custom authentication token used for doing custom authentication on the server side.
71+
* @param principal this used to identify who this token represents
72+
* @param credentials this is credentials authenticating the principal
73+
* @param realm this is the "realm:, specifying the authentication provider.
74+
* @param scheme this it the authentication scheme, specifying what kind of authentication that should be used
75+
* @return an authentication token that can be used to connect to Neo4j
76+
* * @see GraphDatabase#driver(String, AuthToken)
77+
*/
78+
public static AuthToken custom( String principal, String credentials, String realm, String scheme)
79+
{
80+
return new InternalAuthToken( parameters(
81+
"scheme", scheme,
82+
"principal", principal,
83+
"credentials", credentials,
84+
"realm", realm).asMap( Values.ofValue() ) );
85+
}
86+
87+
/**
88+
* A custom authentication token used for doing custom authentication on the server side.
89+
* @param principal this used to identify who this token represents
90+
* @param credentials this is credentials authenticating the principal
91+
* @param realm this is the "realm:, specifying the authentication provider.
92+
* @param scheme this it the authentication scheme, specifying what kind of authentication that shoud be used
93+
* @param parameters extra parameters to be sent along the authentication provider.
94+
* @return an authentication token that can be used to connect to Neo4j
95+
* * @see GraphDatabase#driver(String, AuthToken)
96+
*/
97+
public static AuthToken custom( String principal, String credentials, String realm, String scheme, Map<String, Object> parameters)
98+
{
99+
return new InternalAuthToken( parameters(
100+
"scheme", scheme,
101+
"principal", principal,
102+
"credentials", credentials,
103+
"realm", realm,
104+
"parameters", parameters).asMap( Values.ofValue() ) );
105+
}
106+
49107
/**
50108
* No authentication scheme. This will only work if authentication is disabled
51109
* on the Neo4j Instance we are connecting to.

driver/src/test/java/org/neo4j/driver/v1/integration/CredentialsIT.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.rules.ExpectedException;
2424
import org.junit.rules.TemporaryFolder;
2525

26+
import java.util.HashMap;
27+
2628
import org.neo4j.driver.internal.security.InternalAuthToken;
2729
import org.neo4j.driver.v1.Driver;
2830
import org.neo4j.driver.v1.GraphDatabase;
@@ -35,6 +37,7 @@
3537
import static org.hamcrest.MatcherAssert.assertThat;
3638
import static org.hamcrest.Matchers.equalTo;
3739
import static org.neo4j.driver.v1.AuthTokens.basic;
40+
import static org.neo4j.driver.v1.AuthTokens.custom;
3841
import static org.neo4j.driver.v1.Values.ofValue;
3942
import static org.neo4j.driver.v1.Values.parameters;
4043

@@ -85,6 +88,42 @@ public void shouldGetHelpfulErrorOnInvalidCredentials() throws Throwable
8588
}
8689
}
8790

91+
@Test
92+
public void shouldBeAbleToConnectWithCustomToken() throws Throwable
93+
{
94+
// Given
95+
String password = "secret";
96+
enableAuth( password );
97+
98+
// When & Then
99+
try( Driver driver = GraphDatabase.driver( neo4j.uri(),
100+
custom("neo4j", password, "native", "basic" ) );
101+
Session session = driver.session() )
102+
{
103+
Value single = session.run( "CREATE () RETURN 1" ).single().get( 0 );
104+
assertThat( single.asLong(), equalTo( 1L ) );
105+
}
106+
}
107+
108+
@Test
109+
public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters() throws Throwable
110+
{
111+
// Given
112+
String password = "secret";
113+
enableAuth( password );
114+
HashMap<String,Object> parameters = new HashMap<>();
115+
parameters.put( "secret", 16 );
116+
117+
// When & Then
118+
try( Driver driver = GraphDatabase.driver( neo4j.uri(),
119+
custom("neo4j", password, "native", "basic", parameters ) );
120+
Session session = driver.session() )
121+
{
122+
Value single = session.run( "CREATE () RETURN 1" ).single().get( 0 );
123+
assertThat( single.asLong(), equalTo( 1L ) );
124+
}
125+
}
126+
88127
private void enableAuth( String password ) throws Exception
89128
{
90129
neo4j.restart( Neo4jSettings.TEST_SETTINGS

0 commit comments

Comments
 (0)