Skip to content

Commit 3a5da0d

Browse files
author
Zhen Li
authored
Merge pull request #221 from pontusmelke/1.1-more-auth
Support for custom auth-tokens
2 parents 58b63e7 + 229f3b7 commit 3a5da0d

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-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.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1;
20+
21+
import org.junit.Test;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import org.neo4j.driver.internal.security.InternalAuthToken;
27+
import org.neo4j.driver.internal.value.ListValue;
28+
import org.neo4j.driver.internal.value.MapValue;
29+
import org.neo4j.driver.internal.value.StringValue;
30+
31+
import static java.util.Arrays.asList;
32+
import static org.hamcrest.core.IsEqual.equalTo;
33+
import static org.junit.Assert.assertThat;
34+
import static org.neo4j.driver.v1.AuthTokens.basic;
35+
import static org.neo4j.driver.v1.AuthTokens.custom;
36+
import static org.neo4j.driver.v1.Values.values;
37+
38+
public class AuthTokensTest
39+
{
40+
41+
@Test
42+
public void basicAuthWithoutRealm()
43+
{
44+
InternalAuthToken basic = (InternalAuthToken) basic( "foo", "bar" );
45+
46+
Map<String,Value> map = basic.toMap();
47+
48+
assertThat( map.size(), equalTo( 3 ) );
49+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "basic" ) ) );
50+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
51+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
52+
}
53+
54+
@Test
55+
public void basicAuthWithRealm()
56+
{
57+
InternalAuthToken basic = (InternalAuthToken) basic( "foo", "bar", "baz" );
58+
59+
Map<String,Value> map = basic.toMap();
60+
61+
assertThat( map.size(), equalTo( 4 ) );
62+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "basic" ) ) );
63+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
64+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
65+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
66+
}
67+
68+
@Test
69+
public void customAuthWithoutParameters()
70+
{
71+
InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme" );
72+
73+
Map<String,Value> map = basic.toMap();
74+
75+
assertThat( map.size(), equalTo( 4 ) );
76+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) );
77+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
78+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
79+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
80+
}
81+
82+
@Test
83+
public void customAuthParameters()
84+
{
85+
HashMap<String,Object> parameters = new HashMap<>();
86+
parameters.put( "list", asList( 1, 2, 3 ) );
87+
InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme", parameters );
88+
89+
90+
Map<String,Value> expectedParameters = new HashMap<>();
91+
expectedParameters.put( "list", new ListValue( values( 1, 2, 3 ) ) );
92+
Map<String,Value> map = basic.toMap();
93+
94+
assertThat( map.size(), equalTo( 5 ) );
95+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) );
96+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
97+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
98+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
99+
assertThat( map.get( "parameters" ), equalTo( (Value) new MapValue( expectedParameters ) ) );
100+
}
101+
}

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

Lines changed: 56 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,59 @@ public void shouldGetHelpfulErrorOnInvalidCredentials() throws Throwable
8588
}
8689
}
8790

91+
@Test
92+
public void shouldBeAbleToProvideRealmWithBasicAuth() 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+
basic("neo4j", password, "native") );
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 shouldBeAbleToConnectWithCustomToken() throws Throwable
110+
{
111+
// Given
112+
String password = "secret";
113+
enableAuth( password );
114+
115+
// When & Then
116+
try( Driver driver = GraphDatabase.driver( neo4j.uri(),
117+
custom("neo4j", password, "native", "basic" ) );
118+
Session session = driver.session() )
119+
{
120+
Value single = session.run( "CREATE () RETURN 1" ).single().get( 0 );
121+
assertThat( single.asLong(), equalTo( 1L ) );
122+
}
123+
}
124+
125+
@Test
126+
public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters() throws Throwable
127+
{
128+
// Given
129+
String password = "secret";
130+
enableAuth( password );
131+
HashMap<String,Object> parameters = new HashMap<>();
132+
parameters.put( "secret", 16 );
133+
134+
// When & Then
135+
try( Driver driver = GraphDatabase.driver( neo4j.uri(),
136+
custom("neo4j", password, "native", "basic", parameters ) );
137+
Session session = driver.session() )
138+
{
139+
Value single = session.run( "CREATE () RETURN 1" ).single().get( 0 );
140+
assertThat( single.asLong(), equalTo( 1L ) );
141+
}
142+
}
143+
88144
private void enableAuth( String password ) throws Exception
89145
{
90146
neo4j.restart( Neo4jSettings.TEST_SETTINGS

0 commit comments

Comments
 (0)