, * grant_types: array, * token_endpoint_auth_method: string, * software_id: string, * software_version: string, * raw_metadata: array|null, * created_at: string, * last_used_at: string|null * } * * @since 2.8.2 */ final class OAuthClientStore { /** Client registered itself via Dynamic Client Registration. */ public const MODE_DCR = 'dcr'; /** Client identified itself via a Client ID Metadata Document (its id is its own https URL). */ public const MODE_CIMD = 'cimd'; /** Default grant_types stored for a newly registered client. */ private const DEFAULT_GRANT_TYPES = array( 'authorization_code', 'refresh_token' ); /** * Widths, in characters, of the self-reported columns nothing upstream * bounds. WordPress refuses a whole insert or update when one value is * longer than its column, so an over-long value is cut to fit instead. */ private const SOFTWARE_ID_WIDTH = 190; private const SOFTWARE_VERSION_WIDTH = 60; /** @var \wpdb */ private $wpdb; /** @var string */ private $table; /** * @param \wpdb|null $wpdb Defaults to the global $wpdb. */ public function __construct( ?\wpdb $wpdb = null ) { $this->wpdb = $wpdb instanceof \wpdb ? $wpdb : $GLOBALS['wpdb']; $this->table = $this->wpdb->prefix . 'aipt_mcp_oauth_clients'; } /** * Fully-qualified table name. * * @return string */ public function table(): string { return $this->table; } /** * A client_id's lookup hash. Not a secret: client_id is public * (returned at registration and sent on every authorize request); this * only keeps the indexed column at a fixed, safe width. * * @param string $client_id Raw client id. * @return string */ public static function hashClientId( string $client_id ): string { return hash( 'sha256', $client_id ); } /** * Register a new client. * * @param array{client_id:string,registration_mode:string,client_name?:string,redirect_uris:array,grant_types?:array,token_endpoint_auth_method?:string,software_id?:string,software_version?:string,raw_metadata?:array|null,now?:string} $data Client fields. * @return int|null New row id, or null when the input is invalid or the insert failed. */ public function create( array $data ): ?int { $wpdb = $this->wpdb; $client_id = isset( $data['client_id'] ) ? (string) $data['client_id'] : ''; $mode = isset( $data['registration_mode'] ) ? (string) $data['registration_mode'] : ''; if ( '' === $client_id ) { return null; } if ( ! in_array( $mode, array( self::MODE_DCR, self::MODE_CIMD ), true ) ) { return null; } if ( empty( $data['redirect_uris'] ) || ! is_array( $data['redirect_uris'] ) ) { return null; } $now = isset( $data['now'] ) ? (string) $data['now'] : gmdate( 'Y-m-d H:i:s' ); $grant_types = isset( $data['grant_types'] ) && is_array( $data['grant_types'] ) ? $data['grant_types'] : self::DEFAULT_GRANT_TYPES; $raw_metadata = array_key_exists( 'raw_metadata', $data ) && is_array( $data['raw_metadata'] ) ? $data['raw_metadata'] : null; $result = $wpdb->insert( $this->table, array( 'client_id' => $client_id, 'client_id_hash' => self::hashClientId( $client_id ), 'registration_mode' => $mode, 'client_name' => isset( $data['client_name'] ) ? (string) $data['client_name'] : '', 'redirect_uris' => wp_json_encode( array_values( $data['redirect_uris'] ) ), 'grant_types' => wp_json_encode( array_values( $grant_types ) ), 'token_endpoint_auth_method' => isset( $data['token_endpoint_auth_method'] ) ? (string) $data['token_endpoint_auth_method'] : 'none', 'software_id' => self::clamp( $data['software_id'] ?? '', self::SOFTWARE_ID_WIDTH ), 'software_version' => self::clamp( $data['software_version'] ?? '', self::SOFTWARE_VERSION_WIDTH ), 'raw_metadata' => null === $raw_metadata ? null : wp_json_encode( $raw_metadata ), 'created_at' => $now, ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ); if ( false === $result ) { $this->logError( __FUNCTION__ ); return null; } return (int) $wpdb->insert_id; } /** * Look up a client by its raw client_id. * * @param string $client_id Raw client id. * @return array|null * @phpstan-return McpOAuthClientRow|null */ public function findByClientId( string $client_id ): ?array { $wpdb = $this->wpdb; if ( '' === $client_id ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table} WHERE client_id_hash = %s", self::hashClientId( $client_id ) ), ARRAY_A ); if ( is_array( $row ) ) { return $this->mapRow( $row ); } if ( '' !== $wpdb->last_error ) { $this->logError( __FUNCTION__ ); } return null; } /** * Look up a client by its row id. * * @param int $id Row id. * @return array|null * @phpstan-return McpOAuthClientRow|null */ public function find( int $id ): ?array { $wpdb = $this->wpdb; if ( $id <= 0 ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ), ARRAY_A ); if ( is_array( $row ) ) { return $this->mapRow( $row ); } if ( '' !== $wpdb->last_error ) { $this->logError( __FUNCTION__ ); } return null; } /** * Update a client's self-reported metadata (never its client_id or * registration_mode). * * @param int $id Row id. * @param array{client_name?:string,redirect_uris?:array,raw_metadata?:array|null,software_id?:string,software_version?:string} $data Fields to update. * @return bool */ public function updateMetadata( int $id, array $data ): bool { $wpdb = $this->wpdb; if ( $id <= 0 ) { return false; } $fields = array(); $formats = array(); if ( array_key_exists( 'client_name', $data ) ) { $fields['client_name'] = (string) $data['client_name']; $formats[] = '%s'; } if ( array_key_exists( 'redirect_uris', $data ) && is_array( $data['redirect_uris'] ) ) { $fields['redirect_uris'] = wp_json_encode( array_values( $data['redirect_uris'] ) ); $formats[] = '%s'; } if ( array_key_exists( 'raw_metadata', $data ) ) { $fields['raw_metadata'] = is_array( $data['raw_metadata'] ) ? wp_json_encode( $data['raw_metadata'] ) : null; $formats[] = '%s'; } if ( array_key_exists( 'software_id', $data ) ) { $fields['software_id'] = self::clamp( $data['software_id'], self::SOFTWARE_ID_WIDTH ); $formats[] = '%s'; } if ( array_key_exists( 'software_version', $data ) ) { $fields['software_version'] = self::clamp( $data['software_version'], self::SOFTWARE_VERSION_WIDTH ); $formats[] = '%s'; } if ( empty( $fields ) ) { return true; } $result = $wpdb->update( $this->table, $fields, array( 'id' => $id ), $formats, array( '%d' ) ); if ( false === $result ) { $this->logError( __FUNCTION__ ); return false; } return true; } /** * Record that a client was just used. * * @param int $id Row id. * @param string $now Current UTC datetime. * @return void */ public function touchLastUsed( int $id, string $now ): void { $wpdb = $this->wpdb; if ( $id <= 0 ) { return; } $result = $wpdb->update( $this->table, array( 'last_used_at' => $now ), array( 'id' => $id ), array( '%s' ), array( '%d' ) ); if ( false === $result ) { $this->logError( __FUNCTION__ ); } } /** * Delete clients registered before $cutoff that have NEVER had a * connection row (checked via NOT EXISTS against aipt_mcp_connections, * so a client with at least one connection — active, revoked or * disabled — is never removed). * * @param string $cutoff UTC datetime; clients created before this are eligible. * @param int $limit Max rows deleted by this call. * @return int Number of rows deleted; 0 on a database error. */ public function deleteUnusedOlderThan( string $cutoff, int $limit ): int { $wpdb = $this->wpdb; $connections_table = $wpdb->prefix . 'aipt_mcp_connections'; $sql = "DELETE FROM {$this->table} WHERE created_at < %s AND NOT EXISTS (SELECT 1 FROM {$connections_table} c WHERE c.oauth_client_id = {$this->table}.id) LIMIT %d"; $deleted = $wpdb->query( $wpdb->prepare( $sql, $cutoff, max( 1, $limit ) ) ); if ( false === $deleted ) { $this->logError( __FUNCTION__ ); return 0; } return (int) $deleted; } /** * Normalize a raw row: cast integer columns, decode JSON columns. * * @param array $row Raw row from $wpdb. * @return array * @phpstan-return McpOAuthClientRow */ private function mapRow( array $row ): array { $redirect_uris = isset( $row['redirect_uris'] ) ? json_decode( (string) $row['redirect_uris'], true ) : null; $grant_types = isset( $row['grant_types'] ) ? json_decode( (string) $row['grant_types'], true ) : null; $raw_metadata = isset( $row['raw_metadata'] ) && null !== $row['raw_metadata'] ? json_decode( (string) $row['raw_metadata'], true ) : null; return array( 'id' => (int) ( $row['id'] ?? 0 ), 'client_id' => (string) ( $row['client_id'] ?? '' ), 'client_id_hash' => (string) ( $row['client_id_hash'] ?? '' ), 'registration_mode' => (string) ( $row['registration_mode'] ?? '' ), 'client_name' => (string) ( $row['client_name'] ?? '' ), 'redirect_uris' => is_array( $redirect_uris ) ? $redirect_uris : array(), 'grant_types' => is_array( $grant_types ) ? $grant_types : array(), 'token_endpoint_auth_method' => (string) ( $row['token_endpoint_auth_method'] ?? 'none' ), 'software_id' => (string) ( $row['software_id'] ?? '' ), 'software_version' => (string) ( $row['software_version'] ?? '' ), 'raw_metadata' => is_array( $raw_metadata ) ? $raw_metadata : null, 'created_at' => (string) ( $row['created_at'] ?? '' ), 'last_used_at' => isset( $row['last_used_at'] ) ? (string) $row['last_used_at'] : null, ); } /** * Cut by UTF-8 characters, the way the column counts them. WordPress * defines mb_substr() itself when mbstring is missing. * * @param mixed $value Raw field value. * @param int $width Column width in characters. * @return string */ private static function clamp( $value, int $width ): string { return mb_substr( is_scalar( $value ) ? (string) $value : '', 0, $width, 'UTF-8' ); } /** * @param string $method Calling method name, for the log line. * @return void */ private function logError( string $method ): void { $wpdb = $this->wpdb; } }