@@ -252,56 +252,12 @@ private OkHttpClient setupOkHttpClient() throws GeneralSecurityException {
252252 // redirection
253253 builder .followRedirects (true ).followSslRedirects (true );
254254
255- // authenticator
256- Authenticator authenticator = setupAuthenticator ();
257- if (authenticator != null )
258- builder .authenticator (authenticator );
259-
260255 // trusted certificate (pinned certificate)
261256 setupTrustedCertificate (builder );
262257
263258 return builder .build ();
264259 }
265260
266- private Authenticator setupAuthenticator () {
267- if (options != null && options .containsKey (kC4ReplicatorOptionAuthentication )) {
268- Map <String , Object > auth = (Map <String , Object >) options .get (kC4ReplicatorOptionAuthentication );
269- if (auth != null ) {
270- final String username = (String ) auth .get (kC4ReplicatorAuthUserName );
271- final String password = (String ) auth .get (kC4ReplicatorAuthPassword );
272- if (username != null && password != null ) {
273- return new Authenticator () {
274- @ Override
275- public Request authenticate (Route route , Response response ) throws IOException {
276- // http://www.ietf.org/rfc/rfc2617.txt
277- Log .v (TAG , "Authenticating for response: " + response );
278- // If failed 3 times, give up.
279- if (responseCount (response ) >= 3 )
280- return null ;
281-
282- List <Challenge > challenges = response .challenges ();
283- Log .v (TAG , "Challenges: " + challenges );
284- if (challenges != null ) {
285- for (Challenge challenge : challenges ) {
286- if (challenge .scheme ().equals ("Basic" )) {
287- String credential = Credentials .basic (username , password );
288- return response .request ().newBuilder ().header ("Authorization" , credential ).build ();
289- }
290- // NOTE: Not implemented Digest authentication
291- // https://github.com/rburgst/okhttp-digest
292- //else if(challenge.scheme().equals("Digest")){
293- //}
294- }
295- }
296- return null ;
297- }
298- };
299- }
300- }
301- }
302- return null ;
303- }
304-
305261 private void setupTrustedCertificate (OkHttpClient .Builder builder ) throws GeneralSecurityException {
306262 if (options != null && options .containsKey (kC4ReplicatorOptionPinnedServerCert )) {
307263 byte [] pin = (byte []) options .get (kC4ReplicatorOptionPinnedServerCert );
@@ -357,6 +313,9 @@ private Request newRequest() {
357313 String cookieString = (String ) options .get (kC4ReplicatorOptionCookies );
358314 if (cookieString != null )
359315 builder .addHeader ("Cookie" , cookieString );
316+
317+ // Basic Auth:
318+ setupAuthHeader (builder );
360319 }
361320
362321 // Configure WebSocket related headers:
@@ -368,6 +327,19 @@ private Request newRequest() {
368327 return builder .build ();
369328 }
370329
330+ private void setupAuthHeader (Request .Builder builder ) {
331+ if (options != null && options .containsKey (kC4ReplicatorOptionAuthentication )) {
332+ Map <String , Object > auth = (Map <String , Object >) options .get (kC4ReplicatorOptionAuthentication );
333+ final String type = (String ) auth .get (kC4ReplicatorAuthType );
334+ final String username = (String ) auth .get (kC4ReplicatorAuthUserName );
335+ final String password = (String ) auth .get (kC4ReplicatorAuthPassword );
336+ if (kC4AuthTypeBasic .equals (type ) && username != null && password != null ) {
337+ String credential = Credentials .basic (username , password );
338+ builder .header ("Authorization" , credential );
339+ }
340+ }
341+ }
342+
371343 private void receivedHTTPResponse (Response response ) {
372344 int httpStatus = response .code ();
373345 Log .v (TAG , "receivedHTTPResponse() httpStatus -> " + httpStatus );
0 commit comments