Skip to content

Commit

Permalink
More consistent getter syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
henriknyman committed Sep 27, 2016
1 parent 5a7d60f commit cbb2db6
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 57 deletions.
Expand Up @@ -42,19 +42,19 @@ private PluginApiAuthToken( String principal, char[] credentials, Map<String,Obj
} }


@Override @Override
public String getPrincipal() public String principal()
{ {
return principal; return principal;
} }


@Override @Override
public char[] getCredentials() public char[] credentials()
{ {
return credentials; return credentials;
} }


@Override @Override
public Map<String,Object> getParameters() public Map<String,Object> parameters()
{ {
return parameters; return parameters;
} }
Expand Down
Expand Up @@ -53,28 +53,28 @@ private PluginAuthInfo( Object principal, Object hashedCredentials, ByteSource c


private PluginAuthInfo( AuthInfo authInfo, SimpleHash hashedCredentials, String realmName ) private PluginAuthInfo( AuthInfo authInfo, SimpleHash hashedCredentials, String realmName )
{ {
this( authInfo.getPrincipal(), hashedCredentials.getBytes(), hashedCredentials.getSalt(), realmName, this( authInfo.principal(), hashedCredentials.getBytes(), hashedCredentials.getSalt(), realmName,
authInfo.getRoles().stream().collect( Collectors.toSet() ) ); authInfo.roles().stream().collect( Collectors.toSet() ) );
} }


public static PluginAuthInfo create( AuthInfo authInfo, String realmName ) public static PluginAuthInfo create( AuthInfo authInfo, String realmName )
{ {
return new PluginAuthInfo( authInfo.getPrincipal(), realmName, return new PluginAuthInfo( authInfo.principal(), realmName,
authInfo.getRoles().stream().collect( Collectors.toSet() ) ); authInfo.roles().stream().collect( Collectors.toSet() ) );
} }


public static PluginAuthInfo createCacheable( AuthInfo authInfo, String realmName, SecureHasher secureHasher ) public static PluginAuthInfo createCacheable( AuthInfo authInfo, String realmName, SecureHasher secureHasher )
{ {
if ( authInfo instanceof CacheableAuthInfo ) if ( authInfo instanceof CacheableAuthInfo )
{ {
byte[] credentials = ((CacheableAuthInfo) authInfo).getCredentials(); byte[] credentials = ((CacheableAuthInfo) authInfo).credentials();
SimpleHash hashedCredentials = secureHasher.hash( credentials ); SimpleHash hashedCredentials = secureHasher.hash( credentials );
return new PluginAuthInfo( authInfo, hashedCredentials, realmName ); return new PluginAuthInfo( authInfo, hashedCredentials, realmName );
} }
else else
{ {
return new PluginAuthInfo( authInfo.getPrincipal(), realmName, return new PluginAuthInfo( authInfo.principal(), realmName,
authInfo.getRoles().stream().collect( Collectors.toSet() ) ); authInfo.roles().stream().collect( Collectors.toSet() ) );
} }
} }


Expand Down
Expand Up @@ -55,13 +55,13 @@ public CustomCacheableAuthenticationInfo.CredentialsMatcher getCredentialsMatche


private static PluginAuthenticationInfo create( AuthenticationInfo authenticationInfo, String realmName ) private static PluginAuthenticationInfo create( AuthenticationInfo authenticationInfo, String realmName )
{ {
return new PluginAuthenticationInfo( authenticationInfo.getPrincipal(), realmName, null ); return new PluginAuthenticationInfo( authenticationInfo.principal(), realmName, null );
} }


private static PluginAuthenticationInfo create( AuthenticationInfo authenticationInfo, SimpleHash hashedCredentials, private static PluginAuthenticationInfo create( AuthenticationInfo authenticationInfo, SimpleHash hashedCredentials,
String realmName ) String realmName )
{ {
return new PluginAuthenticationInfo( authenticationInfo.getPrincipal(), return new PluginAuthenticationInfo( authenticationInfo.principal(),
hashedCredentials.getBytes(), hashedCredentials.getSalt(), realmName ); hashedCredentials.getBytes(), hashedCredentials.getSalt(), realmName );
} }


Expand All @@ -71,11 +71,11 @@ public static PluginAuthenticationInfo createCacheable( AuthenticationInfo authe
if ( authenticationInfo instanceof CustomCacheableAuthenticationInfo ) if ( authenticationInfo instanceof CustomCacheableAuthenticationInfo )
{ {
CustomCacheableAuthenticationInfo info = (CustomCacheableAuthenticationInfo) authenticationInfo; CustomCacheableAuthenticationInfo info = (CustomCacheableAuthenticationInfo) authenticationInfo;
return new PluginAuthenticationInfo( authenticationInfo, realmName, info.getCredentialsMatcher() ); return new PluginAuthenticationInfo( authenticationInfo, realmName, info.credentialsMatcher() );
} }
else if ( authenticationInfo instanceof CacheableAuthenticationInfo ) else if ( authenticationInfo instanceof CacheableAuthenticationInfo )
{ {
byte[] credentials = ((CacheableAuthenticationInfo) authenticationInfo).getCredentials(); byte[] credentials = ((CacheableAuthenticationInfo) authenticationInfo).credentials();
SimpleHash hashedCredentials = secureHasher.hash( credentials ); SimpleHash hashedCredentials = secureHasher.hash( credentials );
return PluginAuthenticationInfo.create( authenticationInfo, hashedCredentials, realmName ); return PluginAuthenticationInfo.create( authenticationInfo, hashedCredentials, realmName );
} }
Expand Down
Expand Up @@ -35,6 +35,6 @@ private PluginAuthorizationInfo( Set<String> roles )


public static PluginAuthorizationInfo create( AuthorizationInfo authorizationInfo ) public static PluginAuthorizationInfo create( AuthorizationInfo authorizationInfo )
{ {
return new PluginAuthorizationInfo( new LinkedHashSet<>( authorizationInfo.getRoles() ) ); return new PluginAuthorizationInfo( new LinkedHashSet<>( authorizationInfo.roles() ) );
} }
} }
Expand Up @@ -38,7 +38,7 @@ public interface AuthToken
* *
* @return the identity to authenticate. * @return the identity to authenticate.
*/ */
String getPrincipal(); String principal();


