Make WordPress Core

Changeset 41553


Ignore:
Timestamp:
09/21/2017 12:57:16 PM (9 years ago)
Author:
adamsilverstein
Message:

REST API JS Client: Improve nonce handling, refresh stale nonce on sync.

Keep the nonce used for cookie based authentication fresh by pulling in and using any new nonce supplied in the response headers.

  • Enable passing nonce to init so each api/endpoint can use a unique nonce.
  • Store nonce for endpoint on endpointModel.
  • New model helper nonce() retrieves a model's routeModel nonce.
  • When a response header contains a nonce that doesn't match the stored nonce, replace it.

Fixes #40422.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r41351 r41553  
    794794                                }
    795795
    796                                 if ( ! _.isUndefined( wpApiSettings.nonce ) && ! _.isNull( wpApiSettings.nonce ) ) {
     796                                if ( _.isFunction( model.nonce ) && ! _.isUndefined( model.nonce() ) && ! _.isNull( model.nonce() ) ) {
    797797                                        beforeSend = options.beforeSend;
    798798
     
    800800                                        // options.dataType = 'jsonp';
    801801
     802                                        // Include the nonce with requests.
    802803                                        options.beforeSend = function( xhr ) {
    803                                                 xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
     804                                                xhr.setRequestHeader( 'X-WP-Nonce', model.nonce() );
    804805
    805806                                                if ( beforeSend ) {
    806807                                                        return beforeSend.apply( this, arguments );
     808                                                }
     809                                        };
     810
     811                                        // Update the nonce when a new nonce is returned with the response.
     812                                        options.complete = function( xhr ) {
     813                                                var returnedNonce = xhr.getResponseHeader( 'X-WP-Nonce' );
     814
     815                                                if ( returnedNonce && _.isFunction( model.nonce ) && model.nonce() !== returnedNonce ) {
     816                                                        model.endpointModel.set( 'nonce', returnedNonce );
    807817                                                }
    808818                                        };
     
    10491059                        apiRoot: wpApiSettings.root,
    10501060                        versionString: wp.api.versionString,
     1061                        nonce: null,
    10511062                        schema: null,
    10521063                        models: {},
     
    10661077
    10671078                        model.schemaModel = new wp.api.models.Schema( null, {
    1068                                 apiRoot: model.get( 'apiRoot' ),
    1069                                 versionString: model.get( 'versionString' )
     1079                                apiRoot:       model.get( 'apiRoot' ),
     1080                                versionString: model.get( 'versionString' ),
     1081                                nonce:         model.get( 'nonce' )
    10701082                        } );
    10711083
     
    12391251                                                },
    12401252
     1253                                                // Track nonces on the Endpoint 'routeModel'.
     1254                                                nonce: function() {
     1255                                                        return routeModel.get( 'nonce' );
     1256                                                },
     1257
     1258                                                endpointModel: routeModel,
     1259
    12411260                                                // Include a reference to the original route object.
    12421261                                                route: modelRoute,
     
    12851304                                                },
    12861305
     1306                                                // Track nonces at the Endpoint level.
     1307                                                nonce: function() {
     1308                                                        return routeModel.get( 'nonce' );
     1309                                                },
     1310
     1311                                                endpointModel: routeModel,
     1312
    12871313                                                // Include a reference to the original route object.
    12881314                                                route: modelRoute,
     
    14061432
    14071433                args                      = args || {};
     1434                attributes.nonce          = args.nonce || wpApiSettings.nonce || '';
    14081435                attributes.apiRoot        = args.apiRoot || wpApiSettings.root || '/wp-json';
    14091436                attributes.versionString  = args.versionString || wpApiSettings.versionString || 'wp/v2/';
Note: See TracChangeset for help on using the changeset viewer.