-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathclass-token.php
More file actions
149 lines (134 loc) · 3.61 KB
/
Copy pathclass-token.php
File metadata and controls
149 lines (134 loc) · 3.61 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
<?php
namespace WP\OAuth2\Endpoints;
use WP_Error;
use WP_Http;
use WP\OAuth2\Client;
use WP_REST_Request;
/**
* Token endpoint handler.
*/
class Token {
public function register_routes() {
register_rest_route( 'oauth2', '/access_token', array(
'methods' => 'POST',
'callback' => array( $this, 'exchange_token' ),
'args' => array(
'grant_type' => array(
'required' => true,
'type' => 'string',
'validate_callback' => array( $this, 'validate_grant_type' ),
),
'client_id' => array(
'required' => true,
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
),
'code' => array(
'required' => true,
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
),
),
));
}
/**
* Validates the given grant type.
*
* @param string $type Grant type.
*
* @return bool Whether or not the grant type is valid.
*/
public function validate_grant_type( $type ) {
return $type === 'authorization_code';
}
/**
* Validates the token given in the request, and issues a new token for the user.
*
* @param WP_REST_Request $request Request object.
*
* @return array|WP_Error Token data on success, or error on failure.
*/
public function exchange_token( WP_REST_Request $request ) {
// Check headers for client authentication.
// https://tools.ietf.org/html/rfc6749#section-2.3.1
if ( isset( $_SERVER['PHP_AUTH_USER'] ) ) {
$client_id = $_SERVER['PHP_AUTH_USER'];
$client_secret = $_SERVER['PHP_AUTH_PW'];
} else {
$client_id = $request['client_id'];
$client_secret = $request['client_secret'];
}
if ( empty( $client_id ) ) {
// invalid_client
return new WP_Error(
'oauth2.endpoints.token.exchange_token.no_client_id',
__( 'Missing client ID.'),
array(
'status' => WP_Http::UNAUTHORIZED,
)
);
}
$client = Client::get_by_id( $client_id );
if ( empty( $client ) ) {
return new WP_Error(
'oauth2.endpoints.token.exchange_token.invalid_client',
sprintf( __( 'Client ID %s is invalid.', 'oauth2' ), $client_id ),
array(
'status' => WP_Http::BAD_REQUEST,
'client_id' => $client_id,
)
);
}
if ( $client->requires_secret() ) {
// Confidential client, secret must be verified.
if ( empty( $client_secret ) ) {
// invalid_request
return new WP_Error(
'oauth2.endpoints.token.exchange_token.secret_required',
__( 'Secret is required for confidential clients.', 'oauth2' ),
array(
'status' => WP_Http::UNAUTHORIZED
)
);
}
if ( ! $client->check_secret( $client_secret ) ) {
return new WP_Error(
'oauth2.endpoints.token.exchange_token.invalid_secret',
__( 'Supplied secret is not valid for the client.', 'oauth2' ),
array(
'status' => WP_Http::UNAUTHORIZED
)
);
}
}
$auth_code = $client->get_authorization_code( $request['code'] );
if ( is_wp_error( $auth_code ) ) {
return $auth_code;
}
$is_valid = $auth_code->validate();
if ( is_wp_error( $is_valid ) ) {
// Invalid request, but code itself exists, so we should delete
// (and silently ignore errors).
$auth_code->delete();
return $is_valid;
}
// Looks valid, delete the code and issue a token.
$user = $auth_code->get_user();
if ( is_wp_error( $user ) ) {
return $user;
}
$did_delete = $auth_code->delete();
if ( is_wp_error( $did_delete ) ) {
return $did_delete;
}
$token = $client->issue_token( $user );
if ( is_wp_error( $token ) ) {
return $token;
}
$data = array(
'access_token' => $token->get_key(),
'token_type' => 'bearer',
);
return $data;
}
}