-
Notifications
You must be signed in to change notification settings - Fork 3
feat: AJAX uses token authentication #181
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
Open
dcalhoun
wants to merge
31
commits into
trunk
Choose a base branch
from
feat/authorize-ajax-with-application-passwords
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
51d7437
feat: Authorize AJAX with application passwords
dcalhoun c687f50
refactor: Rename AJAX and api-fetch configuration utilities
dcalhoun 1f9d1cc
fix: Configure AJAX after the library loads
dcalhoun e14bbf5
test: Fix test imports and assertions
dcalhoun 5997c2e
fix: Set the global WordPress admin AJAX URL
dcalhoun f12aa24
test: Assert AJAX configuration
dcalhoun 054742e
feat: Allow configuring the Android asset loader domain
dcalhoun 8781146
docs: Note AJAX support requirements
dcalhoun 34932e4
docs: Note AJAX CORS errors in troubleshooting documentation
dcalhoun 0e45e3d
fix: Add type checks before wrapping wp.ajax methods (#282)
dcalhoun 61f2628
feat: conditionally reinstate VideoPress bridge
dcalhoun a63e447
fix: filter lodash-js-after inline script from editor assets
dcalhoun d23adec
feat: vendor and load wp-util.js
dcalhoun 576cf0a
fix: align wp.ajax wrapper signatures with wp-util
dcalhoun 6807e8c
fix: use home URL for iOS demo app site URL
dcalhoun 4c3442c
feat: alias wp.media.ajax and wp.media.post to wp.ajax
dcalhoun 0415441
build: Use wp-util from production WordPress release
dcalhoun c5b6964
docs: Expand inline comments
dcalhoun f5df112
refactor: scope AJAX auth to same-site requests via ajaxPrefilter
dcalhoun d58aed2
fix: strip trailing slash from siteURL before building AJAX URLs
dcalhoun 83fc125
fix: warn instead of logging success when jQuery is unavailable for A…
dcalhoun fcb130f
fix: use origin-based matching for AJAX auth header injection
dcalhoun 4c90007
docs: separate AJAX config examples from Android requirement
dcalhoun 50361f0
refactor: remove redundant AJAX setup from VideoPress bridge
dcalhoun f4abf21
fix: add defensive guards to AJAX configuration
dcalhoun c1af7d3
fix: validate assetLoaderDomain in Android EditorConfiguration
dcalhoun fb76ac7
docs: add vendor README documenting wp-util.js source
dcalhoun 282c499
fix: add https scheme to WP.com site URL in Android demo app
dcalhoun 1035b14
refactor: extract duplicated AJAX URL into local variable
dcalhoun 51d58ee
docs: remove lint/format exclusion note from vendor README
dcalhoun 78bc47e
refactor: derive Android WebView asset domain from site URL
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ android/ | |
| build/ | ||
| dist/ | ||
| ios/ | ||
| vendor/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ android | |
| ios | ||
| package-lock.json | ||
| .github/**/*.md | ||
| vendor | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -425,7 +425,7 @@ class SitePreparationViewModel { | |
|
|
||
| return EditorConfigurationBuilder( | ||
| postType: selectedPostTypeDetails, | ||
| siteURL: URL(string: apiRoot.siteUrlString())!, | ||
| siteURL: URL(string: apiRoot.homeUrlString())!, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensures WP.com sites use TLS for the |
||
| siteApiRoot: siteApiRoot | ||
| ) | ||
| .setShouldUseThemeStyles(canUseEditorStyles) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getGBKit } from './bridge'; | ||
| import { warn, debug } from './logger'; | ||
|
|
||
| /** | ||
| * Configure AJAX for use without authentication cookies. | ||
| * | ||
| * GutenbergKit runs in a WebView without WordPress session cookies, | ||
| * so AJAX requests need explicit URL and token-based authentication. | ||
| * Additionally, WordPress core media globals (`wp.media.ajax`, | ||
| * `wp.media.post`) are normally set by wp-includes/js/media-models.js, | ||
| * which GutenbergKit doesn't load — so we alias them here. | ||
| * | ||
| * @return {void} | ||
| */ | ||
| export function configureAjax() { | ||
| window.wp = window.wp || {}; | ||
| window.wp.ajax = window.wp.ajax || {}; | ||
| window.wp.ajax.settings = window.wp.ajax.settings || {}; | ||
|
|
||
| const { siteURL: rawSiteURL, authHeader } = getGBKit(); | ||
| const siteURL = rawSiteURL?.replace( /\/+$/, '' ); | ||
| configureAjaxUrl( siteURL ); | ||
| configureAjaxAuth( siteURL, authHeader ); | ||
| configureMediaAjax(); | ||
| } | ||
|
|
||
| function configureAjaxUrl( siteURL ) { | ||
| if ( ! siteURL ) { | ||
| warn( 'Unable to configure AJAX URL without siteURL' ); | ||
| return; | ||
| } | ||
|
|
||
| const ajaxUrl = `${ siteURL }/wp-admin/admin-ajax.php`; | ||
| // Global used within WordPress admin pages | ||
| window.ajaxurl = ajaxUrl; | ||
| // Global used by WordPress' JavaScript API | ||
| window.wp.ajax.settings.url = ajaxUrl; | ||
|
|
||
| debug( 'AJAX URL configured' ); | ||
| } | ||
|
|
||
| function configureAjaxAuth( siteURL, authHeader ) { | ||
| if ( ! siteURL ) { | ||
| warn( 'Unable to configure AJAX auth without siteURL' ); | ||
| return; | ||
| } | ||
|
|
||
| if ( ! authHeader ) { | ||
| warn( 'Unable to configure AJAX auth without authHeader' ); | ||
| return; | ||
| } | ||
|
|
||
| if ( ! window.jQuery?.ajaxPrefilter ) { | ||
| warn( 'Unable to configure AJAX auth: jQuery not available' ); | ||
| return; | ||
| } | ||
|
|
||
| let siteOrigin; | ||
| try { | ||
| siteOrigin = new URL( siteURL ).origin; | ||
| } catch { | ||
| warn( 'Unable to configure AJAX auth: invalid siteURL' ); | ||
| return; | ||
| } | ||
|
|
||
| window.jQuery.ajaxPrefilter( function ( options ) { | ||
| if ( ! isSameOrigin( options.url, siteOrigin ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const originalBeforeSend = options.beforeSend; | ||
| options.beforeSend = function ( xhr ) { | ||
| xhr.setRequestHeader( 'Authorization', authHeader ); | ||
| if ( typeof originalBeforeSend === 'function' ) { | ||
| originalBeforeSend( xhr ); | ||
| } | ||
| }; | ||
| } ); | ||
|
|
||
| debug( 'AJAX auth configured' ); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a request URL shares the same origin as the site. | ||
| * | ||
| * Uses `URL.origin` so that scheme, host, and port must all match exactly, | ||
| * preventing credential leakage to lookalike domains (e.g. | ||
| * `https://example.com.evil.com`). | ||
| * | ||
| * @param {string} requestUrl The URL of the outgoing request. | ||
| * @param {string} siteOrigin The origin derived from `siteURL`. | ||
| * @return {boolean} Whether the request targets the same origin. | ||
| */ | ||
| function isSameOrigin( requestUrl, siteOrigin ) { | ||
| try { | ||
| return new URL( requestUrl ).origin === siteOrigin; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Alias `wp.media.ajax` and `wp.media.post` to the (now-authenticated) | ||
| * `wp.ajax.send` and `wp.ajax.post`. WordPress core normally sets these | ||
| * in `wp-includes/js/media-models.js`, which GutenbergKit doesn't load. | ||
| * | ||
| * @see https://github.com/WordPress/wordpress-develop/blob/117af7e/src/js/_enqueues/wp/media/models.js#L134 | ||
| */ | ||
| function configureMediaAjax() { | ||
| if ( ! window.wp.ajax.send || ! window.wp.ajax.post ) { | ||
| warn( | ||
| 'Unable to configure media AJAX: wp.ajax.send/post not available' | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| window.wp.media = window.wp.media || {}; | ||
| window.wp.media.ajax = window.wp.ajax.send; | ||
| window.wp.media.post = window.wp.ajax.post; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
account.usernameis protocol-less, which causes errors as aGBK.siteURLvalue. It is safe to assume TLS for WP.com sites.