-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathREST_Authentication_Controller.php
More file actions
177 lines (161 loc) · 4.92 KB
/
REST_Authentication_Controller.php
File metadata and controls
177 lines (161 loc) · 4.92 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
<?php
/**
* Class Google\Site_Kit\Core\Authentication\REST_Authentication_Controller
*
* @package Google\Site_Kit
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Authentication;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\REST_Route;
use Google\Site_Kit\Core\REST_API\REST_Routes;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
/**
* REST Authentication Controller Class.
*
* @since 1.131.0
* @access private
* @ignore
*/
final class REST_Authentication_Controller {
/**
* Authentication instance.
*
* @since 1.131.0
* @var Authentication
*/
protected $authentication;
/**
* Constructor.
*
* @since 1.131.0
*
* @param Authentication $authentication Authentication instance.
*/
public function __construct( Authentication $authentication ) {
$this->authentication = $authentication;
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.131.0
*/
public function register() {
add_filter(
'googlesitekit_rest_routes',
function ( $routes ) {
return array_merge( $routes, $this->get_rest_routes() );
}
);
add_filter(
'googlesitekit_apifetch_preload_paths',
function ( $routes ) {
$authentication_routes = array(
'/' . REST_Routes::REST_ROOT . '/core/site/data/connection',
'/' . REST_Routes::REST_ROOT . '/core/user/data/authentication',
);
return array_merge( $routes, $authentication_routes );
}
);
}
/**
* Gets related REST routes.
*
* @since 1.3.0
* @since 1.131.0 Moved to REST_Authentication_Controller class.
*
* @return array List of REST_Route objects.
*/
private function get_rest_routes() {
$can_setup = function () {
return current_user_can( Permissions::SETUP );
};
$can_access_authentication = function () {
return current_user_can( Permissions::VIEW_SPLASH ) || current_user_can( Permissions::VIEW_DASHBOARD );
};
$can_disconnect = function () {
return current_user_can( Permissions::AUTHENTICATE );
};
$can_view_authenticated_dashboard = function () {
return current_user_can( Permissions::VIEW_AUTHENTICATED_DASHBOARD );
};
return array(
new REST_Route(
'core/site/data/connection',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function () {
$data = array(
'connected' => $this->authentication->credentials()->has(),
'resettable' => $this->authentication->get_options_instance()->has( Credentials::OPTION ),
'setupCompleted' => $this->authentication->is_setup_completed(),
'hasConnectedAdmins' => $this->authentication->get_has_connected_admins_instance()->get(),
'hasMultipleAdmins' => $this->authentication->get_has_multiple_admins_instance()->get(),
);
return new WP_REST_Response( $data );
},
'permission_callback' => $can_setup,
),
)
),
new REST_Route(
'core/user/data/authentication',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function () {
$oauth_client = $this->authentication->get_oauth_client();
$is_authenticated = $this->authentication->is_authenticated();
$data = array(
'authenticated' => $is_authenticated,
'requiredScopes' => $oauth_client->get_required_scopes(),
'grantedScopes' => $is_authenticated ? $oauth_client->get_granted_scopes() : array(),
'unsatisfiedScopes' => $is_authenticated ? $oauth_client->get_unsatisfied_scopes() : array(),
'needsReauthentication' => $oauth_client->needs_reauthentication(),
'disconnectedReason' => $this->authentication->get_disconnected_reason_instance()->get(),
'connectedProxyURL' => $this->authentication->get_connected_proxy_url_instance()->get(),
);
return new WP_REST_Response( $data );
},
'permission_callback' => $can_access_authentication,
),
)
),
new REST_Route(
'core/user/data/disconnect',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => function () {
$this->authentication->disconnect();
return new WP_REST_Response( true );
},
'permission_callback' => $can_disconnect,
),
)
),
new REST_Route(
'core/user/data/get-token',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => function () {
$this->authentication->do_refresh_user_token();
return new WP_REST_Response(
array(
'token' => $this->authentication->get_oauth_client()->get_access_token(),
)
);
},
'permission_callback' => $can_view_authenticated_dashboard,
),
)
),
);
}
}