* Conditionally set basic authentication on a server request object. * * @method _auth * @private * @param {Object} request A superagent request object * @param {Object} options A WPRequest _options object * @param {Boolean} forceAuthentication whether to force authentication on the request *
( request, options, forceAuthentication )
| 44 | * @param {Object} A superagent request object, conditionally configured to use basic auth |
| 45 | */ |
| 46 | function _auth( request, options, forceAuthentication ) { |
| 47 | // If we're not supposed to authenticate, don't even start |
| 48 | if ( ! forceAuthentication && ! options.auth && ! options.nonce ) { |
| 49 | return request; |
| 50 | } |
| 51 | |
| 52 | // Enable nonce in options for Cookie authentication http://wp-api.org/guides/authentication.html |
| 53 | if ( options.nonce ) { |
| 54 | request.set( 'X-WP-Nonce', options.nonce ); |
| 55 | return request; |
| 56 | } |
| 57 | |
| 58 | // Retrieve the username & password from the request options if they weren't provided |
| 59 | const username = options.username; |
| 60 | const password = options.password; |
| 61 | |
| 62 | // If no username or no password, can't authenticate |
| 63 | if ( ! username || ! password ) { |
| 64 | return request; |
| 65 | } |
| 66 | |
| 67 | // Can authenticate: set basic auth parameters on the request |
| 68 | return request.auth( username, password ); |
| 69 | } |
| 70 | |
| 71 | // Pagination-Related Helpers |
| 72 | // ========================== |
no outgoing calls
no test coverage detected