Source: classes/Authentication.php

  1. <?php
  2. /**
  3. * Authentication base class
  4. *
  5. * @package distributor
  6. */
  7. namespace Distributor;
  8. use \Distributor\ExternalConnection as ExternalConnection;
  9. /**
  10. * Authentication types extend this base abstract class. Authentication types
  11. * are used to authenticate push and pull requests for an external connection. Note that static
  12. * methods are used for interacting with the type whereas class instances deal with
  13. * an actual external connection.
  14. */
  15. abstract class Authentication {
  16. /**
  17. * Error message
  18. *
  19. * @var string
  20. */
  21. public static $error_message;
  22. /**
  23. * Set associative arguments as instance variables
  24. *
  25. * @param array $args Array of arguments to set.
  26. * @since 0.8
  27. */
  28. public function __construct( $args ) {
  29. if ( ! empty( $args ) ) {
  30. foreach ( $args as $key => $value ) {
  31. $this->$key = $value;
  32. }
  33. }
  34. }
  35. /**
  36. * Format request args for a GET request so auth occurs
  37. *
  38. * @param array $args Arguments to format.
  39. * @param array $context optional array of information about the request
  40. * @since 0.8
  41. * @return array
  42. */
  43. public function format_get_args( $args = array(), $context = array() ) {
  44. if ( ! isset( $args['headers'] ) ) {
  45. $args['headers'] = array();
  46. }
  47. $args['headers']['X-Distributor-Version'] = DT_VERSION;
  48. /**
  49. * Format request args for a GET request so auth occurs.
  50. *
  51. * @since 0.8
  52. * @hook dt_auth_format_get_args
  53. *
  54. * @param {array} $args Array of request arguments.
  55. * @param {array} $context Optional array of information about the request.
  56. * @param {object} $this The authentication class.
  57. *
  58. * @return {array} Array of request arguments.
  59. */
  60. return apply_filters( 'dt_auth_format_get_args', $args, $context, $this );
  61. }
  62. /**
  63. * Format request args for a POST request so auth occurs
  64. *
  65. * @param array $args Arguments to format.
  66. * @param array $context optional array of information about the request
  67. * @since 0.8
  68. * @return array
  69. */
  70. public function format_post_args( $args, $context = array() ) {
  71. if ( ! isset( $args['headers'] ) ) {
  72. $args['headers'] = array();
  73. }
  74. $args['headers']['X-Distributor-Version'] = DT_VERSION;
  75. /**
  76. * Format request args for a POST request so auth occurs
  77. *
  78. * @since 0.8
  79. * @hook dt_auth_format_post_args
  80. *
  81. * @param {array} $args Array of request arguments.
  82. * @param {array} $context Optional array of information about the request.
  83. * @param {object} $this The authentication class.
  84. *
  85. * @return {array} Array of request arguments.
  86. */
  87. return apply_filters( 'dt_auth_format_post_args', $args, $context, $this );
  88. }
  89. /**
  90. * Output a credentials form in the external connection management screen.
  91. *
  92. * Child classes should implement - public static function credentials_form();
  93. */
  94. /**
  95. * Store an associate array as credentials for use with an external connection.
  96. *
  97. * Child classes should implement - public static function prepare_credentials( $args );
  98. */
  99. /**
  100. * Store pre-sanizited auth credentials in DB
  101. *
  102. * @param int $external_connection_id External connection ID.
  103. * @param array $args Array of credentials to store. Should be pre-sanitized.
  104. * @since 0.8
  105. */
  106. public static function store_credentials( $external_connection_id, $args ) {
  107. update_post_meta( $external_connection_id, 'dt_external_connection_auth', $args );
  108. }
  109. /**
  110. * Oauth connection error logging facility for non production environments.
  111. *
  112. * @param string $error_message The error message to log.
  113. */
  114. public static function log_authentication_error( $error_message ) {
  115. // Store the message for output at the top of the authorization form
  116. self::$error_message = $error_message;
  117. add_action(
  118. 'auth_admin_notices',
  119. function() {
  120. ?>
  121. <div class="notice notice-error is-dismissible">
  122. <p>
  123. <strong>
  124. <?php esc_html_e( 'Authorization error:', 'distributor' ); ?>
  125. </strong> <?php echo esc_html( self::$error_message ); ?>
  126. </p>
  127. </div>
  128. <?php
  129. }
  130. );
  131. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  132. $time = gmdate( '[d/M/Y:H:i:s]' );
  133. // @codingStandardsIgnoreLine - error_log is only used when WP_DEBUG is true.
  134. error_log( $time . ': ' . $error_message );
  135. }
  136. }
  137. }