1
1
package io .r2dbc .postgresql .authentication ;
2
2
3
3
import com .ongres .scram .client .ScramClient ;
4
- import com .ongres .scram .client .ScramSession ;
5
4
import com .ongres .scram .common .exception .ScramInvalidServerSignatureException ;
6
5
import com .ongres .scram .common .exception .ScramParseException ;
7
6
import com .ongres .scram .common .exception .ScramServerErrorException ;
17
16
import reactor .core .Exceptions ;
18
17
import reactor .util .annotation .Nullable ;
19
18
20
- import static com .ongres .scram .client .ScramClient .ChannelBinding .NO ;
21
- import static com .ongres .scram .common .stringprep .StringPreparations .NO_PREPARATION ;
19
+ import static com .ongres .scram .common .StringPreparation .NO_PREPARATION ;
22
20
23
21
public class SASLAuthenticationHandler implements AuthenticationHandler {
24
22
25
23
private final CharSequence password ;
26
24
27
25
private final String username ;
28
26
29
- private ScramSession .ClientFinalProcessor clientFinalProcessor ;
30
-
31
- private ScramSession scramSession ;
27
+ private ScramClient scramClient ;
32
28
33
29
/**
34
30
* Create a new handler.
@@ -73,24 +69,20 @@ public FrontendMessage handle(AuthenticationMessage message) {
73
69
}
74
70
75
71
private FrontendMessage handleAuthenticationSASL (AuthenticationSASL message ) {
76
- ScramClient scramClient = ScramClient
77
- .channelBinding (NO )
72
+ scramClient = ScramClient .builder ()
73
+ .advertisedMechanisms (message .getAuthenticationMechanisms ())
74
+ .username (this .username )
75
+ .password (this .password .toString ().toCharArray ())
78
76
.stringPreparation (NO_PREPARATION )
79
- .selectMechanismBasedOnServerAdvertised (message .getAuthenticationMechanisms ().toArray (new String [0 ]))
80
- .setup ();
81
-
82
- this .scramSession = scramClient .scramSession (this .username );
77
+ .build ();
83
78
84
- return new SASLInitialResponse (ByteBufferUtils .encode (this . scramSession . clientFirstMessage ()), scramClient .getScramMechanism ().getName ());
79
+ return new SASLInitialResponse (ByteBufferUtils .encode (scramClient . clientFirstMessage (). toString ()), scramClient .getScramMechanism ().getName ());
85
80
}
86
81
87
82
private FrontendMessage handleAuthenticationSASLContinue (AuthenticationSASLContinue message ) {
88
83
try {
89
- this .clientFinalProcessor = this .scramSession
90
- .receiveServerFirstMessage (ByteBufferUtils .decode (message .getData ()))
91
- .clientFinalProcessor (this .password .toString ());
92
-
93
- return new SASLResponse (ByteBufferUtils .encode (clientFinalProcessor .clientFinalMessage ()));
84
+ scramClient .serverFirstMessage (ByteBufferUtils .decode (message .getData ()));
85
+ return new SASLResponse (ByteBufferUtils .encode (scramClient .clientFinalMessage ().toString ()));
94
86
} catch (ScramParseException e ) {
95
87
throw Exceptions .propagate (e );
96
88
}
@@ -99,7 +91,7 @@ private FrontendMessage handleAuthenticationSASLContinue(AuthenticationSASLConti
99
91
@ Nullable
100
92
private FrontendMessage handleAuthenticationSASLFinal (AuthenticationSASLFinal message ) {
101
93
try {
102
- this . clientFinalProcessor . receiveServerFinalMessage (ByteBufferUtils .decode (message .getAdditionalData ()));
94
+ scramClient . serverFinalMessage (ByteBufferUtils .decode (message .getAdditionalData ()));
103
95
return null ;
104
96
} catch (ScramParseException | ScramInvalidServerSignatureException | ScramServerErrorException e ) {
105
97
throw Exceptions .propagate (e );
0 commit comments