Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/resource owner grant type #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 69 additions & 29 deletions inc/endpoints/class-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ public function register_routes() {
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'username '=> [
'required' => false,
'type' => 'string',
],
'password'=> [
'required' => false,
'type' => 'string',
],
'code' => [
'required' => true,
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
'required' => false,
'type' => 'string'
],
],
]
Expand All @@ -52,7 +59,7 @@ public function register_routes() {
* @return bool Whether or not the grant type is valid.
*/
public function validate_grant_type( $type ) {
return 'authorization_code' === $type;
return 'authorization_code' === $type || 'password';
}

/**
Expand All @@ -76,36 +83,69 @@ public function exchange_token( WP_REST_Request $request ) {
);
}

$auth_code = $client->get_authorization_code( $request['code'] );
if ( is_wp_error( $auth_code ) ) {
return $auth_code;
}
if ($request->get_param('grant_type') == 'authorization_code') {
$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();
$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;
}
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;
}
// Looks valid, delete the code and issue a token.
$user = $auth_code->get_user();
if (is_wp_error($user)) {
return $user;
}

$token = $client->issue_token( $user );
if ( is_wp_error( $token ) ) {
return $token;
}
$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;
}
} else if ($request->get_param('grant_type') === 'password') {
$username = $request->get_param('username');
$password = $request->get_param('password');
try {
$user = wp_authenticate($username, $password);
if (is_wp_error($user)) {
return $user;
}
$token = $client->issue_token($user);
if (is_wp_error($token)) {
return $token;
}
$token = $client->issue_token($user);
if (is_wp_error($token)) {
return $token;
}
$data = [
'access_token' => $token->get_key(),
'token_type' => 'bearer',
];
return $data;
} catch (Exception $e) {
return new WP_Error(
'oauth2.endpoints.token.exchange_token.invalid_client',
/* translators: %s: client ID */
sprintf(__('Client ID %s is invalid.', 'oauth2'), $request['client_id']),
[
'status' => WP_Http::BAD_REQUEST,
'client_id' => $request['client_id'],
]
);
}
}
$data = [
'access_token' => $token->get_key(),
'token_type' => 'bearer',
Expand Down