Skip to content

Commit 81a3b72

Browse files
committed
Upgrade to new scram-client 3.0
1 parent 444da0e commit 81a3b72

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
4949
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
5050
<reactor.version>2023.0.6</reactor.version>
51+
<scram-client.version>3.0</scram-client.version>
5152
<slf4j.version>2.0.13</slf4j.version>
5253
<spring-framework.version>6.1.8</spring-framework.version>
5354
<testcontainers.version>1.19.8</testcontainers.version>
@@ -147,7 +148,7 @@
147148
</dependency>
148149
<dependency>
149150
<groupId>com.ongres.scram</groupId>
150-
<artifactId>client</artifactId>
151+
<artifactId>scram-client</artifactId>
151152
<version>${scram-client.version}</version>
152153
</dependency>
153154
<dependency>

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

+11-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.client.ScramSession;
54
import com.ongres.scram.common.exception.ScramInvalidServerSignatureException;
65
import com.ongres.scram.common.exception.ScramParseException;
76
import com.ongres.scram.common.exception.ScramServerErrorException;
@@ -17,18 +16,15 @@
1716
import reactor.core.Exceptions;
1817
import reactor.util.annotation.Nullable;
1918

20-
import static com.ongres.scram.client.ScramClient.ChannelBinding.NO;
21-
import static com.ongres.scram.common.stringprep.StringPreparations.NO_PREPARATION;
19+
import static com.ongres.scram.common.StringPreparation.NO_PREPARATION;
2220

2321
public class SASLAuthenticationHandler implements AuthenticationHandler {
2422

2523
private final CharSequence password;
2624

2725
private final String username;
2826

29-
private ScramSession.ClientFinalProcessor clientFinalProcessor;
30-
31-
private ScramSession scramSession;
27+
private ScramClient scramClient;
3228

3329
/**
3430
* Create a new handler.
@@ -73,24 +69,20 @@ public FrontendMessage handle(AuthenticationMessage message) {
7369
}
7470

7571
private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
76-
ScramClient scramClient = ScramClient
77-
.channelBinding(NO)
72+
scramClient = ScramClient.builder()
73+
.advertisedMechanisms(message.getAuthenticationMechanisms())
74+
.username(this.username)
75+
.password(this.password.toString().toCharArray())
7876
.stringPreparation(NO_PREPARATION)
79-
.selectMechanismBasedOnServerAdvertised(message.getAuthenticationMechanisms().toArray(new String[0]))
80-
.setup();
81-
82-
this.scramSession = scramClient.scramSession(this.username);
77+
.build();
8378

84-
return new SASLInitialResponse(ByteBufferUtils.encode(this.scramSession.clientFirstMessage()), scramClient.getScramMechanism().getName());
79+
return new SASLInitialResponse(ByteBufferUtils.encode(scramClient.clientFirstMessage().toString()), scramClient.getScramMechanism().getName());
8580
}
8681

8782
private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLContinue message) {
8883
try {
89-
this.clientFinalProcessor = this.scramSession
90-
.receiveServerFirstMessage(ByteBufferUtils.decode(message.getData()))
91-
.clientFinalProcessor(this.password.toString());
92-
93-
return new SASLResponse(ByteBufferUtils.encode(clientFinalProcessor.clientFinalMessage()));
84+
scramClient.serverFirstMessage(ByteBufferUtils.decode(message.getData()));
85+
return new SASLResponse(ByteBufferUtils.encode(scramClient.clientFinalMessage().toString()));
9486
} catch (ScramParseException e) {
9587
throw Exceptions.propagate(e);
9688
}
@@ -99,7 +91,7 @@ private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLConti
9991
@Nullable
10092
private FrontendMessage handleAuthenticationSASLFinal(AuthenticationSASLFinal message) {
10193
try {
102-
this.clientFinalProcessor.receiveServerFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
94+
scramClient.serverFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
10395
return null;
10496
} catch (ScramParseException | ScramInvalidServerSignatureException | ScramServerErrorException e) {
10597
throw Exceptions.propagate(e);

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535

3636
import java.util.Collections;
3737

38-
import static com.ongres.scram.client.ScramClient.ChannelBinding.NO;
39-
import static com.ongres.scram.common.stringprep.StringPreparations.NO_PREPARATION;
38+
import static com.ongres.scram.common.StringPreparation.NO_PREPARATION;
4039
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
4140
import static org.assertj.core.api.Assertions.assertThat;
4241
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -82,17 +81,18 @@ void createAuthenticationMD5Password() {
8281

8382
@Test
8483
void createAuthenticationSASL() {
85-
ScramClient scramClient = ScramClient
86-
.channelBinding(NO)
84+
ScramClient scramClient = ScramClient.builder()
85+
.advertisedMechanisms(Collections.singletonList("SCRAM-SHA-256"))
86+
.username("test-username")
87+
.password("test-password".toCharArray())
8788
.stringPreparation(NO_PREPARATION)
88-
.selectMechanismBasedOnServerAdvertised("SCRAM-SHA-256")
89-
.setup();
89+
.build();
9090

9191
// @formatter:off
9292
Client client = TestClient.builder()
9393
.window()
9494
.expectRequest(new StartupMessage( "test-database", "test-username", new TestStartupParameterProvider())).thenRespond(new AuthenticationSASL(Collections.singletonList("SCRAM-SHA-256")))
95-
.expectRequest(new SASLInitialResponse(ByteBufferUtils.encode(scramClient.scramSession("test-username").clientFirstMessage()), "SCRAM-SHA-256")).thenRespond(AuthenticationOk.INSTANCE)
95+
.expectRequest(new SASLInitialResponse(ByteBufferUtils.encode(scramClient.clientFirstMessage().toString()), "SCRAM-SHA-256")).thenRespond(AuthenticationOk.INSTANCE)
9696
.done()
9797
.build();
9898
// @formatter:on
@@ -104,6 +104,12 @@ void createAuthenticationSASL() {
104104
.username("test-username")
105105
.password("test-password")
106106
.build();
107+
108+
new PostgresqlConnectionFactory(testClientFactory(client, configuration), configuration)
109+
.create()
110+
.as(StepVerifier::create)
111+
.expectNextCount(1)
112+
.verifyComplete();
107113
}
108114

109115
@Test

0 commit comments

Comments
 (0)