oauth-api.php
9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace EM_OAuth;
use EM_Exception, EM_Notices, EM_Options;
class OAuth_API {
/**
* The name of this service to be displayed to the end user in notices etc.
* @var string
*/
protected static $service_name = 'EM OAuth 2.0';
/**
* @var string
*/
protected static $service_url = 'http://example.com';
/**
* Ths option/key name used to differentiate this from other OAuth objects stored in the database tables.
* @var string
*/
protected static $option_name = 'oauth';
/**
* Where data about this API is stored, which is by default in a serialized array with option name 'em_oauth'
* @var string
*/
protected static $option_dataset = 'dbem_oauth';
/**
* Allows overriding the default client class to be loaded, set this to the token class name you'd like to use instead.
* By default, the extended classname preceded by _Client will be used if it exists, otherwise OAuth_API_Client.
* @var string
*/
protected static $client_class;
/**
* Allows overriding the default token class to be loaded, set this to the token class name you'd like to use instead.
* By default, the extended classname preceded by _Token will be used if it exists, otherwise OAuth_API_Token.
* @var string
*/
protected static $token_class;
/**
* Defines whether authorization tokens are stored at a site, user or (eventually) network level.
* @var string 'site' or 'user' level (future consideration for 'network')
*/
protected static $authorization_scope = 'site';
/**
* Whether or not storage destination supports multiple accounts (e.g. multiple accounts for a site or a user)
* @var bool
*/
protected static $multiple_tokens = false;
/**
* @return string
*/
public static function get_service_name() {
return static::$service_name;
}
/**
* @return string
*/
public static function get_service_url() {
return static::$service_url;
}
/**
* @return string
*/
public static function get_option_name() {
return static::$option_name;
}
/**
* @return string
*/
public static function get_option_dataset() {
return static::$option_dataset;
}
/**
* @return OAuth_API_Client|string String representation of token class, used for instantiation or static function/property reference
*/
public static function get_client_class() {
if( static::$client_class !== null && class_exists(static::$client_class) ) return static::$client_class;
if( static::$client_class === null && class_exists(get_called_class().'_Client') ){
static::$client_class = get_called_class().'_Client';
return static::$client_class;
}
return 'EM_OAuth\OAuth_API_Client';
}
/**
* @return OAuth_API_Token|string String representation of token class, used for instantiation or static function/property reference
*/
public static function get_token_class() {
if( static::$token_class !== null && class_exists(static::$token_class) ) return static::$token_class;
if( static::$token_class === null && class_exists(get_called_class().'_Token') ){
static::$token_class = get_called_class().'_Token';
return static::$token_class;
}
return 'EM_OAuth\OAuth_API_Token';
}
/**
* @return string
*/
public static function get_authorization_scope() {
return static::$authorization_scope;
}
/**
* @return bool
*/
public static function supports_multiple_tokens() {
return static::$multiple_tokens;
}
/**
* Loads the service credentials into an abstract client api object. If a user ID is supplied and there's an issue retrieving an access token, an exception will be returned.
* @param int $user_id The User ID in WordPress
* @param int $api_user_id The ID of the account in Google (i.e. the email)
* @return OAuth_API_Client
* @throws EM_Exception
*/
public static function get_client( $user_id = 0, $api_user_id = 0 ) {
//set up the client
$client_class = static::get_client_class();
$client = new $client_class(); /* @var OAuth_API_Client $client */
//load user access token
if( $user_id !== false ) {
if ( empty($user_id) ) $user_id = get_current_user_id();
$client->load_token( $user_id, $api_user_id );
}
return $client;
}
public static function get_user_tokens( $user_id = false ){
if( static::$authorization_scope !== 'user' ) return array();
if( empty($user_id) ) $user_id = get_current_user_id();
$user_tokens = get_user_meta( $user_id, static::$option_dataset.'_'.static::$option_name, true );
if( empty($user_tokens) ) $user_tokens = array();
return $user_tokens;
}
/**
* @return array[OAuth_API_Token]
*/
public static function get_site_tokens(){
if( static::$authorization_scope !== 'site' ) return array();
$site_tokens = EM_Options::get(static::$option_name.'_token', array(), static::$option_dataset);
if( empty($site_tokens) ) $site_tokens = array();
return $site_tokens;
}
/**
* Includes and calls the code required to handle a callback from FB to store user auth token.
*/
public static function oauth_authorize() {
global $EM_Notices;
if( !empty($EM_Notices) ) $EM_Notices = new EM_Notices();
if( !empty($_REQUEST['code']) ){
try{
$client = static::get_client(false);
if( $client->oauth_state && (empty($_REQUEST['state']) || !wp_verify_nonce( $_REQUEST['state'], static::$option_name.'_authorize')) ){
$EM_Notices->add_error( sprintf( esc_html__( 'There was an error connecting to %s: %s', 'events-manager' ), static::$service_name, '<code>No State Provided</code>'), true );
}else{
try {
$client->request( $_REQUEST['code'] );
$EM_Notices->add_confirm( sprintf( esc_html__( 'Your account has been successfully connected with %s!', 'events-manager' ), static::$service_name ), true);
} catch ( EM_Exception $e ){
$EM_Notices->add_error( sprintf( esc_html__( 'There was an error connecting to %s: %s', 'events-manager' ), static::$service_name, '<code>'.$e->getMessage().'</code>' ), true );
}
}
} catch ( EM_Exception $ex ){
$EM_Notices->add_error($ex->get_messages(), true);
}
}else{
$EM_Notices->add_error( sprintf( esc_html__( 'There was an error connecting to %s: %s', 'events-manager' ), static::$service_name, '<code>No Authorization Code Provided</code>'), true );
}
// Redirect to settings page
$query_args = array( 'page' => 'events-manager-options' );
$url = add_query_arg( $query_args, admin_url( 'admin.php' ) );
wp_redirect( $url );
die();
}
/**
* Handles disconnecting a user from one or all their connected Google accounts, attempting to revoke their key in the process.
*/
public static function oauth_disconnect(){
global $EM_Notices;
if( !empty($EM_Notices) ) $EM_Notices = new EM_Notices();
if( static::$authorization_scope == 'user' ){
$account_tokens = static::get_user_tokens();
}else{
$account_tokens = static::get_site_tokens();
}
$accounts_to_disconnect = array();
if( empty($_REQUEST['user']) && !empty($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], 'em-oauth-'. static::$option_name .'-disconnect') ){
$accounts_to_disconnect = array_keys($account_tokens);
}elseif( !empty($_REQUEST['account']) && !empty($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], 'em-oauth-'. static::$option_name .'-disconnect-'.$_REQUEST['account']) ){
if( !empty($account_tokens[$_REQUEST['account']]) ){
$accounts_to_disconnect[] = $_REQUEST['account'];
}
}else{
$EM_Notices->add_error('Missing nonce, please contact your administrator.', true);
}
if( !empty($accounts_to_disconnect) ){
$errors = $disconnected_accounts = array();
foreach( $accounts_to_disconnect as $account_id ){
try{
$client = static::get_client( get_current_user_id(), $account_id);
$client->revoke();
} catch ( EM_Exception $ex ){
$account_name = !empty( $client->token->email ) ? $client->token->email : $client->token->name;
$errors[] = "<em>$account_name</em> - " . $ex->getMessage();
} finally{
$disconnected_accounts[] = $account_id;
unset($account_tokens[$account_id]);
}
}
if( !empty($disconnected_accounts) ){
if( static::$authorization_scope == 'user' ){
if( empty($account_tokens) ){
delete_user_meta( get_current_user_id(), 'em_oauth_'.static::$option_name );
}else{
update_user_meta( get_current_user_id(), 'em_oauth_'.static::$option_name, $account_tokens );
}
}else{
EM_Options::set(static::$option_name.'_token', $account_tokens, static::$option_dataset);
}
$success = _n('You have successfully disconnected from your %s account.', 'You have successfully disconnected from your %s accounts.', count($accounts_to_disconnect), 'events-manager');
$EM_Notices->add_confirm(sprintf($success, static::$service_name), true);
}
if( !empty($errors) ){
$error_msg = sprintf( esc_html__('There were some issues whilst disconnecting from your %s account(s) :', 'events-manager'), static::$service_name );
array_unshift( $errors, $error_msg );
$EM_Notices->add_error( $errors, true );
}
}
// Redirect to settings page
$query_args = array( 'page' => 'events-manager-options' );
$url = add_query_arg( $query_args, admin_url( 'admin.php' ) );
wp_redirect( $url );
die();
}
}
//include dependents
include('oauth-api-token.php');
include('oauth-api-client.php');
if( is_admin() ){
include('oauth-admin-settings.php');
}