PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.52
OttoKit: All-in-One Automation Platform v1.0.52
1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Controllers / AuthController.php
suretriggers / src / Controllers Last commit date
AuthController.php 2 years ago AutomationController.php 3 years ago EventController.php 3 years ago GlobalSearchController.php 1 year 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
229 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 // Remove the respective integration triggers field data after deleting the connection.
93 $saved_triggers_data = OptionController::get_option( 'trigger_data', [] );
94 foreach ( $saved_triggers_data as $index => $trigger ) {
95 if ( is_array( $saved_triggers_data ) && is_array( $trigger ) && ! empty( $trigger ) && $integration === $index ) {
96 unset( $saved_triggers_data[ $index ] );
97 }
98 }
99 $saved_triggers_data = OptionController::set_option( 'trigger_data', $saved_triggers_data );
100 }
101
102 /**
103 * Add or revoke access token from Sass.
104 *
105 * @param object $request Request.
106 */
107 public function revoke_connection( $request ) {
108 $secret_key = $request->get_header( 'st_authorization' );
109 list($secret_key) = sscanf( $secret_key, 'Bearer %s' );
110
111 if ( $this->secret_key !== $secret_key ) {
112 return RestController::error_message( 'Invalid secret key.' );
113 }
114
115 // delete the suretrigger_options from wp_options table once the connection is deleted on SAAS.
116 OptionController::set_option( 'secret_key', null );
117
118 return RestController::success_message();
119
120 }
121
122 /**
123 * Save sure triggers connection.
124 *
125 * @return void
126 */
127 public function save_connection() {
128 if ( ! isset( $_GET['sure-trigger-connect-nonce'] ) ) {
129 return;
130 }
131
132 if ( ! isset( $_GET['connection-status'] ) ) {
133 return;
134 }
135
136 $nonce = sanitize_text_field( wp_unslash( $_GET['sure-trigger-connect-nonce'] ) );
137 $connection_status = (bool) sanitize_text_field( wp_unslash( $_GET['connection-status'] ) );
138
139 if ( false === wp_verify_nonce( $nonce, 'sure-trigger-connect' ) ) {
140 return;
141 }
142
143 if ( false === current_user_can( 'administrator' ) ) {
144 return;
145 }
146
147 $access_key = isset( $_GET['sure-triggers-access-key'] ) ? sanitize_text_field( wp_unslash( $_GET['sure-triggers-access-key'] ) ) : false;
148
149 if ( false === $connection_status ) {
150 $access_key = 'connection-denied';
151 }
152
153 $connected_email_id = isset( $_GET['connected_email'] ) ? sanitize_email( wp_unslash( $_GET['connected_email'] ) ) : '';
154
155 OptionController::set_option( 'secret_key', $access_key );
156 OptionController::set_option( 'connected_email_key', $connected_email_id );
157
158 /**
159 * If there any SureCart
160 */
161 $this->post_authorize_create_sc_connection();
162 }
163
164 /**
165 * Create SureCart connection at saas end.
166 *
167 * @return void
168 */
169 public function post_authorize_create_sc_connection() {
170 if ( ! is_plugin_active( 'surecart/surecart.php' ) || ! class_exists( ApiToken::class ) ) {
171 return;
172 }
173
174 $this->create_sc_connection();
175 }
176
177 /**
178 * Send a request to the SAAS to create SureCart connection for authorized user
179 *
180 * @return string
181 */
182 public function create_sc_connection() {
183 $sc_api_key = ApiToken::get();
184
185 if ( empty( $sc_api_key ) ) {
186 return;
187 }
188
189 $secret_key = OptionController::get_option( 'secret_key' );
190 $connected_email = OptionController::get_option( 'connected_email_key' );
191
192 wp_remote_post(
193 trailingslashit( API_SERVER_URL ) . 'connection/create-sc',
194 [
195 'sslverify' => false,
196 'headers' => [
197 'Authorization' => 'Bearer ' . $secret_key,
198 'scapikey' => $sc_api_key,
199 ],
200 'body' => [
201 'email' => $connected_email,
202 'title' => 'SureCart | ' . get_bloginfo( 'name' ),
203 ],
204 ]
205 );
206 }
207
208 /**
209 * Update Sure Cart connection whenever update the API key
210 *
211 * @param string $option Option.
212 * @param mixed $old_value Old value.
213 * @param mixed $value Value.
214 * @return void
215 */
216 public function updated_sc_api_key( $option, $old_value, $value ) {
217 if ( 'sc_api_token' !== $option ) {
218 return;
219 }
220
221 if ( $value ) {
222 $this->create_sc_connection();
223 }
224 }
225
226 }
227
228 AuthController::get_instance();
229