/** /**
* Returns the credentials that verifies the identity. * Returns the credentials that verifies the identity.
Expand All @@ -50,7 +50,7 @@ public interface AuthToken
* *
* @return the credentials that verifies the identity. * @return the credentials that verifies the identity.
*/ */
char[] getCredentials(); char[] credentials();


/** /**
* Returns an optional custom parameter map if provided by the client. * Returns an optional custom parameter map if provided by the client.
Expand All @@ -60,5 +60,5 @@ public interface AuthToken
* *
* @return a custom parameter map if provided by the client, otherwise <tt>null</tt> * @return a custom parameter map if provided by the client, otherwise <tt>null</tt>
*/ */
Map<String,Object> getParameters(); Map<String,Object> parameters();
} }
Expand Up @@ -43,27 +43,27 @@ public interface AuthInfo extends Serializable
* *
* @return a principal that uniquely identifies the authenticated subject within this realm. * @return a principal that uniquely identifies the authenticated subject within this realm.
*/ */
Object getPrincipal(); Object principal();


/** /**
* Should return the roles assigned to this principal. * Should return the roles assigned to this principal.
* *
* @return the roles assigned to this principal * @return the roles assigned to this principal
*/ */
Collection<String> getRoles(); Collection<String> roles();


static AuthInfo of( Object principal, Collection<String> roles ) static AuthInfo of( Object principal, Collection<String> roles )
{ {
return new AuthInfo() return new AuthInfo()
{ {
@Override @Override
public Object getPrincipal() public Object principal()
{ {
return principal; return principal;
} }


@Override @Override
public Collection<String> getRoles() public Collection<String> roles()
{ {
return roles; return roles;
} }
Expand Down
Expand Up @@ -37,7 +37,7 @@ public interface AuthenticationInfo extends Serializable
* *
* @return a principal that uniquely identifies the authenticated subject within this realm. * @return a principal that uniquely identifies the authenticated subject within this realm.
*/ */
Object getPrincipal(); Object principal();


static AuthenticationInfo of( Object principal ) static AuthenticationInfo of( Object principal )
{ {
Expand Down
Expand Up @@ -34,7 +34,7 @@ public interface AuthorizationInfo extends Serializable
* *
* @return the roles assigned to the principals recognized by an <tt>AuthorizationPlugin</tt> * @return the roles assigned to the principals recognized by an <tt>AuthorizationPlugin</tt>
*/ */
Collection<String> getRoles(); Collection<String> roles();


static AuthorizationInfo of( Collection<String> roles ) static AuthorizationInfo of( Collection<String> roles )
{ {
Expand Down
Expand Up @@ -58,10 +58,10 @@ public interface CacheableAuthInfo extends AuthInfo
* *
* @return a principal that uniquely identifies the authenticated subject within this realm * @return a principal that uniquely identifies the authenticated subject within this realm
* *
* @see AuthToken#getPrincipal() * @see AuthToken#principal()
*/ */
@Override @Override
Object getPrincipal(); Object principal();


/** /**
* Should return credentials that can be cached, so that successive authentication attempts could be performed * Should return credentials that can be cached, so that successive authentication attempts could be performed
Expand All @@ -73,29 +73,29 @@ public interface CacheableAuthInfo extends AuthInfo
* *
* @return credentials that can be cached * @return credentials that can be cached
* *
* @see AuthToken#getCredentials() * @see AuthToken#credentials()
* @see AuthPlugin#authenticateAndAuthorize(AuthToken) * @see AuthPlugin#authenticateAndAuthorize(AuthToken)
*/ */
byte[] getCredentials(); byte[] credentials();


static CacheableAuthInfo of( Object principal, byte[] credentials, Collection<String> roles ) static CacheableAuthInfo of( Object principal, byte[] credentials, Collection<String> roles )
{ {
return new CacheableAuthInfo() return new CacheableAuthInfo()
{ {
@Override @Override
public Object getPrincipal() public Object principal()
{ {
return principal; return principal;
} }


@Override @Override
public byte[] getCredentials() public byte[] credentials()
{ {
return credentials; return credentials;
} }


@Override @Override
public Collection<String> getRoles() public Collection<String> roles()
{ {
return roles; return roles;
} }
Expand Down
Expand Up @@ -47,10 +47,10 @@ public interface CacheableAuthenticationInfo extends AuthenticationInfo
* *
* @return a principal that uniquely identifies the authenticated subject within this realm * @return a principal that uniquely identifies the authenticated subject within this realm
* *
* @see AuthToken#getPrincipal() * @see AuthToken#principal()
*/ */
@Override @Override
Object getPrincipal(); Object principal();


/** /**
* Should return credentials that can be cached, so that successive authentication attempts could be performed * Should return credentials that can be cached, so that successive authentication attempts could be performed
Expand All @@ -62,10 +62,10 @@ public interface CacheableAuthenticationInfo extends AuthenticationInfo
* *
* @return credentials that can be cached * @return credentials that can be cached
* *
* @see AuthToken#getCredentials() * @see AuthToken#credentials()
* @see AuthenticationPlugin#authenticate(AuthToken) * @see AuthenticationPlugin#authenticate(AuthToken)
*/ */
byte[] getCredentials(); byte[] credentials();


/** /**
* Creates a new <tt>CacheableAuthenticationInfo</tt> * Creates a new <tt>CacheableAuthenticationInfo</tt>
Expand All @@ -79,13 +79,13 @@ static CacheableAuthenticationInfo of( Object principal, byte[] credentials )
return new CacheableAuthenticationInfo() return new CacheableAuthenticationInfo()
{ {
@Override @Override
public Object getPrincipal() public Object principal()
{ {
return principal; return principal;
} }


@Override @Override
public byte[] getCredentials() public byte[] credentials()
{ {
return credentials; return credentials;
} }
Expand Down
Expand Up @@ -31,7 +31,7 @@
* <p>This is an alternative to <tt>CacheableAuthenticationInfo</tt> to use if you want to manage your own way of * <p>This is an alternative to <tt>CacheableAuthenticationInfo</tt> to use if you want to manage your own way of
* hashing and matching credentials. On authentication, when a cached authentication info from a previous successful * hashing and matching credentials. On authentication, when a cached authentication info from a previous successful
* authentication attempt is found for the principal within the auth token map, then <tt>doCredentialsMatch</tt> * authentication attempt is found for the principal within the auth token map, then <tt>doCredentialsMatch</tt>
* of the <tt>CredentialsMatcher</tt> returned by <tt>getCredentialsMatcher</tt> will be called to determine * of the <tt>CredentialsMatcher</tt> returned by <tt>credentialsMatcher</tt> will be called to determine
* if the credentials match. * if the credentials match.
* *
* <p>NOTE: Caching only occurs if it is explicitly enabled by the plugin. * <p>NOTE: Caching only occurs if it is explicitly enabled by the plugin.
Expand Down Expand Up @@ -66,20 +66,20 @@ interface CredentialsMatcher
* @return the credentials matcher that will be used to verify the credentials of an auth token against the * @return the credentials matcher that will be used to verify the credentials of an auth token against the
* cached credentials in this object * cached credentials in this object
*/ */
CredentialsMatcher getCredentialsMatcher(); CredentialsMatcher credentialsMatcher();


static CustomCacheableAuthenticationInfo of( Object principal, CredentialsMatcher credentialsMatcher ) static CustomCacheableAuthenticationInfo of( Object principal, CredentialsMatcher credentialsMatcher )
{ {
return new CustomCacheableAuthenticationInfo() return new CustomCacheableAuthenticationInfo()
{ {
@Override @Override
public Object getPrincipal() public Object principal()
{ {
return principal; return principal;
} }


@Override @Override
public CredentialsMatcher getCredentialsMatcher() public CredentialsMatcher credentialsMatcher()
{ {
return credentialsMatcher; return credentialsMatcher;
} }
Expand Down
Expand Up @@ -21,7 +21,6 @@


import java.util.Hashtable; import java.util.Hashtable;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;


import javax.naming.Context; import javax.naming.Context;
Expand Down Expand Up @@ -57,8 +56,8 @@ public AuthInfo authenticateAndAuthorize( AuthToken authToken ) throws Authentic
{ {
try try
{ {
String username = authToken.getPrincipal(); String username = authToken.principal();
char[] password = authToken.getCredentials(); char[] password = authToken.credentials();


LdapContext ctx = authenticate( username, password ); LdapContext ctx = authenticate( username, password );
Set<String> roles = authorize( ctx, username ); Set<String> roles = authorize( ctx, username );
Expand Down
Expand Up @@ -38,8 +38,8 @@ public String name()
@Override @Override
public AuthInfo authenticateAndAuthorize( AuthToken authToken ) public AuthInfo authenticateAndAuthorize( AuthToken authToken )
{ {
String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
Expand Down
Expand Up @@ -36,8 +36,8 @@ public String name()
@Override @Override
public AuthenticationInfo authenticate( AuthToken authToken ) public AuthenticationInfo authenticate( AuthToken authToken )
{ {
String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
Expand Down
Expand Up @@ -42,8 +42,8 @@ public AuthInfo authenticateAndAuthorize( AuthToken authToken )
{ {
getAuthInfoCallCount.incrementAndGet(); getAuthInfoCallCount.incrementAndGet();


String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
Expand Down
Expand Up @@ -40,8 +40,8 @@ public AuthenticationInfo authenticate( AuthToken authToken )
{ {
getAuthenticationInfoCallCount.incrementAndGet(); getAuthenticationInfoCallCount.incrementAndGet();


String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
Expand Down
Expand Up @@ -41,8 +41,8 @@ public String name()
@Override @Override
public AuthenticationInfo authenticate( AuthToken authToken ) public AuthenticationInfo authenticate( AuthToken authToken )
{ {
String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
Expand Down
Expand Up @@ -41,14 +41,14 @@ public AuthenticationInfo authenticate( AuthToken authToken )
{ {
getAuthenticationInfoCallCount.incrementAndGet(); getAuthenticationInfoCallCount.incrementAndGet();


String principal = authToken.getPrincipal(); String principal = authToken.principal();
char[] credentials = authToken.getCredentials(); char[] credentials = authToken.credentials();


if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) ) if ( principal.equals( "neo4j" ) && Arrays.equals( credentials, "neo4j".toCharArray() ) )
{ {
return CustomCacheableAuthenticationInfo.of( "neo4j", return CustomCacheableAuthenticationInfo.of( "neo4j",
( token ) -> { ( token ) -> {
char[] tokenCredentials = token.getCredentials(); char[] tokenCredentials = token.credentials();
return Arrays.equals( tokenCredentials, "neo4j".toCharArray() ); return Arrays.equals( tokenCredentials, "neo4j".toCharArray() );
} ); } );
} }
Expand Down

0 comments on commit cbb2db6

Please sign in to comment.