1
1
package io .r2dbc .postgresql .authentication ;
2
2
3
3
import com .ongres .scram .client .ScramClient ;
4
- import com .ongres .scram .common .StringPreparation ;
5
4
import com .ongres .scram .common .exception .ScramException ;
6
5
import com .ongres .scram .common .util .TlsServerEndpoint ;
7
6
import io .r2dbc .postgresql .client .ConnectionContext ;
25
24
import java .security .cert .CertificateException ;
26
25
import java .security .cert .X509Certificate ;
27
26
27
+ import static com .ongres .scram .common .StringPreparation .POSTGRESQL_PREPARATION ;
28
+ import static com .ongres .scram .common .util .TlsServerEndpoint .TLS_SERVER_END_POINT ;
29
+
28
30
public class SASLAuthenticationHandler implements AuthenticationHandler {
29
31
30
32
private static final Logger LOG = Loggers .getLogger (SASLAuthenticationHandler .class );
@@ -82,22 +84,16 @@ public FrontendMessage handle(AuthenticationMessage message) {
82
84
}
83
85
84
86
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
-
91
87
ScramClient .FinalBuildStage builder = ScramClient .builder ()
92
88
.advertisedMechanisms (message .getAuthenticationMechanisms ())
93
89
.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 );
96
92
97
93
SSLSession sslSession = this .context .getSslSession ();
98
94
99
95
if (sslSession != null && sslSession .isValid ()) {
100
- builder .channelBinding (TlsServerEndpoint . TLS_SERVER_END_POINT , extractSslEndpoint (sslSession ));
96
+ builder .channelBinding (TLS_SERVER_END_POINT , extractSslEndpoint (sslSession ));
101
97
}
102
98
103
99
this .scramClient = builder .build ();
@@ -107,14 +103,9 @@ private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
107
103
108
104
private static byte [] extractSslEndpoint (SSLSession sslSession ) {
109
105
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 ]);
118
109
}
119
110
} catch (CertificateException | SSLException e ) {
120
111
LOG .debug ("Cannot extract X509Certificate from SSL session" , e );
@@ -125,7 +116,6 @@ private static byte[] extractSslEndpoint(SSLSession sslSession) {
125
116
private FrontendMessage handleAuthenticationSASLContinue (AuthenticationSASLContinue message ) {
126
117
try {
127
118
this .scramClient .serverFirstMessage (ByteBufferUtils .decode (message .getData ()));
128
-
129
119
return new SASLResponse (ByteBufferUtils .encode (this .scramClient .clientFinalMessage ().toString ()));
130
120
} catch (ScramException e ) {
131
121
throw Exceptions .propagate (e );
0 commit comments