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

feat: Authenticate private resource requests #21331

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
Expand Up @@ -2444,6 +2444,7 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
"postType" to postType,
"postTitle" to editPostRepository.getPost()?.title,
"postContent" to editPostRepository.getPost()?.content,
"siteURL" to site.url,
"siteApiRoot" to siteApiRoot,
"authHeader" to authHeader,
"siteApiNamespace" to siteApiNamespace,
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ google-play-services-auth = '20.4.1'
google-services = '4.4.2'
gravatar = '1.0.0'
greenrobot-eventbus = '3.3.1'
gutenberg-kit = 'trunk-a58a46f3fbb892f311b562e3c122d7ef4ebbfe33'
gutenberg-kit = '34-b2ae11f9c3d2d8d4ab7770ec6a12bd77d5e1263f'
gutenberg-mobile = 'v1.121.0'
indexos-media-for-mobile = '43a9026f0973a2f0a74fa813132f6a16f7499c3a'
jackson-databind = '2.12.7.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.wordpress.android.editor.gutenberg;

import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipData;
Expand All @@ -21,6 +26,9 @@
import android.view.inputmethod.InputMethodManager;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -68,6 +76,7 @@
import org.wordpress.android.util.helpers.MediaFile;
import org.wordpress.android.util.helpers.MediaGallery;
import org.wordpress.aztec.IHistoryListener;
import org.wordpress.gutenberg.GutenbergRequestInterceptor;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent.LogExceptionCallback;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergEmbedWebViewActivity;
import org.wordpress.mobile.WPAndroidGlue.GutenbergJsException;
Expand Down Expand Up @@ -97,6 +106,7 @@
import org.wordpress.gutenberg.GutenbergView.ContentChangeListener;
import org.wordpress.gutenberg.GutenbergWebViewPool;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -107,6 +117,8 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.wordpress.mobile.WPAndroidGlue.Media.createRNMediaUsingMimeType;
Expand All @@ -117,7 +129,8 @@
EditorThemeUpdateListener,
GutenbergDialogPositiveClickInterface,
GutenbergDialogNegativeClickInterface,
GutenbergNetworkConnectionListener {
GutenbergNetworkConnectionListener,
GutenbergRequestInterceptor {
@Nullable private GutenbergView mGutenbergView;
private static final String GUTENBERG_EDITOR_NAME = "gutenberg";
private static final String KEY_HTML_MODE_ENABLED = "KEY_HTML_MODE_ENABLED";
Expand Down Expand Up @@ -181,6 +194,7 @@

private ProgressDialog mSavingContentProgressDialog;
@Nullable private static Map<String, Object> mSettings;
OkHttpClient mHttpClient = new OkHttpClient();
Fixed Show fixed Hide fixed

public static GutenbergEditorFragment newInstance(Context context,
boolean isNewPost,
Expand Down Expand Up @@ -291,6 +305,7 @@
mGutenbergView.setEditorDidBecomeAvailable(view -> {
mEditorFragmentListener.onEditorFragmentContentReady(new ArrayList<Object>(), false);
});
mGutenbergView.setRequestInterceptor(this);

Integer postId = (Integer) mSettings.get("postId");
if (postId != null && postId == 0) {
Expand Down Expand Up @@ -1635,4 +1650,47 @@
mEditorFragmentListener.onMediaRetryAll(mFailedMediaIds);
}
}

@Nullable @Override
public WebResourceResponse modifyRequest(@NonNull WebView view, @NonNull WebResourceRequest request) {
Uri url = request.getUrl();
String siteURL = (String) (mSettings != null ? mSettings.get("siteURL") : "");
String regex = siteURL + "/.*\\.(jpg|jpeg|png|gif|bmp|webp|mp4|mov|avi|mkv|mp3|wav|flac)(\\?.*)?$";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url.toString());

if (siteURL == null || !matcher.matches()) {
return null;
}

try {
Request okHttpRequest = new Request.Builder()
.url(url.toString())
.headers(Headers.of(request.getRequestHeaders()))
.addHeader("Authorization", mSettings.get("authHeader").toString())
.build();

Response response = mHttpClient.newCall(okHttpRequest).execute();

ResponseBody body = response.body();
if (body == null) {
return null;
}

okhttp3.MediaType contentType = body.contentType();
if (contentType == null) {
return null;
}

return new WebResourceResponse(
contentType.toString(),
response.header("content-encoding"),
body.byteStream()
);
} catch (IOException e) {
// We don't need to handle this ourselves, just tell the WebView that
// we weren't able to fetch the resource
return null;
}
}
}
Loading