Skip to content

Commit d12885e

Browse files
committed
Add bearer authentication support
1 parent 6ee3936 commit d12885e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import org.neo4j.driver.internal.security.InternalAuthToken;
2525

2626
import static java.util.Collections.singletonMap;
27+
import static org.neo4j.driver.Values.value;
2728
import static org.neo4j.driver.internal.security.InternalAuthToken.CREDENTIALS_KEY;
2829
import static org.neo4j.driver.internal.security.InternalAuthToken.PARAMETERS_KEY;
2930
import static org.neo4j.driver.internal.security.InternalAuthToken.PRINCIPAL_KEY;
3031
import static org.neo4j.driver.internal.security.InternalAuthToken.REALM_KEY;
3132
import static org.neo4j.driver.internal.security.InternalAuthToken.SCHEME_KEY;
3233
import static org.neo4j.driver.internal.util.Iterables.newHashMapWithSize;
33-
import static org.neo4j.driver.Values.value;
3434

3535
/**
3636
* This is a listing of the various methods of authentication supported by this
@@ -79,13 +79,32 @@ public static AuthToken basic( String username, String password, String realm )
7979
return new InternalAuthToken( map );
8080
}
8181

82+
/**
83+
* The bearer authentication scheme, using a base64 encoded token.
84+
*
85+
* @param token base64 encoded token
86+
* @return an authentication token that can be used to connect to Neo4j
87+
* @throws NullPointerException when token is {@code null}
88+
* @see GraphDatabase#driver(String, AuthToken)
89+
*/
90+
public static AuthToken bearer( String token )
91+
{
92+
Objects.requireNonNull( token, "Token can't be null" );
93+
94+
Map<String,Value> map = newHashMapWithSize( 2 );
95+
map.put( SCHEME_KEY, value( "bearer" ) );
96+
map.put( CREDENTIALS_KEY, value( token ) );
97+
return new InternalAuthToken( map );
98+
}
99+
82100
/**
83101
* The kerberos authentication scheme, using a base64 encoded ticket
102+
*
84103
* @param base64EncodedTicket a base64 encoded service ticket
85104
* @return an authentication token that can be used to connect to Neo4j
105+
* @throws NullPointerException when ticket is {@code null}
86106
* @see GraphDatabase#driver(String, AuthToken)
87107
* @since 1.3
88-
* @throws NullPointerException when ticket is {@code null}
89108
*/
90109
public static AuthToken kerberos( String base64EncodedTicket )
91110
{

driver/src/test/java/org/neo4j/driver/AuthTokensTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525

26-
import org.neo4j.driver.AuthToken;
27-
import org.neo4j.driver.AuthTokens;
28-
import org.neo4j.driver.Value;
2926
import org.neo4j.driver.internal.security.InternalAuthToken;
3027
import org.neo4j.driver.internal.value.ListValue;
3128
import org.neo4j.driver.internal.value.MapValue;
@@ -103,6 +100,22 @@ void customAuthParameters()
103100
assertThat( map.get( "parameters" ), equalTo( (Value) new MapValue( expectedParameters ) ) );
104101
}
105102

103+
@Test
104+
void shouldSupportBearerAuth()
105+
{
106+
// GIVEN
107+
String tokenStr = "token";
108+
109+
// WHEN
110+
InternalAuthToken token = (InternalAuthToken) AuthTokens.bearer( tokenStr );
111+
112+
// THEN
113+
Map<String,Value> map = token.toMap();
114+
assertThat( map.size(), equalTo( 2 ) );
115+
assertThat( map.get( "scheme" ), equalTo( new StringValue( "bearer" ) ) );
116+
assertThat( map.get( "credentials" ), equalTo( new StringValue( tokenStr ) ) );
117+
}
118+
106119
@Test
107120
void basicKerberosAuthWithRealm()
108121
{
@@ -141,6 +154,13 @@ void shouldAllowBasicAuthTokenWithNullRealm()
141154
assertEquals( "password", map.get( "credentials" ).asString() );
142155
}
143156

157+
@Test
158+
void shouldNotAllowBearerAuthTokenWithNullToken()
159+
{
160+
NullPointerException e = assertThrows( NullPointerException.class, () -> AuthTokens.bearer( null ) );
161+
assertEquals( "Token can't be null", e.getMessage() );
162+
}
163+
144164
@Test
145165
void shouldNotAllowKerberosAuthTokenWithNullTicket()
146166
{

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/NewDriver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public TestkitResponse process( TestkitState testkitState )
7474
data.authorizationToken.getTokens().get( "credentials" ),
7575
data.authorizationToken.getTokens().get( "realm" ) );
7676
break;
77+
case "bearer":
78+
authToken = AuthTokens.bearer( data.authorizationToken.getTokens().get( "credentials" ) );
79+
break;
7780
default:
7881
return BackendError.builder()
7982
.data( BackendError

0 commit comments

Comments
 (0)