PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.0.54
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.0.54
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / public / auth / class-oauth.php

class-oauth.php in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.0.54, at public/auth/class-oauth.php

87 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadin\auth;
4
5 use Leadin\data\User;
6 use Leadin\data\Portal_Options;
7 use Leadin\auth\OAuthCrypto;
8 use Leadin\admin\Routing;
9 use Leadin\admin\MenuConstants;
10
11 /**
12 * Class managing OAuth2 authorization
13 */
14 class OAuth {
15
16 const MAX_RETRIES = 3;
17 const RETRY_DELAY_MICROSECONDS = 100000; // 100ms converted to microseconds
18
19 /**
20 * Authorizes the plugin with given oauth credentials by storing them in the options DB.
21 *
22 * @param string $refresh_token OAuth refresh token to store.
23 */
24 public static function authorize( $refresh_token ) {
25 $encrypted_refresh_token = OAuthCrypto::encrypt( $refresh_token );
26 Portal_Options::set_refresh_token( $encrypted_refresh_token );
27
28 Portal_Options::set_last_authorize_time();
29 }
30
31 /**
32 * Deauthorizes the plugin by deleting OAuth credentials from the options DB.
33 */
34 public static function deauthorize() {
35 Portal_Options::delete_refresh_token();
36
37 Portal_Options::set_last_deauthorize_time();
38 }
39
40 /**
41 * Attempts to get and decrypt the refresh token with retries.
42 * Records an error if decryption fails or if the token is invalid.
43 *
44 * @return string The decrypted refresh token, or an empty string on failure.
45 */
46 public static function get_refresh_token() {
47 for ( $attempt = 0; $attempt < self::MAX_RETRIES; $attempt++ ) {
48 $encrypted_refresh_token = Portal_Options::get_refresh_token();
49
50 if ( ! self::is_valid_value( $encrypted_refresh_token ) ) {
51 Portal_Options::set_refresh_token_error( 'Token is invalid or missing' );
52 self::retry_delay();
53 continue;
54 }
55
56 $refresh_token = OAuthCrypto::decrypt( $encrypted_refresh_token );
57
58 if ( ! self::is_valid_value( $refresh_token ) ) {
59 Portal_Options::set_refresh_token_error( 'Decryption failed' );
60 self::retry_delay();
61 continue;
62 }
63
64 return $refresh_token;
65 }
66
67 return '';
68 }
69
70 /**
71 * Checks if the provided value is valid (not false, null, or empty).
72 *
73 * @param mixed $value The value to check.
74 * @return bool Whether the value is valid.
75 */
76 private static function is_valid_value( $value ) {
77 return false !== $value && null !== $value && '' !== $value;
78 }
79
80 /**
81 * Delays the execution to handle transient issues before retrying.
82 */
83 private static function retry_delay() {
84 usleep( self::RETRY_DELAY_MICROSECONDS );
85 }
86 }
87