Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Fix BasicAuth header not getting sent when SG GUEST is enabled
Browse files Browse the repository at this point in the history
Sent the Basic Auth header without wating for challenge.

#1766
  • Loading branch information
pasin committed Oct 22, 2018
1 parent 7a55719 commit b72e27e
Showing 1 changed file with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,56 +252,12 @@ private OkHttpClient setupOkHttpClient() throws GeneralSecurityException {
// redirection
builder.followRedirects(true).followSslRedirects(true);

// authenticator
Authenticator authenticator = setupAuthenticator();
if (authenticator != null)
builder.authenticator(authenticator);

// trusted certificate (pinned certificate)
setupTrustedCertificate(builder);

return builder.build();
}

private Authenticator setupAuthenticator() {
if (options != null && options.containsKey(kC4ReplicatorOptionAuthentication)) {
Map<String, Object> auth = (Map<String, Object>) options.get(kC4ReplicatorOptionAuthentication);
if (auth != null) {
final String username = (String) auth.get(kC4ReplicatorAuthUserName);
final String password = (String) auth.get(kC4ReplicatorAuthPassword);
if (username != null && password != null) {
return new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
// http://www.ietf.org/rfc/rfc2617.txt
Log.v(TAG, "Authenticating for response: " + response);
// If failed 3 times, give up.
if (responseCount(response) >= 3)
return null;

List<Challenge> challenges = response.challenges();
Log.v(TAG, "Challenges: " + challenges);
if (challenges != null) {
for (Challenge challenge : challenges) {
if (challenge.scheme().equals("Basic")) {
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Authorization", credential).build();
}
// NOTE: Not implemented Digest authentication
// https://github.com/rburgst/okhttp-digest
//else if(challenge.scheme().equals("Digest")){
//}
}
}
return null;
}
};
}
}
}
return null;
}

private void setupTrustedCertificate(OkHttpClient.Builder builder) throws GeneralSecurityException {
if (options != null && options.containsKey(kC4ReplicatorOptionPinnedServerCert)) {
byte[] pin = (byte[]) options.get(kC4ReplicatorOptionPinnedServerCert);
Expand Down Expand Up @@ -357,6 +313,9 @@ private Request newRequest() {
String cookieString = (String) options.get(kC4ReplicatorOptionCookies);
if (cookieString != null)
builder.addHeader("Cookie", cookieString);

// Basic Auth:
setupAuthHeader(builder);
}

// Configure WebSocket related headers:
Expand All @@ -368,6 +327,19 @@ private Request newRequest() {
return builder.build();
}

private void setupAuthHeader(Request.Builder builder) {
if (options != null && options.containsKey(kC4ReplicatorOptionAuthentication)) {
Map<String, Object> auth = (Map<String, Object>) options.get(kC4ReplicatorOptionAuthentication);
final String type = (String) auth.get(kC4ReplicatorAuthType);
final String username = (String) auth.get(kC4ReplicatorAuthUserName);
final String password = (String) auth.get(kC4ReplicatorAuthPassword);
if (kC4AuthTypeBasic.equals(type) && username != null && password != null) {
String credential = Credentials.basic(username, password);
builder.header("Authorization", credential);
}
}
}

private void receivedHTTPResponse(Response response) {
int httpStatus = response.code();
Log.v(TAG, "receivedHTTPResponse() httpStatus -> " + httpStatus);
Expand Down

0 comments on commit b72e27e

Please sign in to comment.