Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cea1e9
commit 3dfc0d6
Showing
39 changed files
with
834 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
community/bolt/src/main/java/org/neo4j/bolt/security/auth/Authentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.bolt.security.auth; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Authenticate a given token. | ||
* <p> | ||
* The provided token must contain the following items: | ||
* <ul> | ||
* <li><code>scheme</code>: a string defining the authentication scheme.</li> | ||
* <li><code>principal</code>: The security principal, the format of the value depends on the authentication | ||
* scheme.</li> | ||
* <li><code>credentials</code>: The credentials corresponding to the <code>principal</code>, the format of the | ||
* value depends on the authentication scheme.</li> | ||
* </ul> | ||
* <p> | ||
* | ||
* For updating the credentials the new credentials is supplied with the key <code>new-credentials</code>. | ||
*/ | ||
public interface Authentication | ||
{ | ||
/** | ||
* Authenticate the provided token. | ||
* @param authToken The token to be authenticated. | ||
* @throws AuthenticationException If authentication failed. | ||
*/ | ||
void authenticate( Map<String,Object> authToken ) throws AuthenticationException; | ||
|
||
/** | ||
* Allows all tokens to authenticate. | ||
*/ | ||
Authentication NONE = authToken -> { | ||
//Do nothing | ||
}; | ||
|
||
String SCHEME_KEY = "scheme"; | ||
String PRINCIPAL = "principal"; | ||
String CREDENTIALS = "credentials"; | ||
String NEW_CREDENTIALS = "new-credentials"; | ||
} |
53 changes: 53 additions & 0 deletions
53
community/bolt/src/main/java/org/neo4j/bolt/security/auth/AuthenticationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.bolt.security.auth; | ||
|
||
import java.io.IOException; | ||
|
||
import org.neo4j.kernel.api.exceptions.Status; | ||
|
||
public class AuthenticationException extends IOException implements Status.HasStatus | ||
{ | ||
private final Status status; | ||
|
||
public AuthenticationException( Status status ) | ||
{ | ||
super( status.code().description() ); | ||
this.status = status; | ||
} | ||
public AuthenticationException( Status status, String message ) | ||
{ | ||
super( message ); | ||
this.status = status; | ||
} | ||
|
||
public AuthenticationException( Status status, String message, Throwable e ) | ||
{ | ||
super(message, e); | ||
this.status = status; | ||
} | ||
|
||
|
||
@Override | ||
public Status status() | ||
{ | ||
return status; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
community/bolt/src/main/java/org/neo4j/bolt/security/auth/BasicAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.bolt.security.auth; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.neo4j.kernel.api.exceptions.Status; | ||
import org.neo4j.server.security.auth.AuthManager; | ||
|
||
/** | ||
* Performs basic authentication with user name and password. | ||
*/ | ||
public class BasicAuthentication implements Authentication | ||
{ | ||
private final AuthManager authManager; | ||
private final static String SCHEME = "basic"; | ||
|
||
|
||
public BasicAuthentication( AuthManager authManager ) | ||
{ | ||
this.authManager = authManager; | ||
} | ||
|
||
@Override | ||
public void authenticate( Map<String,Object> authToken ) throws AuthenticationException | ||
{ | ||
if ( !SCHEME.equals( authToken.get( SCHEME_KEY ) ) ) | ||
{ | ||
throw new AuthenticationException( Status.Security.AuthenticationFailed, | ||
"Authorization token must contain: '" + SCHEME_KEY + " : " + SCHEME + "'" ); | ||
} | ||
|
||
String user = safeCast( PRINCIPAL, authToken ); | ||
String password = safeCast( CREDENTIALS, authToken ); | ||
if ( authToken.containsKey( NEW_CREDENTIALS ) ) | ||
{ | ||
update( user, password, safeCast( NEW_CREDENTIALS, authToken ) ); | ||
} | ||
else | ||
{ | ||
authenticate( user, password ); | ||
} | ||
} | ||
|
||
private void authenticate( String user, String password ) throws AuthenticationException | ||
{ | ||
switch ( authManager.authenticate( user, password ) ) | ||
{ | ||
case SUCCESS: | ||
break; | ||
case PASSWORD_CHANGE_REQUIRED: | ||
throw new AuthenticationException( Status.Security.CredentialsExpired ); | ||
case TOO_MANY_ATTEMPTS: | ||
throw new AuthenticationException( Status.Security.AuthenticationRateLimit ); | ||
case FAILURE: | ||
throw new AuthenticationException( Status.Security.AuthenticationFailed ); | ||
} | ||
} | ||
|
||
private void update( String user, String password, String newPassword ) throws AuthenticationException | ||
{ | ||
switch ( authManager.authenticate( user, password ) ) | ||
{ | ||
case SUCCESS: | ||
case PASSWORD_CHANGE_REQUIRED: | ||
try | ||
{ | ||
authManager.setPassword( user, newPassword ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new AuthenticationException( Status.Security.AuthenticationFailed, e.getMessage(), e ); | ||
} | ||
break; | ||
default: | ||
throw new AuthenticationException( Status.Security.AuthenticationFailed ); | ||
} | ||
} | ||
|
||
private String safeCast( String key, Map<String,Object> authToken ) throws AuthenticationException | ||
{ | ||
Object value = authToken.get( key ); | ||
if ( value == null || !(value instanceof String) ) | ||
{ | ||
throw new AuthenticationException( Status.Security.AuthenticationFailed, | ||
"The value associated with the key `" + key + "` must be a String but was: " + | ||
(value == null ? "null" : value.getClass().getSimpleName())); | ||
} | ||
|
||
return (String) value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.