22
33import com .ongres .scram .common .StringPreparation ;
44import it .aboutbits .postgresql .crd .role .RoleSpec ;
5+ import lombok .extern .slf4j .Slf4j ;
56import org .jooq .DSLContext ;
67import org .jspecify .annotations .NullMarked ;
78
2021import static it .aboutbits .postgresql .core .infrastructure .persistence .Tables .PG_AUTHID ;
2122
2223@ NullMarked
24+ @ Slf4j
2325public final class PostgreSQLAuthenticationUtil {
2426 private static final String MD5 = "MD5" ;
2527 private static final String SHA_256 = "SHA-256" ;
@@ -89,7 +91,8 @@ private static boolean verifyPostgresScramSha256(String postgresVerifier, String
8991 int iterations ;
9092 try {
9193 iterations = Integer .parseInt (iterationsAndSalt .substring (0 , colonIterationsAndSalt ));
92- } catch (NumberFormatException ex ) {
94+ } catch (NumberFormatException e ) {
95+ log .error ("Invalid iterations format in PostgreSQL verifier: %s" .formatted (postgresVerifier ), e );
9396 return false ;
9497 }
9598 if (iterations <= 0 ) {
@@ -110,7 +113,8 @@ private static boolean verifyPostgresScramSha256(String postgresVerifier, String
110113 try {
111114 salt = Base64 .getDecoder ().decode (saltB64 );
112115 currentStoredKey = Base64 .getDecoder ().decode (storedKeyB64 );
113- } catch (IllegalArgumentException ex ) {
116+ } catch (IllegalArgumentException e ) {
117+ log .error ("Invalid salt or stored key format in PostgreSQL verifier: %s" .formatted (postgresVerifier ), e );
114118 return false ;
115119 }
116120
@@ -119,7 +123,7 @@ private static boolean verifyPostgresScramSha256(String postgresVerifier, String
119123 byte [] expectedStoredKey = null ;
120124 try {
121125 // RFC 5802/7677:
122- // saltedPassword := Hi(password, salt, iterations) (PBKDF2-HMAC-SHA-256, 32 bytes)
126+ // saltedPassword := Hi(password, salt, iterations) (PBKDF2-HMAC-SHA-256, 32 bytes)
123127 // clientKey := HMAC(saltedPassword, "Client Key")
124128 // storedKey := H(clientKey) (SHA-256)
125129 saltedPassword = pbkdf2HmacSha256 (preparedPassword , salt , iterations , 32 );
@@ -160,7 +164,8 @@ private static boolean verifyPostgresMd5(
160164 3 ,
161165 postgresMd5 .length ()
162166 );
163- } catch (IllegalArgumentException ex ) {
167+ } catch (IllegalArgumentException e ) {
168+ log .error ("Invalid MD5 format in PostgreSQL verifier: %s" .formatted (postgresMd5 ), e );
164169 return false ; // not valid hex
165170 }
166171
0 commit comments