AuthController.php
2 years ago
AutomationController.php
3 years ago
EventController.php
3 years ago
GlobalSearchController.php
2 years ago
IntegrationsController.php
2 years ago
OptionController.php
3 years ago
RestController.php
2 years ago
RoutesController.php
3 years ago
SettingsController.php
3 years ago
AuthController.php
221 lines
| 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\Traits\SingletonLoader; |
| 18 | |
| 19 | /** |
| 20 | * AuthController- Connect and revoke user access_token. |
| 21 | * |
| 22 | * @category AuthController |
| 23 | * @package SureTriggers |
| 24 | * @author BSF <username@example.com> |
| 25 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 26 | * @link https://www.brainstormforce.com/ |
| 27 | * @since 1.0.0 |
| 28 | * |
| 29 | * @psalm-suppress UndefinedTrait |
| 30 | */ |
| 31 | class AuthController { |
| 32 | |
| 33 | |
| 34 | use SingletonLoader; |
| 35 | |
| 36 | /** |
| 37 | * Access token for authentication. |
| 38 | * |
| 39 | * @var string $secret_key |
| 40 | */ |
| 41 | private $access_token; |
| 42 | |
| 43 | /** |
| 44 | * Connection id for authentication. |
| 45 | * |
| 46 | * @var string $secret_key |
| 47 | */ |
| 48 | private $connection_id; |
| 49 | |
| 50 | /** |
| 51 | * Secret Key for authentication. |
| 52 | * |
| 53 | * @var string $secret_key |
| 54 | */ |
| 55 | private $secret_key; |
| 56 | |
| 57 | /** |
| 58 | * List of conected integrations/plugins. |
| 59 | * |
| 60 | * @var array $connected_integrations |
| 61 | */ |
| 62 | private $connected_integrations; |
| 63 | |
| 64 | /** |
| 65 | * Initialise data. |
| 66 | */ |
| 67 | public function __construct() { |
| 68 | $this->access_token = OptionController::get_option( 'access_token' ); |
| 69 | $this->connection_id = OptionController::get_option( 'connection_id' ); |
| 70 | $this->connected_integrations = OptionController::get_option( 'connected_integrations', [] ); |
| 71 | $this->secret_key = OptionController::get_option( 'secret_key' ); |
| 72 | add_action( 'admin_init', [ $this, 'save_connection' ] ); |
| 73 | add_action( 'updated_option', [ $this, 'updated_sc_api_key' ], 10, 3 ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Remove the respective integration triggers after deleting the connection |
| 78 | * |
| 79 | * @param string $integration Integration Name. |
| 80 | */ |
| 81 | public static function remove_integration_triggers( $integration ) { |
| 82 | $saved_triggers = OptionController::get_option( 'triggers', [] ); |
| 83 | |
| 84 | foreach ( $saved_triggers as $index => $trigger ) { |
| 85 | if ( ! empty( $trigger['integration'] ) && $integration === $trigger['integration'] ) { |
| 86 | unset( $saved_triggers[ $index ] ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | $saved_triggers = OptionController::set_option( 'triggers', $saved_triggers ); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add or revoke access token from Sass. |
| 96 | * |
| 97 | * @param object $request Request. |
| 98 | */ |
| 99 | public function revoke_connection( $request ) { |
| 100 | $secret_key = $request->get_header( 'st_authorization' ); |
| 101 | list($secret_key) = sscanf( $secret_key, 'Bearer %s' ); |
| 102 | |
| 103 | if ( $this->secret_key !== $secret_key ) { |
| 104 | return RestController::error_message( 'Invalid secret key.' ); |
| 105 | } |
| 106 | |
| 107 | // delete the suretrigger_options from wp_options table once the connection is deleted on SAAS. |
| 108 | OptionController::set_option( 'secret_key', null ); |
| 109 | |
| 110 | return RestController::success_message(); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Save sure triggers connection. |
| 116 | * |
| 117 | * @return void |
| 118 | */ |
| 119 | public function save_connection() { |
| 120 | if ( ! isset( $_GET['sure-trigger-connect-nonce'] ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if ( ! isset( $_GET['connection-status'] ) ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $nonce = sanitize_text_field( wp_unslash( $_GET['sure-trigger-connect-nonce'] ) ); |
| 129 | $connection_status = (bool) sanitize_text_field( wp_unslash( $_GET['connection-status'] ) ); |
| 130 | |
| 131 | if ( false === wp_verify_nonce( $nonce, 'sure-trigger-connect' ) ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if ( false === current_user_can( 'administrator' ) ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $access_key = isset( $_GET['sure-triggers-access-key'] ) ? sanitize_text_field( wp_unslash( $_GET['sure-triggers-access-key'] ) ) : false; |
| 140 | |
| 141 | if ( false === $connection_status ) { |
| 142 | $access_key = 'connection-denied'; |
| 143 | } |
| 144 | |
| 145 | $connected_email_id = isset( $_GET['connected_email'] ) ? sanitize_email( wp_unslash( $_GET['connected_email'] ) ) : ''; |
| 146 | |
| 147 | OptionController::set_option( 'secret_key', $access_key ); |
| 148 | OptionController::set_option( 'connected_email_key', $connected_email_id ); |
| 149 | |
| 150 | /** |
| 151 | * If there any SureCart |
| 152 | */ |
| 153 | $this->post_authorize_create_sc_connection(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Create SureCart connection at saas end. |
| 158 | * |
| 159 | * @return void |
| 160 | */ |
| 161 | public function post_authorize_create_sc_connection() { |
| 162 | if ( ! is_plugin_active( 'surecart/surecart.php' ) || ! class_exists( ApiToken::class ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | $this->create_sc_connection(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Send a request to the SAAS to create SureCart connection for authorized user |
| 171 | * |
| 172 | * @return string |
| 173 | */ |
| 174 | public function create_sc_connection() { |
| 175 | $sc_api_key = ApiToken::get(); |
| 176 | |
| 177 | if ( empty( $sc_api_key ) ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | $secret_key = OptionController::get_option( 'secret_key' ); |
| 182 | $connected_email = OptionController::get_option( 'connected_email_key' ); |
| 183 | |
| 184 | wp_remote_post( |
| 185 | trailingslashit( API_SERVER_URL ) . 'connection/create-sc', |
| 186 | [ |
| 187 | 'sslverify' => false, |
| 188 | 'headers' => [ |
| 189 | 'Authorization' => 'Bearer ' . $secret_key, |
| 190 | 'scapikey' => $sc_api_key, |
| 191 | ], |
| 192 | 'body' => [ |
| 193 | 'email' => $connected_email, |
| 194 | 'title' => 'SureCart | ' . get_bloginfo( 'name' ), |
| 195 | ], |
| 196 | ] |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Update Sure Cart connection whenever update the API key |
| 202 | * |
| 203 | * @param string $option Option. |
| 204 | * @param mixed $old_value Old value. |
| 205 | * @param mixed $value Value. |
| 206 | * @return void |
| 207 | */ |
| 208 | public function updated_sc_api_key( $option, $old_value, $value ) { |
| 209 | if ( 'sc_api_token' !== $option ) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if ( $value ) { |
| 214 | $this->create_sc_connection(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | AuthController::get_instance(); |
| 221 |