Yoast\WP\SEO\AI\Authentication\Application
OAuth_Auth_Strategy::send
Acquires a site token, dispatches via MyYoast_Client::authenticated_request, and translates the response.
The WP user is identified to yoast-ai on every call because the site-level OAuth token is shared across users. POST requests carry user_id in the body; GET requests carry it as a query parameter.
Метод класса: OAuth_Auth_Strategy{}
Хуков нет.
Возвращает
Response. The parsed response.
Использование
$OAuth_Auth_Strategy = new OAuth_Auth_Strategy(); $OAuth_Auth_Strategy->send( $request, $user ): Response;
Код OAuth_Auth_Strategy::send() OAuth Auth Strategy::send Yoast 28.9
public function send( Request $request, WP_User $user ): Response {
$resource = $this->api_client->get_resource_url();
try {
$token_set = $this->myyoast_client->get_site_token( [ self::AI_SCOPE ], $resource );
} catch ( Token_Request_Failed_Exception | Token_Storage_Exception $exception ) {
$this->logger->warning( 'OAuth send: site token unavailable ({error}); surfacing as OAUTH_TOKEN_UNAVAILABLE.', [ 'error' => $exception->getMessage() ] );
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception data, not output.
throw new Auth_Strategy_Unavailable_Exception( 'OAUTH_TOKEN_UNAVAILABLE', 0, 'OAUTH_TOKEN_UNAVAILABLE', $exception );
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
$method = $request->get_http_method();
$url = $this->api_client->get_url( $request->get_action_path() );
$user_id = (string) $user->ID;
$options = [
'headers' => \array_merge( $request->get_headers(), [ 'Content-Type' => 'application/json' ] ),
'timeout' => $this->api_client->get_request_timeout(),
];
if ( $method === Request::METHOD_POST ) {
$body = \array_merge( ( $request->get_body() ?? [] ), [ 'user_id' => $user_id ] );
$options['body'] = WPSEO_Utils::format_json_encode( $body );
}
else {
$url = \add_query_arg( [ 'user_id' => $user_id ], $url );
}
$http_response = $this->myyoast_client->authenticated_request( $method, $url, $token_set, $options );
if ( $http_response->is_transport_failure() ) {
$this->logger->warning( 'OAuth send: transport failure reaching yoast-ai; surfacing as WP_HTTP_REQUEST_ERROR.' );
throw new WP_Request_Exception( \esc_html( $http_response->get_body_value( 'error_description', '' ) ) );
}
try {
return $this->response_validator->assert_success( $this->to_response( $http_response ) );
} catch ( Unauthorized_Exception $exception ) {
$error_code = $this->error_code( $exception );
// Only drop the cached token when the challenge says the token itself is the problem
// (`invalid_token`). A replayed DPoP proof (`invalid_dpop_proof`) or any other 401 leaves
// a still-valid token in place — discarding it would force a needless re-issue, and the
// retry must come with a fresh DPoP proof, not a fresh token.
if ( $error_code === 'invalid_token' ) {
$this->logger->debug( 'OAuth send: 401 invalid_token from yoast-ai; clearing cached site token before rethrowing.' );
$this->myyoast_client->clear_site_token( $resource );
}
else {
$this->logger->warning(
'OAuth send: 401 from yoast-ai ({error_code}: {message}); keeping cached site token.',
[
'error_code' => $this->error_label( $error_code ),
'message' => $exception->getMessage(),
],
);
}
throw $exception;
} catch ( Forbidden_Exception $exception ) {
if ( $this->is_insufficient_scope( $exception ) ) {
$this->logger->warning( 'OAuth send: yoast-ai returned insufficient_scope.' );
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
throw new Insufficient_Scope_Exception(
'INSUFFICIENT_SCOPE',
$exception->getCode(),
'INSUFFICIENT_SCOPE',
$exception,
$exception->get_response_headers(),
);
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
if ( $this->is_consent_required( $exception ) ) {
$this->logger->warning( 'OAuth send: yoast-ai requires user consent.' );
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
throw new Consent_Required_Exception(
'CONSENT_REQUIRED',
$exception->getCode(),
'CONSENT_REQUIRED',
$exception,
$exception->get_response_headers(),
);
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
throw $exception;
} catch ( Remote_Request_Exception $exception ) {
$this->logger->warning(
'OAuth send: remote request failed ({error_code}: {message}); rethrowing.',
[
'error_code' => $this->error_label( $this->error_code( $exception ) ),
'message' => $exception->getMessage(),
],
);
throw $exception;
}
}