Source: classes/Authentications/WordPressBasicAuth.php

  1. <?php
  2. /**
  3. * WP basic auth functionality
  4. *
  5. * @package distributor
  6. */
  7. namespace Distributor\Authentications;
  8. use \Distributor\Authentication as Authentication;
  9. /**
  10. * This auth type is simple username/password WP style
  11. */
  12. class WordPressBasicAuth extends Authentication {
  13. /**
  14. * Auth handler slug
  15. *
  16. * @var string
  17. */
  18. public static $slug = 'user-pass';
  19. /**
  20. * Does auth require creds or not
  21. *
  22. * @var boolean
  23. */
  24. public static $requires_credentials = true;
  25. /**
  26. * Pretty auth label to use
  27. *
  28. * @var string
  29. */
  30. public static $label = 'Username/Password';
  31. /**
  32. * Site URL
  33. *
  34. * @var string
  35. */
  36. public $site_url;
  37. /**
  38. * Username
  39. *
  40. * @var string
  41. */
  42. public $username;
  43. /**
  44. * Password
  45. *
  46. * @var string
  47. */
  48. public $password;
  49. /**
  50. * API Key
  51. *
  52. * @var string
  53. */
  54. public $client_id;
  55. /**
  56. * API Secret
  57. *
  58. * @var string
  59. */
  60. public $client_secret;
  61. /**
  62. * Redirect URI
  63. *
  64. * @var string
  65. */
  66. public $redirect_uri;
  67. /**
  68. * Access Token
  69. *
  70. * @var string
  71. */
  72. public $base64_encoded;
  73. /**
  74. * Created Post ID
  75. *
  76. * @var string
  77. */
  78. public $dt_created_post_id;
  79. /**
  80. * Setup class
  81. *
  82. * @param array $args Assoc array of args.
  83. */
  84. public function __construct( $args ) {
  85. parent::__construct( $args );
  86. if ( isset( $this->password ) && isset( $this->username ) ) {
  87. $this->base64_encoded = base64_encode( $this->username . ':' . $this->password ); // @codingStandardsIgnoreLine valid use of base64_encode
  88. }
  89. if ( empty( $this->base64_encoded ) ) {
  90. $this->base64_encoded = false;
  91. }
  92. }
  93. /**
  94. * Output credentials form for this auth type
  95. *
  96. * @param array $args Array of args.
  97. * @since 0.8
  98. */
  99. public static function credentials_form( $args = array() ) {
  100. if ( empty( $args['username'] ) ) {
  101. $args['username'] = '';
  102. }
  103. ?>
  104. <div class="external-connection-wizard card">
  105. <h3><?php esc_html_e( 'Remote Connection Wizard', 'distributor' ); ?></h3>
  106. <p>
  107. <?php esc_html_e( 'Enter the URL of a site that also has the latest version of Distributor installed and the wizard will attempt to generate an application-specific password and fill in the rest of the connection details for you.', 'distributor' ); ?>
  108. </p>
  109. <p>
  110. <?php esc_html_e( 'If you are not already logged in to the other site, you will be prompted to log in before continuing. The login details you enter will not be stored on this site.', 'distributor' ); ?>
  111. </p>
  112. <label for="dt_external_site_url"><?php esc_html_e( 'External Site URL', 'distributor' ); ?></label><br>
  113. <input type="text" name="dt_external_connection_auth[site_url]" data-auth-field="dt_external_site_url" value="" class="widefat" id="dt_external_site_url" placeholder="https://remotesite.com" autocomplete="off" value="">
  114. <p>
  115. <button class="button button-large establish-connection-button button-primary">
  116. <?php esc_html_e( 'Authorize Connection', 'distributor' ); ?>
  117. </button>
  118. <a href="#" class="manual-setup-button">
  119. <?php esc_html_e( 'Manually Set Up Connection', 'distributor' ); ?>
  120. </a>
  121. <div class="dt-wizard-status">
  122. <span class="spinner is-active"></span>
  123. <span><?php esc_html_e( 'Checking the connection...', 'distributor' ); ?></span>
  124. </div>
  125. <div class="dt-wizard-error">
  126. </div>
  127. </p>
  128. <p class="description">
  129. <?php esc_html_e( 'Note: the remote site must also be running Distributor version 1.6.0 or higher to use this wizard. If not, please manually set up the connection.', 'distributor' ); ?>
  130. </p>
  131. </div>
  132. <div class="external-connection-setup">
  133. <h3><?php esc_html_e( 'Edit configuration', 'distributor' ); ?></h3>
  134. <label for="dt_username"><?php esc_html_e( 'Username', 'distributor' ); ?></label><br>
  135. <input type="text" name="dt_external_connection_auth[username]" data-auth-field="username" value="<?php echo esc_attr( $args['username'] ); ?>" class="auth-field" id="dt_username" autocomplete="off" >
  136. <span class="description"><?php esc_html_e( 'A username from the external WordPress site to connect with. For full functionality, this needs to be a user with an administrator role.', 'distributor' ); ?></span>
  137. <p>
  138. <label for="dt_username"><?php esc_html_e( 'Password', 'distributor' ); ?> <?php
  139. if ( ! empty( $args['base64_encoded'] ) ) :
  140. ?>
  141. <a class="change-password" href="#"><?php esc_html_e( '(Change)', 'distributor' ); ?></a><?php endif; ?></label><br>
  142. <?php if ( ! empty( $args['base64_encoded'] ) ) : ?>
  143. <input disabled type="password" name="dt_external_connection_auth[password]" value="ertdfweewefewwe" data-auth-field="password" class="auth-field" id="dt_password">
  144. <?php else : ?>
  145. <input type="password" name="dt_external_connection_auth[password]" data-auth-field="password" class="auth-field" id="dt_password" autocomplete="off" >
  146. <?php endif; ?>
  147. <span class="description">
  148. <?php
  149. $plugin_link = 'https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/';
  150. printf(
  151. wp_kses_post(
  152. /* translators: %s: Application Passwords documentation URL */
  153. __( '<strong>Important:</strong> We strongly recommend using the <a href="%s">Application Passwords</a> feature on the site you are connecting to in order to create a unique password for this connection. This helps limit the use of your primary password and will allow you to revoke access in the future if needed.', 'distributor' )
  154. ),
  155. esc_url( $plugin_link )
  156. );
  157. ?>
  158. </p>
  159. </div>
  160. <?php
  161. }
  162. /**
  163. * Prepare credentials for this auth type
  164. *
  165. * @param array $args Creds to prepare.
  166. * @since 0.8
  167. * @return array
  168. */
  169. public static function prepare_credentials( $args ) {
  170. $auth = array();
  171. if ( ! empty( $args['username'] ) ) {
  172. $auth['username'] = sanitize_text_field( $args['username'] );
  173. }
  174. if ( ! empty( $args['base64_encoded'] ) ) {
  175. $auth['base64_encoded'] = sanitize_text_field( $args['base64_encoded'] );
  176. }
  177. if ( ! empty( $args['password'] ) ) {
  178. $auth['base64_encoded'] = base64_encode( $args['username'] . ':' . $args['password'] ); // @codingStandardsIgnoreLine valid use of base64_encode
  179. }
  180. /**
  181. * Filter the authorization credentials prepared before saving.
  182. *
  183. * @since 1.0
  184. * @hook dt_auth_prepare_credentials
  185. *
  186. * @param {array} $auth The credentials to be saved.
  187. * @param {array} $args The arguments originally passed to `prepare_credentials`.
  188. * @param {string} $slug The authorization handler type slug.
  189. *
  190. * @return {array} The authorization credentials to be saved.
  191. */
  192. return apply_filters( 'dt_auth_prepare_credentials', $auth, $args, self::$slug );
  193. }
  194. /**
  195. * Add basic auth headers to get args
  196. *
  197. * @param array $args Args to format.
  198. * @param array $context Current context.
  199. * @since 0.8
  200. * @return array
  201. */
  202. public function format_get_args( $args = array(), $context = array() ) {
  203. if ( ! empty( $this->username ) && ! empty( $this->base64_encoded ) ) {
  204. if ( empty( $args['headers'] ) ) {
  205. $args['headers'] = array();
  206. }
  207. $args['headers']['Authorization'] = 'Basic ' . $this->base64_encoded;
  208. }
  209. return parent::format_get_args( $args, $context );
  210. }
  211. /**
  212. * Add basic auth headers to post args
  213. *
  214. * @param array $args Args to format.
  215. * @param array $context Current context.
  216. * @since 0.8
  217. * @return array
  218. */
  219. public function format_post_args( $args, $context = array() ) {
  220. if ( ! empty( $this->username ) && ! empty( $this->base64_encoded ) ) {
  221. if ( empty( $args['headers'] ) ) {
  222. $args['headers'] = array();
  223. }
  224. $args['headers']['Authorization'] = 'Basic ' . $this->base64_encoded;
  225. }
  226. return parent::format_post_args( $args, $context );
  227. }
  228. }