PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.0.48
OttoKit: All-in-One Automation Platform v1.0.48
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 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