11package  io .r2dbc .postgresql .authentication ;
22
33import  com .ongres .scram .client .ScramClient ;
4- import  com .ongres .scram .common .StringPreparation ;
54import  com .ongres .scram .common .exception .ScramException ;
65import  com .ongres .scram .common .util .TlsServerEndpoint ;
76import  io .r2dbc .postgresql .client .ConnectionContext ;
2524import  java .security .cert .CertificateException ;
2625import  java .security .cert .X509Certificate ;
2726
27+ import  static  com .ongres .scram .common .StringPreparation .POSTGRESQL_PREPARATION ;
28+ import  static  com .ongres .scram .common .util .TlsServerEndpoint .TLS_SERVER_END_POINT ;
29+ 
2830public  class  SASLAuthenticationHandler  implements  AuthenticationHandler  {
2931
3032    private  static  final  Logger  LOG  = Loggers .getLogger (SASLAuthenticationHandler .class );
@@ -82,22 +84,16 @@ public FrontendMessage handle(AuthenticationMessage message) {
8284    }
8385
8486    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- 
9187        ScramClient .FinalBuildStage  builder  = ScramClient .builder ()
9288            .advertisedMechanisms (message .getAuthenticationMechanisms ())
9389            .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 );
9692
9793        SSLSession  sslSession  = this .context .getSslSession ();
9894
9995        if  (sslSession  != null  && sslSession .isValid ()) {
100-             builder .channelBinding (TlsServerEndpoint . TLS_SERVER_END_POINT , extractSslEndpoint (sslSession ));
96+             builder .channelBinding (TLS_SERVER_END_POINT , extractSslEndpoint (sslSession ));
10197        }
10298
10399        this .scramClient  = builder .build ();
@@ -107,14 +103,9 @@ private FrontendMessage handleAuthenticationSASL(AuthenticationSASL message) {
107103
108104    private  static  byte [] extractSslEndpoint (SSLSession  sslSession ) {
109105        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 ]);
118109            }
119110        } catch  (CertificateException  | SSLException  e ) {
120111            LOG .debug ("Cannot extract X509Certificate from SSL session" , e );
@@ -125,7 +116,6 @@ private static byte[] extractSslEndpoint(SSLSession sslSession) {
125116    private  FrontendMessage  handleAuthenticationSASLContinue (AuthenticationSASLContinue  message ) {
126117        try  {
127118            this .scramClient .serverFirstMessage (ByteBufferUtils .decode (message .getData ()));
128- 
129119            return  new  SASLResponse (ByteBufferUtils .encode (this .scramClient .clientFinalMessage ().toString ()));
130120        } catch  (ScramException  e ) {
131121            throw  Exceptions .propagate (e );
0 commit comments