| 1 |
<?php |
| 2 |
/** |
| 3 |
* AuthController. |
| 4 |
* php version 5.6 |
| 5 |
* |
| 6 |
* @category AuthController |
| 7 |
* @package SureTriggers |
| 8 |
* @author BSF <username@example.com> |
| 9 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 |
* @link https://www.brainstormforce.com/ |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SureTriggers\Controllers; |
| 15 |
|
| 16 |
use SureCart\Models\ApiToken; |
| 17 |
use SureTriggers\Models\SaasApiToken; |
| 18 |
use SureTriggers\Traits\SingletonLoader; |
| 19 |
use WP_REST_Request; |
| 20 |
|
| 21 |
/** |
| 22 |
* AuthController- Connect and revoke user access_token. |
| 23 |
* |
| 24 |
* @category AuthController |
| 25 |
* @package SureTriggers |
| 26 |
* @author BSF <username@example.com> |
| 27 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 |
* @link https://www.brainstormforce.com/ |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @psalm-suppress UndefinedTrait |
| 32 |
*/ |
| 33 |
class AuthController { |
| 34 |
|
| 35 |
use SingletonLoader; |
| 36 |
|
| 37 |
/** |
| 38 |
* Secret Key for authentication. |
| 39 |
* |
| 40 |
* @var string|mixed $secret_key |
| 41 |
*/ |
| 42 |
private $secret_key; |
| 43 |
|
| 44 |
/** |
| 45 |
* Initialize data. |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
$this->secret_key = SaasApiToken::get(); |
| 49 |
add_action( 'admin_init', [ $this, 'save_connection' ] ); |
| 50 |
add_action( 'updated_option', [ $this, 'updated_sc_api_key' ], 10, 3 ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add or revoke access token from SaaS. |
| 55 |
* |
| 56 |
* @param WP_REST_Request $request Request. |
| 57 |
* |
| 58 |
* @return object|void |
| 59 |
*/ |
| 60 |
public function revoke_connection( $request ) { |
| 61 |
$secret_key = $request->get_header( 'st_authorization' ); |
| 62 |
|
| 63 |
if ( ! is_string( $secret_key ) || empty( $secret_key ) || empty( $this->secret_key ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$parsed = sscanf( $secret_key, 'Bearer %s' ); |
| 68 |
if ( is_array( $parsed ) ) { |
| 69 |
list( $secret_key ) = $parsed; |
| 70 |
} |
| 71 |
|
| 72 |
if ( empty( $secret_key ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! is_string( $this->secret_key ) || ! hash_equals( $this->secret_key, $secret_key ) ) { |
| 77 |
return RestController::error_message( 'Invalid secret key.' ); |
| 78 |
} |
| 79 |
|
| 80 |
// delete the suretrigger_options from wp_options table once the connection is deleted on SAAS. |
| 81 |
SaasApiToken::save( null ); |
| 82 |
|
| 83 |
return RestController::success_message(); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Save sure triggers connection. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function save_connection() { |
| 93 |
if ( ! isset( $_GET['sure-trigger-connect-nonce'] ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! isset( $_GET['connection-status'] ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$nonce = sanitize_text_field( wp_unslash( $_GET['sure-trigger-connect-nonce'] ) ); |
| 102 |
$connection_status = (bool) sanitize_text_field( wp_unslash( $_GET['connection-status'] ) ); |
| 103 |
|
| 104 |
if ( false === wp_verify_nonce( $nonce, 'sure-trigger-connect' ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ( false === current_user_can( 'administrator' ) ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$access_key = isset( $_GET['sure-triggers-access-key'] ) ? sanitize_text_field( wp_unslash( $_GET['sure-triggers-access-key'] ) ) : false; |
| 113 |
|
| 114 |
if ( false === $connection_status ) { |
| 115 |
$access_key = 'connection-denied'; |
| 116 |
} |
| 117 |
|
| 118 |
$connected_email_id = isset( $_GET['connected_email'] ) ? sanitize_email( wp_unslash( $_GET['connected_email'] ) ) : ''; |
| 119 |
|
| 120 |
if ( isset( $access_key ) ) { |
| 121 |
SaasApiToken::save( $access_key ); |
| 122 |
} |
| 123 |
OptionController::set_option( 'connected_email_key', $connected_email_id ); |
| 124 |
|
| 125 |
/** |
| 126 |
* If there any SureCart |
| 127 |
*/ |
| 128 |
$this->post_authorize_create_sc_connection(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Create SureCart connection at saas end. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function post_authorize_create_sc_connection() { |
| 137 |
if ( ! is_plugin_active( 'surecart/surecart.php' ) || ! class_exists( ApiToken::class ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
$this->create_sc_connection(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Send a request to the SAAS to create SureCart connection for authorized user |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function create_sc_connection() { |
| 150 |
if ( ! class_exists( ApiToken::class ) ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
$sc_api_key = ApiToken::get(); |
| 154 |
|
| 155 |
if ( empty( $sc_api_key ) ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
$secret_key = SaasApiToken::get(); |
| 160 |
$connected_email = OptionController::get_option( 'connected_email_key' ); |
| 161 |
|
| 162 |
wp_remote_post( |
| 163 |
trailingslashit( SURE_TRIGGERS_API_SERVER_URL ) . 'connection/create-sc', |
| 164 |
[ |
| 165 |
'sslverify' => false, |
| 166 |
'timeout' => 60, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 167 |
'headers' => [ |
| 168 |
'Authorization' => 'Bearer ' . $secret_key, |
| 169 |
'scapikey' => $sc_api_key, |
| 170 |
], |
| 171 |
'body' => [ |
| 172 |
'email' => $connected_email, |
| 173 |
'title' => 'SureCart | ' . get_bloginfo( 'name' ), |
| 174 |
], |
| 175 |
] |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Update Sure Cart connection whenever update the API key |
| 181 |
* |
| 182 |
* @param string $option Option. |
| 183 |
* @param mixed $old_value Old value. |
| 184 |
* @param mixed $value Value. |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
public function updated_sc_api_key( $option, $old_value, $value ) { |
| 188 |
if ( 'sc_api_token' !== $option ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if ( $value ) { |
| 193 |
$this->create_sc_connection(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
AuthController::get_instance(); |
| 200 |
|