Skip to content
Open
Show file tree
Hide file tree
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
121 changes: 120 additions & 1 deletion openid4vci-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Universal OID4VCI
* Description: Issue verifiable credentials using the universal OID4VCI interface with an organization wallet.
* Version: 0.4.0
* Version: 0.4.9
* Requires at least: 6.6
* Requires PHP: 7.2
* Author: Credenco B.V.
Expand Down Expand Up @@ -44,6 +44,7 @@
function create_block_openid4vci_block_init() {
register_block_type( __DIR__ . '/build/credentialIssue' );
register_block_type( __DIR__ . '/build/credentialIssueOrgWallet' );
register_block_type( __DIR__ . '/build/credentialIssueRaw' );
if(!session_id()) {
session_start();
}
Expand All @@ -53,6 +54,57 @@ function create_block_openid4vci_block_init() {
// Add an action to call our script enqueuing function
//add_action( 'wp_enqueue_script', 'enqueue_my_scripts' );

/**
* Probeert de status_uri van een issuance te pollen voor een voltooide credential.
*
* @param string $statusUri
* @param string $authenticationHeaderName
* @param string $authenticationToken
* @param int $maxAttempts
* @param int $delaySeconds
*
* @return object|null
*/
function openid4vci_fetch_status_result( $statusUri, $authenticationHeaderName, $authenticationToken, $maxAttempts = 3, $delaySeconds = 1 ) {
if ( empty( $statusUri ) ) {
return null;
}

for ( $attempt = 1; $attempt <= $maxAttempts; $attempt ++ ) {
$statusResponse = wp_remote_get( $statusUri, array(
'headers' => array(
'Accept' => 'application/json',
$authenticationHeaderName => $authenticationToken,
),
'timeout' => 20,
'redirection' => 5,
'blocking' => true,
) );

if ( is_wp_error( $statusResponse ) ) {
error_log( '[openid4vci] status poll fout poging ' . $attempt . ': ' . $statusResponse->get_error_message() );
} else {
$statusBody = wp_remote_retrieve_body( $statusResponse );
$statusResult = json_decode( $statusBody );

if ( json_last_error() === JSON_ERROR_NONE ) {
if ( isset( $statusResult->credential ) ) {
return $statusResult;
}
error_log( '[openid4vci] status poll poging ' . $attempt . ' zonder credential: ' . $statusBody );
} else {
error_log( '[openid4vci] status poll JSON fout poging ' . $attempt . ': ' . json_last_error_msg() );
}
}

if ( $attempt < $maxAttempts ) {
sleep( $delaySeconds );
}
}

return null;
}

function sendVciRequest($claims, $attributes) {
$options = new OpenID4VCI_Admin_Options();
$openidEndpoint = $options->openidEndpoint;
Expand Down Expand Up @@ -119,7 +171,74 @@ function sendVciRequest($claims, $attributes) {
return ["success" => false, "error" => $block_content];
}

if ( ! isset( $result->credential ) && isset( $result->status_uri ) ) {
$statusResult = openid4vci_fetch_status_result(
$result->status_uri,
$authenticationHeaderName,
$authenticationToken
);

if ( $statusResult ) {
$result = $statusResult;
}
}

return ["success" => true, "result" => $result];
}

function sendRawIssueRequest( $claims, $attributes ) {
$options = new OpenID4VCI_Admin_Options();
$openidEndpoint = $options->openidEndpoint;
$authenticationHeaderName = $options->authenticationHeaderName;
$authenticationToken = $options->authenticationToken;

if ( ! empty( $attributes['openidEndpoint'] ) ) {
$openidEndpoint = $attributes['openidEndpoint'];
$authenticationHeaderName = $attributes['authenticationHeaderName'];
$authenticationToken = $attributes['authenticationToken'];
}

$templateId = $attributes['credentialIssueTemplateKey'] ?? '';
if ( empty( $templateId ) ) {
$block_content = '<div ' . get_block_wrapper_attributes() . '><p>Template ID ontbreekt voor raw issue.</p></div>';
return [ 'success' => false, 'error' => $block_content ];
}

$payload = array(
'claims' => $claims,
'template_id' => $templateId,
);

$payload = wp_json_encode( $payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );

$response = wp_remote_post( $openidEndpoint, array(
'headers' => array(
'Content-Type' => 'application/json',
$authenticationHeaderName => $authenticationToken,
),
'timeout' => 45,
'redirection' => 5,
'blocking' => true,
'body' => $payload,
) );

if ( is_wp_error( $response ) ) {
$block_content = '<div ' . get_block_wrapper_attributes() . '><p>Raw issue request mislukt: ' . esc_html( $response->get_error_message() ) . '</p></div>';
return [ 'success' => false, 'error' => $block_content ];
}

$body = wp_remote_retrieve_body( $response );
$result = json_decode( $body );

if ( json_last_error() !== JSON_ERROR_NONE ) {
$block_content = '<div ' . get_block_wrapper_attributes() . '><p>Raw issue JSON fout: ' . json_last_error_msg() . '</p></div>';
return [ 'success' => false, 'error' => $block_content ];
}

if ( isset( $result->status ) && isset( $result->detail ) ) {
$block_content = '<div ' . get_block_wrapper_attributes() . '><p>Raw issue API fout: ' . $result->detail . '</p></div>';
return [ 'success' => false, 'error' => $block_content ];
}

return [ 'success' => true, 'result' => $result ];
}
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: The WordPress Contributors
Tags: block
Tested up to: 6.6
Stable tag: 0.2.0
Stable tag: 0.4.9
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down
56 changes: 56 additions & 0 deletions src/credentialIssueRaw/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "openid4vci-plugin/openid4vc-issue-raw",
"version": "0.1.0",
"title": "OID4VCI – Raw credential issue",
"category": "widgets",
"description": "Generate a raw SD-JWT credential directly",
"example": {},
"attributes": {
"openidEndpoint": {
"type": "string"
},
"authenticationHeaderName": {
"type": "string"
},
"authenticationToken": {
"type": "string"
},
"credentialIssueTemplateKey": {
"type": "string"
},
"credentialData": {
"type": "string"
},
"formData": {
"type": "string"
},
"sessionData": {
"type": "string"
},
"showCredential": {
"type": "boolean",
"default": true
},
"buttonLabel": {
"type": "string",
"default": "Generate credential"
}
},
"supports": {
"color": {
"background": false,
"text": true
},
"html": false,
"typography": {
"fontSize": true
}
},
"textdomain": "openid4vc-issue",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:./render.php"
}
2 changes: 2 additions & 0 deletions src/credentialIssueRaw/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php return array('dependencies' => array('react', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n'), 'version' => 'c38a64d9058b58876565');

2 changes: 2 additions & 0 deletions src/credentialIssueRaw/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading