-
Notifications
You must be signed in to change notification settings - Fork 9
/
class-indieauth-token-endpoint.php
171 lines (158 loc) · 5.19 KB
/
class-indieauth-token-endpoint.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
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
<?php
/**
*
*
* Implements IndieAuth Token Endpoint
*/
class IndieAuth_Token_Endpoint {
private $tokens;
public function __construct() {
$this->tokens = new Token_User( '_indieauth_token_' );
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
/**
* Extracts the token from the given authorization header.
*
* @param string $header Authorization header.
*
* @return string|null Token on success, null on failure.
*/
public function get_token_from_bearer_header( $header ) {
if ( is_string( $header ) && preg_match( '/Bearer ([\x20-\x7E]+)/', trim( $header ), $matches ) ) {
return $matches[1];
}
return null;
}
/**
* Register the Route.
*/
public function register_routes() {
register_rest_route(
'indieauth/1.0', '/token', array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'post' ),
'args' => array(
'grant_type' => array(),
'code' => array(),
'client_id' => array(
'validate_callback' => 'rest_is_valid_url',
'sanitize_callback' => 'esc_url_raw',
),
'redirect_uri' => array(
'validate_callback' => 'rest_is_valid_url',
'sanitize_callback' => 'esc_url_raw',
),
'me' => array(
'validate_callback' => 'rest_is_valid_url',
'sanitize_callback' => 'esc_url_raw',
),
'action' => array(),
'token' => array(),
),
),
)
);
register_rest_route(
'indieauth/1.0', '/token', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get' ),
'args' => array(),
),
)
);
}
public function get_token( $token, $hash = true ) {
return $this->tokens->get( $token, $hash );
}
public function get( $request ) {
$params = $request->get_params();
$access_token = $this->get_token_from_bearer_header( $request->get_header( 'Authorization' ) );
if ( ! $access_token ) {
return new WP_OAuth_Response( 'parameter_absent', __( 'Bearer Token Not Supplied', 'indieauth' ), 400 );
}
$token = $this->get_token( $access_token );
if ( ! $token ) {
return new WP_OAuth_Response( 'invalid_token', __( 'Invalid access token', 'indieauth' ), 401 );
}
return rest_ensure_response( $token );
}
public function set_token( $token ) {
if ( ! isset( $token['me'] ) ) {
return false;
}
$user = get_user_by_identifier( $token['me'] );
if ( ! $user ) {
return false;
}
$this->tokens->set_user( $user->ID );
return $this->tokens->set( $token );
}
public function delete_token( $id, $user_id = null ) {
$this->tokens->set_user( $user_id );
return $this->tokens->destroy( $id );
}
// Request or revoke a token
public function post( $request ) {
$params = $request->get_params();
// Revoke Token
if ( isset( $params['action'] ) && isset( $params['token'] ) && 'revoke' === $params['action'] ) {
$this->delete_token( $params['token'] );
return __( 'The Token Provided is No Longer Valid', 'indieauth' );
}
// Request Token
if ( isset( $params['grant_type'] ) && 'authorization_code' === $params['grant_type'] ) {
return $this->request( $params );
}
// Everything Failed
return new WP_OAuth_Response( 'invalid_request', __( 'Invalid Request', 'indieauth' ), 400 );
}
// Request a Token
public function request( $params ) {
$diff = array_diff( array( 'code', 'client_id', 'redirect_uri', 'me' ), array_keys( $params ) );
if ( ! empty( $diff ) ) {
return new WP_OAuth_Response( 'invalid_request', __( 'The request is missing one or more required parameters', 'indieauth' ), 400 );
}
$response = $this->verify_local_authorization_code(
array(
'code' => $params['code'],
'redirect_uri' => $params['redirect_uri'],
'client_id' => $params['client_id'],
)
);
$error = get_oauth_error( $response );
if ( $error ) {
return $error;
}
// Do not issue a token if the authorization code contains no scope
if ( isset( $response['scope'] ) ) {
$token = array(
'token_type' => 'Bearer',
'scope' => $response['scope'],
'me' => $response['me'],
'issued_by' => rest_url( 'indieauth/1.0/token' ),
'client_id' => $params['client_id'],
'issued_at' => current_time( 'timestamp', 1 ),
);
$token['access_token'] = $this->set_token( $token );
if ( $token ) {
// Return only the standard keys in the response
return( wp_array_slice_assoc( $token, array( 'access_token', 'token_type', 'scope', 'me' ) ) );
}
} else {
return new WP_OAuth_Response( 'invalid_grant', __( 'This authorization code was issued with no scope, so it cannot be used to obtain an access token', 'indieauth' ), 400 );
}
return new WP_OAuth_Response( 'server_error', __( 'There was an error issuing the access token', 'indieauth' ), 500 );
}
public static function verify_local_authorization_code( $post_args ) {
$tokens = new Token_User( '_indieauth_code_' );
$return = $tokens->get( $post_args['code'] );
if ( ! $return ) {
return new WP_OAuth_Response( 'invalid_code', __( 'Invalid authorization code', 'indieauth' ), 401 );
}
$tokens->destroy( $post_args['code'] );
return $return;
}
}
new IndieAuth_Token_Endpoint();