Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept multiple headers through OnAuthHeaderRequestedListener #2080

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.wordpress.mobile.WPAndroidGlue;

import android.text.TextUtils;

import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnAuthHeaderRequestedListener;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpHeaderInterceptor implements Interceptor {
private static final String AUTHORIZATION_HEADER_KEY = "Authorization";

private OnAuthHeaderRequestedListener mOnAuthHeaderRequestedListener;

void setOnAuthHeaderRequestedListener(OnAuthHeaderRequestedListener onAuthHeaderRequestedListener) {
Expand All @@ -23,10 +20,13 @@ void setOnAuthHeaderRequestedListener(OnAuthHeaderRequestedListener onAuthHeader
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();

String authHeader = mOnAuthHeaderRequestedListener != null
Map<String, String> authHeaders = mOnAuthHeaderRequestedListener != null
? mOnAuthHeaderRequestedListener.onAuthHeaderRequested(chain.request().url().toString()) : null;
if (!TextUtils.isEmpty(authHeader)) {
builder.addHeader(AUTHORIZATION_HEADER_KEY, authHeader);

if (authHeaders != null) {
for (Map.Entry<String, String> entry : authHeaders.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
}

return chain.proceed(builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public interface OnEditorMountListener {
}

public interface OnAuthHeaderRequestedListener {
String onAuthHeaderRequested(String url);
Map<String, String> onAuthHeaderRequested(String url);
}

public interface OnEditorAutosaveListener {
Expand Down