Skip to content

Commit 3b5a7f0

Browse files
committed
SASL test verifies connection
1 parent e44091e commit 3b5a7f0

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/main/java/io/r2dbc/postgresql/authentication/SASLAuthenticationHandler.java

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.r2dbc.postgresql.authentication;
22

33
import com.ongres.scram.client.ScramClient;
4-
import com.ongres.scram.common.StringPreparation;
54
import com.ongres.scram.common.exception.ScramException;
65
import com.ongres.scram.common.util.TlsServerEndpoint;
76
import io.r2dbc.postgresql.client.ConnectionContext;
@@ -25,6 +24,9 @@
2524
import java.security.cert.CertificateException;
2625
import java.security.cert.X509Certificate;
2726

27+
import static com.ongres.scram.common.StringPreparation.POSTGRESQL_PREPARATION;
28+
import static com.ongres.scram.common.util.TlsServerEndpoint.TLS_SERVER_END_POINT;
29+
2830
public class SASLAuthenticationHandler implements AuthenticationHandler {
2931

3032
private static final Logger LOG = Loggers.getLogger(SASLAuthenticationHandler.class);
@@ -82,22 +84,16 @@ public FrontendMessage handle(AuthenticationMessage message) {
8284
}
8385

8486
private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
85-
86-
char[] password = new char[this.password.length()];
87-
for (int i = 0; i < password.length; i++) {
88-
password[i] = this.password.charAt(i);
89-
}
90-
9187
ScramClient.FinalBuildStage builder = ScramClient.builder()
9288
.advertisedMechanisms(message.getAuthenticationMechanisms())
9389
.username(this.username) // ignored by the server, use startup message
94-
.password(password)
95-
.stringPreparation(StringPreparation.POSTGRESQL_PREPARATION);
90+
.password(password.toString().toCharArray())
91+
.stringPreparation(POSTGRESQL_PREPARATION);
9692

9793
SSLSession sslSession = this.context.getSslSession();
9894

9995
if (sslSession != null && sslSession.isValid()) {
100-
builder.channelBinding(TlsServerEndpoint.TLS_SERVER_END_POINT, extractSslEndpoint(sslSession));
96+
builder.channelBinding(TLS_SERVER_END_POINT, extractSslEndpoint(sslSession));
10197
}
10298

10399
this.scramClient = builder.build();
@@ -107,14 +103,9 @@ private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
107103

108104
private static byte[] extractSslEndpoint(SSLSession sslSession) {
109105
try {
110-
Certificate[] certificates = sslSession.getPeerCertificates();
111-
if (certificates != null && certificates.length > 0) {
112-
Certificate peerCert = certificates[0]; // First certificate is the peer's certificate
113-
if (peerCert instanceof X509Certificate) {
114-
X509Certificate cert = (X509Certificate) peerCert;
115-
return TlsServerEndpoint.getChannelBindingData(cert);
116-
117-
}
106+
Certificate[] certificates = sslSession.getPeerCertificates(); // First certificate is the peer's certificate
107+
if (certificates != null && certificates.length > 0 && certificates[0] instanceof X509Certificate ) {
108+
return TlsServerEndpoint.getChannelBindingData((X509Certificate) certificates[0]);
118109
}
119110
} catch (CertificateException | SSLException e) {
120111
LOG.debug("Cannot extract X509Certificate from SSL session", e);
@@ -125,7 +116,6 @@ private static byte[] extractSslEndpoint(SSLSession sslSession) {
125116
private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLContinue message) {
126117
try {
127118
this.scramClient.serverFirstMessage(ByteBufferUtils.decode(message.getData()));
128-
129119
return new SASLResponse(ByteBufferUtils.encode(this.scramClient.clientFinalMessage().toString()));
130120
} catch (ScramException e) {
131121
throw Exceptions.propagate(e);

src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryUnitTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import java.util.Collections;
3838

39+
import static com.ongres.scram.common.StringPreparation.POSTGRESQL_PREPARATION;
3940
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
4041
import static org.assertj.core.api.Assertions.assertThat;
4142
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -85,6 +86,7 @@ void createAuthenticationSASL() {
8586
.advertisedMechanisms(Collections.singletonList("SCRAM-SHA-256"))
8687
.username("test-username")
8788
.password("test-password".toCharArray())
89+
.stringPreparation(POSTGRESQL_PREPARATION)
8890
.build();
8991

9092
// @formatter:off
@@ -103,6 +105,12 @@ void createAuthenticationSASL() {
103105
.username("test-username")
104106
.password("test-password")
105107
.build();
108+
109+
new PostgresqlConnectionFactory(testClientFactory(client, configuration), configuration)
110+
.create()
111+
.as(StepVerifier::create)
112+
.expectNextCount(1)
113+
.verifyComplete();
106114
}
107115

108116
@Test

0 commit comments

Comments
 (0)