| 1 |
<?php |
| 2 |
/** |
| 3 |
* Google Calendar Auth Class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Integrations\Google; |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use InvalidArgumentException; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Auth |
| 14 |
*/ |
| 15 |
class Client { |
| 16 |
const TIMETICS_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; |
| 17 |
const TIMETICS_TOKEN_URI = 'https://oauth2.googleapis.com/token'; |
| 18 |
const TIMETICS_AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth'; |
| 19 |
const API_BASE_PATH = 'https://www.googleapis.com'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Store google app client id |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $client_id; |
| 27 |
|
| 28 |
/** |
| 29 |
* Store google app secrete |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $client_secrete; |
| 34 |
|
| 35 |
/** |
| 36 |
* Store google app auth scope |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $auth_scope; |
| 41 |
|
| 42 |
/** |
| 43 |
* Store google auth redirect uri |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $redirect_uri; |
| 48 |
|
| 49 |
/** |
| 50 |
* Get app client id |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function get_client_id() { |
| 55 |
return $this->client_id; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get app secrete |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function get_client_secrete() { |
| 64 |
return $this->client_secrete; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get app auth scope |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function get_auth_scope() { |
| 73 |
return $this->auth_scope; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get auth redirect uri |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function get_redirect_uri() { |
| 82 |
return $this->redirect_uri; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get auth url |
| 87 |
* |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public function get_auth_url( $state = '' ) { |
| 91 |
$params = array( |
| 92 |
'client_id' => $this->client_id, |
| 93 |
'scope' => urlencode_deep( $this->auth_scope ), |
| 94 |
'redirect_uri' => $this->redirect_uri, |
| 95 |
'response_type' => 'code', |
| 96 |
'access_type' => 'offline', |
| 97 |
); |
| 98 |
|
| 99 |
if ( ! empty( $state ) ) { |
| 100 |
$params['state'] = $state; |
| 101 |
} |
| 102 |
|
| 103 |
return add_query_arg( $params, self::TIMETICS_AUTH_URL ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set google authentication configuration |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function set_auth_config( $args = array() ) { |
| 112 |
$defaults = array( |
| 113 |
'client_id' => '', |
| 114 |
'client_secrete' => '', |
| 115 |
); |
| 116 |
|
| 117 |
$args = wp_parse_args( $args, $defaults ); |
| 118 |
|
| 119 |
$this->client_id = $args['client_id']; |
| 120 |
$this->client_secrete = $args['client_secrete']; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set authentication scope |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function add_scope( $scope ) { |
| 129 |
$this->auth_scope = $scope; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Set redirect uri |
| 134 |
* |
| 135 |
* @param string $uri Redirect uri |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function set_redirect_uri( $uri ) { |
| 140 |
$this->redirect_uri = $uri; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Fetch access token |
| 145 |
* |
| 146 |
* @param string $code |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
public function fetch_access_token_with_auth_code( $code ) { |
| 151 |
if ( strlen( $code ) === 0 ) { |
| 152 |
throw new InvalidArgumentException( 'Invalid code' ); |
| 153 |
} |
| 154 |
|
| 155 |
$args = array( |
| 156 |
'client_id' => $this->client_id, |
| 157 |
'client_secret' => $this->client_secrete, |
| 158 |
'code' => $code, |
| 159 |
'redirect_uri' => $this->redirect_uri, |
| 160 |
'grant_type' => 'authorization_code', |
| 161 |
); |
| 162 |
|
| 163 |
$response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) ); |
| 164 |
|
| 165 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 166 |
|
| 167 |
if ( 200 != $status_code ) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 172 |
|
| 173 |
return $data; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Fetch access token by using refresh token |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function fetch_access_token_with_refresh_token( $refresh_token ) { |
| 182 |
$args = array( |
| 183 |
'client_id' => $this->client_id, |
| 184 |
'client_secret' => $this->client_secrete, |
| 185 |
'refresh_token' => $refresh_token, |
| 186 |
'redirect_uri' => $this->redirect_uri, |
| 187 |
'grant_type' => 'refresh_token', |
| 188 |
); |
| 189 |
|
| 190 |
$response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) ); |
| 191 |
|
| 192 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 193 |
|
| 194 |
if ( 200 != $status_code ) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 199 |
|
| 200 |
return $data; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Revoke authorization |
| 205 |
* |
| 206 |
* @param string $token |
| 207 |
* |
| 208 |
* @return bool |
| 209 |
*/ |
| 210 |
public function revoke( $token ) { |
| 211 |
$response = wp_remote_post( |
| 212 |
self::TIMETICS_REVOKE_URI, array( |
| 213 |
'headers' => array( |
| 214 |
'content-type' => 'application/x-www-form-urlencoded', |
| 215 |
), |
| 216 |
'body' => build_query( |
| 217 |
array( |
| 218 |
'token' => $token, |
| 219 |
) |
| 220 |
), |
| 221 |
) |
| 222 |
); |
| 223 |
|
| 224 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 225 |
|
| 226 |
return $status_code === 200; |
| 227 |
} |
| 228 |
} |
| 229 |
|