use of io.crate.auth.AuthenticationMethod in project crate by crate.
the class PostgresWireProtocol method initAuthentication.
private void initAuthentication(Channel channel) {
String userName = properties.getProperty("user");
InetAddress address = Netty4HttpServerTransport.getRemoteAddress(channel);
SSLSession sslSession = getSession(channel);
ConnectionProperties connProperties = new ConnectionProperties(address, Protocol.POSTGRES, sslSession);
AuthenticationMethod authMethod = authService.resolveAuthenticationType(userName, connProperties);
if (authMethod == null) {
String errorMessage = String.format(Locale.ENGLISH, "No valid auth.host_based entry found for host \"%s\", user \"%s\". Did you enable TLS in your client?", address.getHostAddress(), userName);
Messages.sendAuthenticationError(channel, errorMessage);
} else {
authContext = new AuthenticationContext(authMethod, connProperties, userName, LOGGER);
if (PASSWORD_AUTH_NAME.equals(authMethod.name())) {
Messages.sendAuthenticationCleartextPassword(channel);
return;
}
finishAuthentication(channel);
}
}
use of io.crate.auth.AuthenticationMethod in project crate by crate.
the class AuthenticationContextTest method testAuthenticationContextCycle.
@Test
public void testAuthenticationContextCycle() throws Exception {
String userName = "crate";
char[] passwd = "passwd".toCharArray();
ConnectionProperties connProperties = new ConnectionProperties(InetAddress.getByName("127.0.0.1"), Protocol.POSTGRES, null);
AuthenticationMethod authMethod = AUTHENTICATION.resolveAuthenticationType(userName, connProperties);
AuthenticationContext authContext = new AuthenticationContext(authMethod, connProperties, userName, LogManager.getLogger(AuthenticationContextTest.class));
authContext.setSecurePassword(passwd);
assertThat(authContext.authenticate(), is(User.CRATE_USER));
assertThat(authContext.password().getChars(), is(passwd));
authContext.close();
// once the authContext has been closed it must not been re-used for authenticating a user
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("SecureString has already been closed");
authContext.password().getChars();
}
use of io.crate.auth.AuthenticationMethod in project crate by crate.
the class PostgresWireProtocolTest method testPasswordMessageAuthenticationProcess.
@Test
public void testPasswordMessageAuthenticationProcess() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new Authentication() {
@Override
public AuthenticationMethod resolveAuthenticationType(String user, ConnectionProperties connectionProperties) {
return new AuthenticationMethod() {
@Nullable
@Override
public User authenticate(String userName, @Nullable SecureString passwd, ConnectionProperties connProperties) {
return null;
}
@Override
public String name() {
return "password";
}
};
}
}, null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf respBuf;
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
channel.writeInbound(buffer);
respBuf = channel.readOutbound();
try {
// AuthenticationCleartextPassword
assertThat((char) respBuf.readByte(), is('R'));
} finally {
respBuf.release();
}
buffer = Unpooled.buffer();
ClientMessages.sendPasswordMessage(buffer, "pw");
channel.writeInbound(buffer);
respBuf = channel.readOutbound();
try {
// Auth OK
assertThat((char) respBuf.readByte(), is('R'));
} finally {
respBuf.release();
}
}
Aggregations