Skip to content

Commit

Permalink
Upgrade to new scram-client 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
reneleonhardt committed May 26, 2024
1 parent 502b565 commit 103c7a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
<reactor.version>2023.0.6</reactor.version>
<scram-client.version>3.0</scram-client.version>
<slf4j.version>2.0.13</slf4j.version>
<spring-framework.version>6.1.8</spring-framework.version>
<testcontainers.version>1.19.8</testcontainers.version>
Expand Down Expand Up @@ -147,7 +148,7 @@
</dependency>
<dependency>
<groupId>com.ongres.scram</groupId>
<artifactId>client</artifactId>
<artifactId>scram-client</artifactId>
<version>${scram-client.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.r2dbc.postgresql.authentication;

import com.ongres.scram.client.ScramClient;
import com.ongres.scram.client.ScramSession;
import com.ongres.scram.common.exception.ScramInvalidServerSignatureException;
import com.ongres.scram.common.exception.ScramParseException;
import com.ongres.scram.common.exception.ScramServerErrorException;
Expand All @@ -17,18 +16,15 @@
import reactor.core.Exceptions;
import reactor.util.annotation.Nullable;

import static com.ongres.scram.client.ScramClient.ChannelBinding.NO;
import static com.ongres.scram.common.stringprep.StringPreparations.NO_PREPARATION;
import static com.ongres.scram.common.StringPreparation.NO_PREPARATION;

public class SASLAuthenticationHandler implements AuthenticationHandler {

private final CharSequence password;

private final String username;

private ScramSession.ClientFinalProcessor clientFinalProcessor;

private ScramSession scramSession;
private ScramClient scramClient;

/**
* Create a new handler.
Expand Down Expand Up @@ -73,24 +69,20 @@ public FrontendMessage handle(AuthenticationMessage message) {
}

private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
ScramClient scramClient = ScramClient
.channelBinding(NO)
scramClient = ScramClient.builder()
.advertisedMechanisms(message.getAuthenticationMechanisms())
.username(this.username)
.password(this.password.toString().toCharArray())
.stringPreparation(NO_PREPARATION)
.selectMechanismBasedOnServerAdvertised(message.getAuthenticationMechanisms().toArray(new String[0]))
.setup();

this.scramSession = scramClient.scramSession(this.username);
.build();

return new SASLInitialResponse(ByteBufferUtils.encode(this.scramSession.clientFirstMessage()), scramClient.getScramMechanism().getName());
return new SASLInitialResponse(ByteBufferUtils.encode(scramClient.clientFirstMessage().toString()), scramClient.getScramMechanism().getName());
}

private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLContinue message) {
try {
this.clientFinalProcessor = this.scramSession
.receiveServerFirstMessage(ByteBufferUtils.decode(message.getData()))
.clientFinalProcessor(this.password.toString());

return new SASLResponse(ByteBufferUtils.encode(clientFinalProcessor.clientFinalMessage()));
scramClient.serverFirstMessage(ByteBufferUtils.decode(message.getData()));
return new SASLResponse(ByteBufferUtils.encode(scramClient.clientFinalMessage().toString()));
} catch (ScramParseException e) {
throw Exceptions.propagate(e);
}
Expand All @@ -99,7 +91,7 @@ private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLConti
@Nullable
private FrontendMessage handleAuthenticationSASLFinal(AuthenticationSASLFinal message) {
try {
this.clientFinalProcessor.receiveServerFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
scramClient.serverFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
return null;
} catch (ScramParseException | ScramInvalidServerSignatureException | ScramServerErrorException e) {
throw Exceptions.propagate(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

import java.util.Collections;

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

@Test
void createAuthenticationSASL() {
ScramClient scramClient = ScramClient
.channelBinding(NO)
ScramClient scramClient = ScramClient.builder()
.advertisedMechanisms(Collections.singletonList("SCRAM-SHA-256"))
.username("test-username")
.password("test-password".toCharArray())
.stringPreparation(NO_PREPARATION)
.selectMechanismBasedOnServerAdvertised("SCRAM-SHA-256")
.setup();
.build();

// @formatter:off
Client client = TestClient.builder()
.window()
.expectRequest(new StartupMessage( "test-database", "test-username", new TestStartupParameterProvider())).thenRespond(new AuthenticationSASL(Collections.singletonList("SCRAM-SHA-256")))
.expectRequest(new SASLInitialResponse(ByteBufferUtils.encode(scramClient.scramSession("test-username").clientFirstMessage()), "SCRAM-SHA-256")).thenRespond(AuthenticationOk.INSTANCE)
.expectRequest(new SASLInitialResponse(ByteBufferUtils.encode(scramClient.clientFirstMessage().toString()), "SCRAM-SHA-256")).thenRespond(AuthenticationOk.INSTANCE)
.done()
.build();
// @formatter:on
Expand All @@ -104,6 +104,12 @@ void createAuthenticationSASL() {
.username("test-username")
.password("test-password")
.build();

new PostgresqlConnectionFactory(testClientFactory(client, configuration), configuration)
.create()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}

@Test
Expand Down

0 comments on commit 103c7a9

Please sign in to comment.