-
Notifications
You must be signed in to change notification settings - Fork 86
feat: Content Provenance experiment (C2PA 2.3 §A.7 text authentication) #294
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
Open
erik-sv
wants to merge
13
commits into
WordPress:develop
Choose a base branch
from
erik-sv:feature/content-provenance-experiment
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9758181
feat: Content Provenance experiment (C2PA 2.3 text authentication)
227e5cd
fix: align hook names and coding standards with 0.6.0 API
0831a75
fix: clean up REST server and Experiments filter in test tearDown
cb2ce21
feat: implement native C2PA signing with JUMBF/COSE/ES256
2180a3b
feat: add C2PA ingredient chains and fix sidebar JS naming
40706ab
fix: make COSE_Sign1 test signature extraction robust across PHP vers…
146bcea
feat: address PR review feedback with COSE signature verification
95f1707
feat: upgrade C2PA claim structure from v1 to v2
2e34377
Update src/experiments/content-provenance/index.js
erik-sv 781b3b1
Update src/experiments/content-provenance/index.js
erik-sv 6b27cc6
Update src/experiments/content-provenance/index.js
erik-sv d2414b3
feat: align Connected_Signer with Encypher API and ensure C2PA text s…
be18c2f
Merge branch 'develop' into feature/content-provenance-experiment
jeffpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| <?php | ||
| /** | ||
| * C2PA Sign WordPress Ability. | ||
| * | ||
| * @package WordPress\AI | ||
| */ | ||
|
|
||
| declare( strict_types=1 ); | ||
|
|
||
| namespace WordPress\AI\Abilities\Content_Provenance; | ||
|
|
||
| use WP_Error; | ||
| use WordPress\AI\Abstracts\Abstract_Ability; | ||
| use WordPress\AI\Experiments\Content_Provenance\C2PA_Manifest_Builder; | ||
| use WordPress\AI\Experiments\Content_Provenance\Signing\Local_Signer; | ||
| use WordPress\AI\Experiments\Content_Provenance\Unicode_Embedder; | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * C2PA Sign Ability. | ||
| * | ||
| * Registers `c2pa/sign` as a callable ability. Other experiments and plugins call: | ||
| * wp_do_ability( 'c2pa/sign', [ 'text' => '...', 'metadata' => [...] ] ) | ||
| * | ||
| * Returns the signed text (with embedded Unicode provenance) on success. | ||
| * | ||
| * @since x.x.x | ||
| */ | ||
| class C2PA_Sign extends Abstract_Ability { | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @return array<string, mixed> The input schema of the ability. | ||
| */ | ||
| protected function input_schema(): array { | ||
| return array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'post_id' => array( | ||
| 'type' => 'integer', | ||
| 'sanitize_callback' => 'absint', | ||
| 'description' => esc_html__( 'Post ID to sign. When provided, text is read from the post and the signed content is persisted.', 'ai' ), | ||
| ), | ||
| 'text' => array( | ||
| 'type' => 'string', | ||
| 'sanitize_callback' => 'sanitize_text_field', | ||
| 'description' => esc_html__( 'Text content to sign. Used when post_id is not provided.', 'ai' ), | ||
| ), | ||
| 'action' => array( | ||
| 'type' => 'string', | ||
| 'enum' => array( 'c2pa.created', 'c2pa.edited' ), | ||
| 'sanitize_callback' => 'sanitize_text_field', | ||
| 'description' => esc_html__( 'C2PA action type.', 'ai' ), | ||
| ), | ||
| 'metadata' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'title' => array( 'type' => 'string' ), | ||
| 'url' => array( 'type' => 'string' ), | ||
| 'author' => array( 'type' => 'string' ), | ||
| 'post_id' => array( 'type' => 'integer' ), | ||
| ), | ||
| 'description' => esc_html__( 'Post metadata: title, url, author, post_id.', 'ai' ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @return array<string, mixed> The output schema of the ability. | ||
| */ | ||
| protected function output_schema(): array { | ||
| return array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'signed_text' => array( | ||
| 'type' => 'string', | ||
| 'description' => esc_html__( 'Text with embedded C2PA provenance.', 'ai' ), | ||
| ), | ||
| 'manifest' => array( | ||
| 'type' => 'string', | ||
| 'description' => esc_html__( 'JUMBF manifest store bytes.', 'ai' ), | ||
| ), | ||
| 'signer_tier' => array( | ||
| 'type' => 'string', | ||
| 'enum' => array( 'local', 'connected', 'byok' ), | ||
| 'description' => esc_html__( 'Signing tier used.', 'ai' ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param mixed $input The input arguments to the ability. | ||
| * @return array{signed_text: string, manifest: string, signer_tier: string}|\WP_Error | ||
| */ | ||
| protected function execute_callback( $input ) { | ||
| $args = wp_parse_args( | ||
| is_array( $input ) ? $input : array(), | ||
| array( | ||
| 'post_id' => 0, | ||
| 'text' => '', | ||
| 'action' => 'c2pa.created', | ||
| 'metadata' => array(), | ||
| ) | ||
| ); | ||
|
|
||
| $post_id = (int) $args['post_id']; | ||
|
|
||
| // Post mode: sign a post by ID, persist the result. | ||
| if ( $post_id > 0 ) { | ||
| return $this->sign_post_by_id( $post_id ); | ||
| } | ||
|
|
||
| // Text mode: sign raw text and return the result without persisting. | ||
| if ( empty( trim( $args['text'] ) ) ) { | ||
| return new WP_Error( 'c2pa_empty_text', esc_html__( 'Text or post_id is required to sign.', 'ai' ) ); | ||
| } | ||
|
|
||
| $experiment = $this->get_experiment(); | ||
| $signer = $experiment ? $experiment->get_public_signer() : $this->make_local_signer(); | ||
|
|
||
| $result = C2PA_Manifest_Builder::build( | ||
| $args['text'], | ||
| $args['action'], | ||
| null, | ||
| is_array( $args['metadata'] ) ? $args['metadata'] : array(), | ||
| $signer | ||
| ); | ||
|
|
||
| if ( is_wp_error( $result ) ) { | ||
| return $result; | ||
| } | ||
|
|
||
| $signed_text = Unicode_Embedder::embed( $args['text'], $result['manifest'] ); | ||
|
|
||
| return array( | ||
| 'signed_text' => $signed_text, | ||
| 'manifest' => $result['manifest'], | ||
| 'signer_tier' => $signer->get_tier(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Signs a post by ID using the experiment's sign_post() flow. | ||
| * | ||
| * Reads the post, delegates to the experiment for signing and persistence, | ||
| * and returns the result in the standard output format. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param int $post_id The post ID to sign. | ||
| * @return array{signed_text: string, manifest: string, signer_tier: string}|\WP_Error | ||
| */ | ||
| private function sign_post_by_id( int $post_id ) { | ||
| $post = get_post( $post_id ); | ||
|
|
||
| if ( ! $post ) { | ||
| return new WP_Error( 'c2pa_post_not_found', esc_html__( 'Post not found.', 'ai' ) ); | ||
| } | ||
|
|
||
| $experiment = $this->get_experiment(); | ||
|
|
||
| if ( ! $experiment ) { | ||
| return new WP_Error( 'c2pa_experiment_unavailable', esc_html__( 'Content Provenance experiment is not active.', 'ai' ) ); | ||
| } | ||
|
|
||
| $success = $experiment->sign_post( $post_id, $post, 'c2pa.created' ); | ||
|
|
||
| if ( ! $success ) { | ||
| return new WP_Error( 'c2pa_signing_failed', esc_html__( 'Signing failed. Post content may be empty.', 'ai' ) ); | ||
| } | ||
|
|
||
| $updated_post = get_post( $post_id ); | ||
|
|
||
| return array( | ||
| 'signed_text' => $updated_post ? $updated_post->post_content : '', | ||
| 'manifest' => (string) get_post_meta( $post_id, '_c2pa_manifest', true ), | ||
| 'signer_tier' => (string) get_post_meta( $post_id, '_c2pa_signer_tier', true ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param mixed $input The input arguments to the ability. | ||
| * @return bool True if the user has permission. | ||
| */ | ||
| protected function permission_callback( $input ): bool { | ||
| $args = is_array( $input ) ? $input : array(); | ||
|
|
||
| if ( ! empty( $args['post_id'] ) ) { | ||
| return current_user_can( 'edit_post', (int) $args['post_id'] ); | ||
| } | ||
|
|
||
| return current_user_can( 'edit_posts' ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @return array<string, mixed> The meta of the ability. | ||
| */ | ||
| protected function meta(): array { | ||
| return array( 'show_in_rest' => true ); | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to get the Content_Provenance experiment instance from the registry. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @return \WordPress\AI\Experiments\Content_Provenance\Content_Provenance|null | ||
| */ | ||
| private function get_experiment(): ?\WordPress\AI\Experiments\Content_Provenance\Content_Provenance { | ||
| return apply_filters( 'wpai_content_provenance_experiment_instance', null ); | ||
| } | ||
|
|
||
| /** | ||
| * Build a fallback local signer using the stored keypair. | ||
| * | ||
| * Generates a new EC P-256 keypair if none exists. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @return \WordPress\AI\Experiments\Content_Provenance\Signing\Local_Signer | ||
| */ | ||
| private function make_local_signer(): Local_Signer { | ||
| $keypair = get_option( '_c2pa_local_keypair', array() ); | ||
|
|
||
| if ( ! is_array( $keypair ) || empty( $keypair['private_key'] ) || empty( $keypair['certificate_pem'] ) ) { | ||
| $keypair = Local_Signer::generate_keypair(); | ||
|
|
||
| if ( is_wp_error( $keypair ) ) { | ||
| return new Local_Signer( | ||
| array( | ||
| 'private_key' => '', | ||
| 'certificate_pem' => '', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| update_option( '_c2pa_local_keypair', $keypair, false ); | ||
| } | ||
|
|
||
| /** @var array{private_key: string, certificate_pem: string} $keypair */ | ||
| return new Local_Signer( $keypair ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.