Skip to content

Commit

Permalink
Update SCRAM dependency to 3.0
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>

[resolves #645][resolves #646]
  • Loading branch information
jorsol authored and mp911de committed Apr 8, 2024
1 parent 2d9a921 commit 26761e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -48,7 +48,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
<reactor.version>2022.0.16</reactor.version>
<scram-client.version>2.1</scram-client.version>
<scram-client.version>3.0</scram-client.version>
<spring-framework.version>5.3.32</spring-framework.version>
<testcontainers.version>1.19.5</testcontainers.version>
<jts-core.version>1.19.0</jts-core.version>
Expand Down Expand Up @@ -127,7 +127,7 @@
</dependency>
<dependency>
<groupId>com.ongres.scram</groupId>
<artifactId>client</artifactId>
<artifactId>scram-client</artifactId>
<version>${scram-client.version}</version>
</dependency>
<dependency>
Expand Down
@@ -1,10 +1,9 @@
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;
import com.ongres.scram.common.StringPreparation;
import com.ongres.scram.common.exception.ScramException;

import io.r2dbc.postgresql.message.backend.AuthenticationMessage;
import io.r2dbc.postgresql.message.backend.AuthenticationSASL;
import io.r2dbc.postgresql.message.backend.AuthenticationSASLContinue;
Expand All @@ -17,18 +16,13 @@
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;

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,35 +67,32 @@ public FrontendMessage handle(AuthenticationMessage message) {
}

private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
ScramClient scramClient = ScramClient
.channelBinding(NO)
.stringPreparation(NO_PREPARATION)
.selectMechanismBasedOnServerAdvertised(message.getAuthenticationMechanisms().toArray(new String[0]))
.setup();

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

return new SASLInitialResponse(ByteBufferUtils.encode(this.scramSession.clientFirstMessage()), scramClient.getScramMechanism().getName());
this.scramClient = ScramClient.builder()
.advertisedMechanisms(message.getAuthenticationMechanisms())
.username(username) // ignored by the server, use startup message
.password(password.toString().toCharArray())
.stringPreparation(StringPreparation.POSTGRESQL_PREPARATION)
.build();

return new SASLInitialResponse(ByteBufferUtils.encode(this.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());
this.scramClient.serverFirstMessage(ByteBufferUtils.decode(message.getData()));

return new SASLResponse(ByteBufferUtils.encode(clientFinalProcessor.clientFinalMessage()));
} catch (ScramParseException e) {
return new SASLResponse(ByteBufferUtils.encode(this.scramClient.clientFinalMessage().toString()));
} catch (ScramException e) {
throw Exceptions.propagate(e);
}
}

@Nullable
private FrontendMessage handleAuthenticationSASLFinal(AuthenticationSASLFinal message) {
try {
this.clientFinalProcessor.receiveServerFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
this.scramClient.serverFinalMessage(ByteBufferUtils.decode(message.getAdditionalData()));
return null;
} catch (ScramParseException | ScramInvalidServerSignatureException | ScramServerErrorException e) {
} catch (ScramException e) {
throw Exceptions.propagate(e);
}
}
Expand Down
Expand Up @@ -29,14 +29,13 @@
import io.r2dbc.postgresql.message.frontend.StartupMessage;
import io.r2dbc.postgresql.util.ByteBufferUtils;
import io.r2dbc.spi.R2dbcNonTransientResourceException;

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

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 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,17 @@ void createAuthenticationMD5Password() {

@Test
void createAuthenticationSASL() {
ScramClient scramClient = ScramClient
.channelBinding(NO)
.stringPreparation(NO_PREPARATION)
.selectMechanismBasedOnServerAdvertised("SCRAM-SHA-256")
.setup();
ScramClient scramClient = ScramClient.builder()
.advertisedMechanisms(Collections.singletonList("SCRAM-SHA-256"))
.username("test-username")
.password("test-password".toCharArray())
.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 Down

0 comments on commit 26761e8

Please sign in to comment.