Skip to content

Commit 26761e8

Browse files
jorsolmp911de
authored andcommitted
Update SCRAM dependency to 3.0
Signed-off-by: Jorge Solórzano <[email protected]> [resolves #645][resolves #646]
1 parent 2d9a921 commit 26761e8

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +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>2022.0.16</reactor.version>
51-
<scram-client.version>2.1</scram-client.version>
51+
<scram-client.version>3.0</scram-client.version>
5252
<spring-framework.version>5.3.32</spring-framework.version>
5353
<testcontainers.version>1.19.5</testcontainers.version>
5454
<jts-core.version>1.19.0</jts-core.version>
@@ -127,7 +127,7 @@
127127
</dependency>
128128
<dependency>
129129
<groupId>com.ongres.scram</groupId>
130-
<artifactId>client</artifactId>
130+
<artifactId>scram-client</artifactId>
131131
<version>${scram-client.version}</version>
132132
</dependency>
133133
<dependency>

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

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package io.r2dbc.postgresql.authentication;
22

33
import com.ongres.scram.client.ScramClient;
4-
import com.ongres.scram.client.ScramSession;
5-
import com.ongres.scram.common.exception.ScramInvalidServerSignatureException;
6-
import com.ongres.scram.common.exception.ScramParseException;
7-
import com.ongres.scram.common.exception.ScramServerErrorException;
4+
import com.ongres.scram.common.StringPreparation;
5+
import com.ongres.scram.common.exception.ScramException;
6+
87
import io.r2dbc.postgresql.message.backend.AuthenticationMessage;
98
import io.r2dbc.postgresql.message.backend.AuthenticationSASL;
109
import io.r2dbc.postgresql.message.backend.AuthenticationSASLContinue;
@@ -17,18 +16,13 @@
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;
22-
2319
public class SASLAuthenticationHandler implements AuthenticationHandler {
2420

2521
private final CharSequence password;
2622

2723
private final String username;
2824

29-
private ScramSession.ClientFinalProcessor clientFinalProcessor;
30-
31-
private ScramSession scramSession;
25+
private ScramClient scramClient;
3226

3327
/**
3428
* Create a new handler.
@@ -73,35 +67,32 @@ public FrontendMessage handle(AuthenticationMessage message) {
7367
}
7468

7569
private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
76-
ScramClient scramClient = ScramClient
77-
.channelBinding(NO)
78-
.stringPreparation(NO_PREPARATION)
79-
.selectMechanismBasedOnServerAdvertised(message.getAuthenticationMechanisms().toArray(new String[0]))
80-
.setup();
81-
82-
this.scramSession = scramClient.scramSession(this.username);
83-
84-
return new SASLInitialResponse(ByteBufferUtils.encode(this.scramSession.clientFirstMessage()), scramClient.getScramMechanism().getName());
70+
this.scramClient = ScramClient.builder()
71+
.advertisedMechanisms(message.getAuthenticationMechanisms())
72+
.username(username) // ignored by the server, use startup message
73+
.password(password.toString().toCharArray())
74+
.stringPreparation(StringPreparation.POSTGRESQL_PREPARATION)
75+
.build();
76+
77+
return new SASLInitialResponse(ByteBufferUtils.encode(this.scramClient.clientFirstMessage().toString()), scramClient.getScramMechanism().getName());
8578
}
8679

8780
private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLContinue message) {
8881
try {
89-
this.clientFinalProcessor = this.scramSession
90-
.receiveServerFirstMessage(ByteBufferUtils.decode(message.getData()))
91-
.clientFinalProcessor(this.password.toString());
82+
this.scramClient.serverFirstMessage(ByteBufferUtils.decode(message.getData()));
9283

93-
return new SASLResponse(ByteBufferUtils.encode(clientFinalProcessor.clientFinalMessage()));
94-
} catch (ScramParseException e) {
84+
return new SASLResponse(ByteBufferUtils.encode(this.scramClient.clientFinalMessage().toString()));
85+
} catch (ScramException e) {
9586
throw Exceptions.propagate(e);
9687
}
9788
}
9889

9990
@Nullable
10091
private FrontendMessage handleAuthenticationSASLFinal(AuthenticationSASLFinal message) {
10192
try {
102-
this.clientFinalProcessor.receiveServerFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
93+
this.scramClient.serverFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
10394
return null;
104-
} catch (ScramParseException | ScramInvalidServerSignatureException | ScramServerErrorException e) {
95+
} catch (ScramException e) {
10596
throw Exceptions.propagate(e);
10697
}
10798
}

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
import io.r2dbc.postgresql.message.frontend.StartupMessage;
3030
import io.r2dbc.postgresql.util.ByteBufferUtils;
3131
import io.r2dbc.spi.R2dbcNonTransientResourceException;
32+
3233
import org.junit.jupiter.api.Test;
3334
import reactor.core.publisher.Mono;
3435
import reactor.test.StepVerifier;
3536

3637
import java.util.Collections;
3738

38-
import static com.ongres.scram.client.ScramClient.ChannelBinding.NO;
39-
import static com.ongres.scram.common.stringprep.StringPreparations.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,17 @@ void createAuthenticationMD5Password() {
8281

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

9190
// @formatter:off
9291
Client client = TestClient.builder()
9392
.window()
9493
.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)
94+
.expectRequest(new SASLInitialResponse(ByteBufferUtils.encode(scramClient.clientFirstMessage().toString()), "SCRAM-SHA-256")).thenRespond(AuthenticationOk.INSTANCE)
9695
.done()
9796
.build();
9897
// @formatter:on

0 commit comments

Comments
 (0)