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