| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\auth; |
| 4 |
|
| 5 |
use Leadin\data\User; |
| 6 |
use Leadin\data\Portal_Options; |
| 7 |
use Leadin\client\Access_Token_Api_Client; |
| 8 |
use Leadin\auth\OAuthCrypto; |
| 9 |
use Leadin\admin\Routing; |
| 10 |
use Leadin\admin\MenuConstants; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class managing OAuth2 authorization |
| 14 |
*/ |
| 15 |
class OAuth { |
| 16 |
|
| 17 |
/** |
| 18 |
* Return the flag checking if we're connected with OAuth. |
| 19 |
* |
| 20 |
* @return bool True if the OAuth version of the plugin is enabled or not. |
| 21 |
*/ |
| 22 |
public static function is_enabled() { |
| 23 |
return ! empty( Portal_Options::get_refresh_token() ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Authorizes the plugin with given oauth credentials by storing them in the options DB. |
| 28 |
* |
| 29 |
* @param string $refresh_token OAuth refresh token to store. |
| 30 |
*/ |
| 31 |
public static function authorize( $refresh_token ) { |
| 32 |
$encrypted_refresh_token = OAuthCrypto::encrypt( $refresh_token ); |
| 33 |
Portal_Options::set_refresh_token( $encrypted_refresh_token ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Deauthorizes the plugin by deleting OAuth credentials from the options DB. |
| 38 |
*/ |
| 39 |
public static function deauthorize() { |
| 40 |
Portal_Options::delete_access_token(); |
| 41 |
Portal_Options::delete_refresh_token(); |
| 42 |
Portal_Options::delete_expiry_time(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the refresh token stored in the options table. |
| 47 |
* |
| 48 |
* @return string The stored refresh token in the Options table. |
| 49 |
* |
| 50 |
* @throws \Exception If no refresh token is available. |
| 51 |
*/ |
| 52 |
public static function get_refresh_token() { |
| 53 |
$encrypted_refresh_token = Portal_Options::get_refresh_token(); |
| 54 |
|
| 55 |
if ( '' === $encrypted_refresh_token ) { |
| 56 |
self::deauthorize(); |
| 57 |
throw new \Exception( 'Refresh token is empty' ); |
| 58 |
} |
| 59 |
|
| 60 |
return OAuthCrypto::decrypt( $encrypted_refresh_token ); |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|