-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordpress_plugin.php
More file actions
285 lines (238 loc) · 9.36 KB
/
wordpress_plugin.php
File metadata and controls
285 lines (238 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Plugin Name: AuthentiChip Integration
* Description: Validates AuthentiChip JWT tokens and provides action hooks for integration
* Version: 1.0.0
* Author: Your Name
* License: CC0 1.0 Universal
*
* Installation:
* 1. Create directory: wp-content/plugins/authentichip/
* 2. Copy this file to: wp-content/plugins/authentichip/wordpress_plugin.php
* 3. Install dependencies: Run `composer require firebase/php-jwt` in the plugin directory
* 4. Activate the plugin in WordPress admin
*
* Usage in your theme or another plugin:
*
* // Handle verified scans
* add_action('authentichip_verified', function($chipId, $uid) {
* error_log("Verified chip: " . $chipId . " (UID: " . $uid . ")");
* // Look up product, grant access, etc.
* }, 10, 2);
*
* // Handle unverified scans
* add_action('authentichip_unverified', function($uid, $status) {
* error_log("Unverified scan - UID: $uid, Status: $status");
* // Log attempt, show limited info, etc.
* }, 10, 2);
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Load Composer dependencies
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
add_action('admin_notices', function() {
echo '<div class="error"><p>AuthentiChip: Please run <code>composer install</code> in the plugin directory.</p></div>';
});
return;
}
use Firebase\JWT\JWT;
use Firebase\JWT\JWK;
use Firebase\JWT\ExpiredException;
use Firebase\JWT\SignatureInvalidException;
class AuthentiChip_Integration {
private $jwks_cache_key = 'authentichip_jwks';
private $jwks_cache_expiry = 21600; // 6 hours
public function __construct() {
// Hook into WordPress init
add_action('init', [$this, 'check_authentication']);
// Add shortcode for displaying chip info
add_shortcode('authentichip_info', [$this, 'shortcode_chip_info']);
// Add REST API endpoint
add_action('rest_api_init', [$this, 'register_rest_routes']);
}
/**
* Check for AuthentiChip parameters on every page load
*/
public function check_authentication() {
$jwt = isset($_GET['vkjwt']) ? sanitize_text_field($_GET['vkjwt']) : null;
$status = isset($_GET['vkstatus']) ? sanitize_text_field($_GET['vkstatus']) : null;
$uid = isset($_GET['vkuid']) ? sanitize_text_field($_GET['vkuid']) : null;
if ($jwt) {
try {
$result = $this->validate_jwt($jwt);
$chipId = $result['chipId'];
$uid = $result['uid'];
// Store in session for later use
if (!session_id()) {
session_start();
}
$_SESSION['authentichip_id'] = $chipId;
$_SESSION['authentichip_uid'] = $uid;
$_SESSION['authentichip_verified'] = true;
$_SESSION['authentichip_timestamp'] = time();
// Fire action hook for other plugins/themes (pass both chipId and uid)
do_action('authentichip_verified', $chipId, $uid);
// Optional: Store in user meta if logged in
if (is_user_logged_in()) {
update_user_meta(get_current_user_id(), 'last_chip_scan', [
'chip_id' => $chipId,
'uid' => $uid,
'timestamp' => time(),
'verified' => true,
]);
}
} catch (Exception $e) {
// Log validation failure
error_log('AuthentiChip validation failed: ' . $e->getMessage());
do_action('authentichip_validation_failed', $e->getMessage());
}
} elseif ($status && $uid) {
// Unverified scan
if (!session_id()) {
session_start();
}
$_SESSION['authentichip_uid'] = $uid;
$_SESSION['authentichip_status'] = $status;
$_SESSION['authentichip_verified'] = false;
$_SESSION['authentichip_timestamp'] = time();
do_action('authentichip_unverified', $uid, $status);
if (is_user_logged_in()) {
update_user_meta(get_current_user_id(), 'last_chip_scan', [
'uid' => $uid,
'status' => $status,
'timestamp' => time(),
'verified' => false,
]);
}
}
}
/**
* Validate JWT and extract chip ID and UID
*
* @return array Associative array with 'chipId' and 'uid'
*/
private function validate_jwt($jwt) {
// Get cached JWKS or fetch fresh
$jwks = get_transient($this->jwks_cache_key);
if (false === $jwks) {
$response = wp_remote_get('https://auth.vivokey.com/.well-known/jwks.json', [
'timeout' => 10,
]);
if (is_wp_error($response)) {
throw new Exception('Unable to fetch JWKS: ' . $response->get_error_message());
}
$body = wp_remote_retrieve_body($response);
$jwks = json_decode($body, true);
if (!$jwks || !isset($jwks['keys'])) {
throw new Exception('Invalid JWKS response');
}
// Cache the JWKS
set_transient($this->jwks_cache_key, $jwks, $this->jwks_cache_expiry);
}
// Parse and validate
$keys = JWK::parseKeySet($jwks);
$decoded = JWT::decode($jwt, $keys);
// Verify issuer
if (!isset($decoded->iss) || $decoded->iss !== 'auth.vivokey.com') {
throw new Exception('Invalid issuer');
}
// Extract chip ID
if (!isset($decoded->sub) || empty($decoded->sub)) {
throw new Exception('Missing chip ID');
}
$chipId = $decoded->sub;
// Validate chip ID format (SHA-256 hash - 64 hex characters)
if (!preg_match('/^[0-9a-f]{64}$/i', $chipId)) {
throw new Exception('Invalid chip ID format - expected SHA-256 hash');
}
// Validate product claim (must be 6 for AuthentiChip)
if (!isset($decoded->product) || $decoded->product !== 6) {
throw new Exception('Invalid product claim - expected product=6 for AuthentiChip');
}
// Validate audience claim exists
if (!isset($decoded->aud) || empty($decoded->aud)) {
throw new Exception('Missing audience (aud) claim in JWT');
}
// Extract UID from client data claim
if (!isset($decoded->cld) || empty($decoded->cld)) {
throw new Exception('Missing client data (cld) claim');
}
// Parse cld as JSON string
$cldData = json_decode($decoded->cld);
if ($cldData === null || !isset($cldData->uid) || empty($cldData->uid)) {
throw new Exception('Missing uid in client data (cld) claim');
}
$uid = $cldData->uid;
return [
'chipId' => $chipId,
'uid' => $uid
];
}
/**
* Shortcode to display chip information
* Usage: [authentichip_info]
*/
public function shortcode_chip_info($atts) {
if (!session_id()) {
session_start();
}
$verified = $_SESSION['authentichip_verified'] ?? false;
if ($verified) {
$chipId = $_SESSION['authentichip_id'] ?? 'Unknown';
$uid = $_SESSION['authentichip_uid'] ?? 'Unknown';
$output = '<div class="authentichip-verified">';
$output .= '<p><strong>Verified Chip</strong></p>';
$output .= '<p>Chip ID: ' . esc_html($chipId) . '</p>';
$output .= '<p>UID: ' . esc_html($uid) . '</p>';
$output .= '</div>';
} else {
$status = $_SESSION['authentichip_status'] ?? null;
$uid = $_SESSION['authentichip_uid'] ?? null;
if ($status && $uid) {
$output = '<div class="authentichip-unverified">';
$output .= '<p><strong>Unverified Scan</strong></p>';
$output .= '<p>Status: ' . esc_html($status) . '</p>';
$output .= '<p>UID: ' . esc_html($uid) . '</p>';
$output .= '</div>';
} else {
$output = '<p>No chip scan detected.</p>';
}
}
return $output;
}
/**
* Register REST API endpoints
*/
public function register_rest_routes() {
register_rest_route('authentichip/v1', '/verify', [
'methods' => 'GET',
'callback' => [$this, 'rest_verify'],
'permission_callback' => '__return_true',
]);
}
/**
* REST API endpoint for external verification
* GET /wp-json/authentichip/v1/verify?vkjwt=...
*/
public function rest_verify($request) {
$jwt = $request->get_param('vkjwt');
if (!$jwt) {
return new WP_Error('no_jwt', 'No JWT provided', ['status' => 400]);
}
try {
$result = $this->validate_jwt($jwt);
return [
'verified' => true,
'chip_id' => $result['chipId'],
'uid' => $result['uid'],
];
} catch (Exception $e) {
return new WP_Error('validation_failed', $e->getMessage(), ['status' => 401]);
}
}
}
// Initialize the plugin
new AuthentiChip_Integration();