-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.php
59 lines (38 loc) · 1.97 KB
/
endpoints.php
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
<?php
defined( 'ABSPATH' ) or die();
use League\OAuth2\Server\Middleware\AuthorizationServerMiddleware;
function cacwpssao_grant_request( WP_REST_Request $request ) {
$params = $request->get_params();
// return var_dump($request);
if( !isset( $params['grant_type'] ) || empty( $params['grant_type'] ) || !isset( $params['client_id'] ) || empty( $params['client_id'] ) || !isset( $params['client_secret'] ) || empty( $params['client_secret'] ) || !isset( $params['scope'] ) || empty( $params['scope'] ) ) {
return new WP_Error( 'malformed-request', 'Token requests require grant_type, client_id, client_secret, and scope.' . print_r( $request, true ), array( 'status' => 401 ) );
}
$oauth = cacwpssaoServer();
$token_request = new CacwpssaoServerRequest( $request );
$response = new CacwpssaoResponse( 200, array(), 'stuff' );
try {
// Try to respond to the request
$token_response = $oauth->respondToAccessTokenRequest($token_request, $response);
return $token_response->getWpResponse();
} catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) {
// All instances of OAuthServerException can be formatted into a HTTP response
return $exception->generateHttpResponse($response)->getWpResponse();
} catch (\Exception $exception) {
// Unknown exception
$body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body)->getWpResponse();
}
}
add_action( 'rest_api_init', function () {
register_rest_route( 'cacoauth/v1', '/token', array(
'methods' => 'GET, POST',
'callback' => 'cacwpssao_grant_request',
) );
} );
function cacwpssao_process_auth( $request, $response ) {
return var_dump( $response->getWpResponse() );
return var_dump($response->getBody()->__toString());
$final_response = $response->withBody($response->getBody());
return var_dump( $final_response );
}