PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.5
OttoKit: All-in-One Automation Platform v1.1.5
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 11 months ago AutomationController.php 11 months ago EventController.php 11 months ago GlobalSearchController.php 9 months ago IntegrationsController.php 11 months ago OptionController.php 3 years ago RestController.php 10 months ago RoutesController.php 1 year ago SettingsController.php 10 months ago WebhookRequestsController.php 1 year ago
AuthController.php
200 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 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 ( $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