Auth.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom class for authenticating with the Promoter Connector. |
| 5 | * |
| 6 | * @since 4.9 |
| 7 | */ |
| 8 | class Tribe__Promoter__Auth { |
| 9 | |
| 10 | /** |
| 11 | * @var Tribe__Promoter__Connector $connector |
| 12 | */ |
| 13 | private $connector; |
| 14 | |
| 15 | /** |
| 16 | * Tribe__Promoter__Auth constructor. |
| 17 | * |
| 18 | * @since 4.9 |
| 19 | * |
| 20 | * @param Tribe__Promoter__Connector $connector Connector object. |
| 21 | * @return void |
| 22 | */ |
| 23 | public function __construct( Tribe__Promoter__Connector $connector ) { |
| 24 | $this->connector = $connector; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Register the promoter auth key as part of the settings in order to make it available into the REST API. |
| 29 | * |
| 30 | * @since 4.12.6 |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function register_setting() { |
| 35 | register_setting( |
| 36 | 'options', |
| 37 | 'tribe_promoter_auth_key', |
| 38 | [ |
| 39 | 'type' => 'string', |
| 40 | 'show_in_rest' => true, |
| 41 | 'description' => __( 'Promoter Key', 'tribe-common' ), |
| 42 | 'sanitize_callback' => 'sanitize_text_field', |
| 43 | ] |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Add an update the KEY used for promoter during the connection. |
| 49 | * |
| 50 | * @since 4.9.12 |
| 51 | * |
| 52 | * @param $secret_key |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function filter_promoter_secret_key( $secret_key ) { |
| 57 | |
| 58 | _deprecated_function( __METHOD__, '4.12.6' ); |
| 59 | |
| 60 | return empty( $secret_key ) ? $this->generate_secret_key() : $secret_key; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Authorize the request with the Promoter Connector. |
| 65 | * |
| 66 | * @since 4.9 |
| 67 | * |
| 68 | * @return bool Whether the request was authorized successfully. |
| 69 | */ |
| 70 | public function authorize_with_connector() { |
| 71 | $secret_key = $this->generate_secret_key(); |
| 72 | $promoter_key = tribe_get_request_var( 'promoter_key' ); |
| 73 | $license_key = tribe_get_request_var( 'license_key' ); |
| 74 | |
| 75 | // send request to auth connector |
| 76 | $result = $this->connector->authorize_with_connector( get_current_user_id(), $secret_key, $promoter_key, $license_key ); |
| 77 | |
| 78 | // If the secret was not stored correctly on Connector Application, remove it! |
| 79 | if ( ! $result ) { |
| 80 | delete_option( 'tribe_promoter_auth_key' ); |
| 81 | } |
| 82 | |
| 83 | return $result; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Grab the WP constant and store it as the auth key, if none exists or is it empty |
| 88 | * it creates a dynamic one. |
| 89 | * |
| 90 | * @since 4.9.12 |
| 91 | * |
| 92 | * @since 4.9 |
| 93 | * |
| 94 | * @return string The secret key. |
| 95 | */ |
| 96 | public function generate_secret_key() { |
| 97 | |
| 98 | $salt = wp_generate_password( 6 ); |
| 99 | |
| 100 | if ( defined( 'AUTH_KEY' ) ) { |
| 101 | $key = AUTH_KEY; |
| 102 | } else { |
| 103 | $key = wp_generate_password( 25 ); |
| 104 | } |
| 105 | |
| 106 | $key = sha1( $salt . get_current_blog_id() . $key . get_bloginfo( 'url' ) ); |
| 107 | |
| 108 | update_option( 'tribe_promoter_auth_key', $key ); |
| 109 | |
| 110 | return $key; |
| 111 | } |
| 112 | } |
| 113 |