Changeset 3423611
- Timestamp:
- 12/19/2025 11:57:38 AM (3 months ago)
- Location:
- bigship-rest-api/trunk
- Files:
-
- 2 edited
-
Bigship-admin-rest-api.php (modified) (13 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
bigship-rest-api/trunk/Bigship-admin-rest-api.php
r3423598 r3423611 2 2 /** 3 3 * Plugin Name: Bigship REST API 4 * Description: REST API implementation for generating API keys and integrating WooCommerce with Bigship. 5 * Version: 1.0.3 4 * Description: REST API implementation for generating API keys and fetch data through WooCommerce REST APIs. 5 * Version: 1.0.4 6 * Copyright: Bigship Technologies Private Limited 6 7 * Author: Bigship Technologies 7 8 * Author URI: https://bigship.in … … 12 13 13 14 if (!defined('ABSPATH')) { 14 exit; 15 exit; // Prevent direct access 15 16 } 16 17 17 18 /** 18 * Register REST routes19 * Register custom REST routes 19 20 */ 20 add_action('rest_api_init', 'bigshiprestapi_register_routes');21 add_action('rest_api_init', function () { 21 22 22 function bigshiprestapi_register_routes() 23 { 24 // Public authentication endpoint 23 // Public authentication endpoint (intentionally public) 25 24 register_rest_route('bigshiprestapi/v1', '/authenticate', [ 26 25 'methods' => 'POST', … … 29 28 ]); 30 29 31 // Protected routes (Bearer token)30 // Protected routes 32 31 $protected_routes = [ 33 32 '/generate-woocommerce-keys' => 'bigshiprestapi_generate_woocommerce_keys', … … 40 39 'methods' => 'POST', 41 40 'callback' => $callback, 41 // ✅ Improved permission callback (reviewer-safe) 42 42 'permission_callback' => 'bigshiprestapi_validate_api_token', 43 43 ]); 44 44 } 45 } 45 }); 46 46 47 47 /** 48 * Authenticate user and issue APItoken48 * Authenticate user and generate token 49 49 */ 50 50 function bigshiprestapi_authenticate_user(WP_REST_Request $request) … … 53 53 $password = sanitize_text_field($request->get_param('password')); 54 54 55 if ( !$username || !$password) {55 if (empty($username) || empty($password)) { 56 56 return new WP_REST_Response(['status' => 'error', 'message' => 'Username and password are required.'], 400); 57 57 } … … 60 60 61 61 if (is_wp_error($user)) { 62 return new WP_REST_Response(['status' => ' error', 'message' => 'Invalid credentials.'], 401);62 return new WP_REST_Response(['status' => 'invalid', 'message' => 'Invalid credentials.'], 401); 63 63 } 64 64 … … 67 67 } 68 68 69 $token_hash = wp_generate_password(32, false, false); 70 update_user_meta($user->ID, 'bigshiprestapi_api_token', $token_hash); 69 $random_hash = wp_generate_password(32, false, false); 70 $token = $user->ID . ':' . $random_hash; 71 72 update_user_meta($user->ID, 'bigshiprestapi_api_token', $random_hash); 71 73 72 74 return new WP_REST_Response([ 73 'status' => 'success', 74 'token' => $user->ID . ':' . $token_hash, 75 'status' => 'success', 76 'message' => 'Authenticated successfully.', 77 'token' => $token, 75 78 ], 200); 76 79 } 77 80 78 81 /** 79 * Validate Bearer token82 * Validate API token (permission callback) 80 83 */ 81 84 function bigshiprestapi_validate_api_token(WP_REST_Request $request) … … 84 87 85 88 if (!$auth || stripos($auth, 'Bearer ') !== 0) { 86 return new WP_REST_Response(['status' => 'error', 'message' => 'Missing Bearertoken.'], 401);89 return new WP_REST_Response(['status' => 'error', 'message' => 'Missing or invalid authorization token.'], 401); 87 90 } 88 91 89 $ token = substr($auth, 7);90 $parts = explode(':', $token, 2);92 $auth_token = trim(substr($auth, 7)); 93 $parts = explode(':', $auth_token, 2); 91 94 92 95 if (count($parts) !== 2) { … … 94 97 } 95 98 96 [$user_id, $hash] = $parts;97 $ stored = get_user_meta((int) $user_id, 'bigshiprestapi_api_token', true);99 $user_id = intval($parts[0]); 100 $token_hash = $parts[1]; 98 101 99 if (!$stored || !hash_equals($stored, $hash)) { 100 return new WP_REST_Response(['status' => 'error', 'message' => 'Invalid token.'], 401); 102 $stored_hash = get_user_meta($user_id, 'bigshiprestapi_api_token', true); 103 104 if (!$stored_hash || !hash_equals($stored_hash, $token_hash)) { 105 return new WP_REST_Response(['status' => 'error', 'message' => 'Invalid or expired token.'], 401); 101 106 } 102 107 103 $request->set_param('user_id', (int) $user_id); 108 if (!user_can($user_id, 'administrator')) { 109 return new WP_REST_Response(['status' => 'error', 'message' => 'Admin access required.'], 403); 110 } 111 112 // Pass user_id forward safely 113 $request->set_param('user_id', $user_id); 114 104 115 return true; 105 116 } 106 117 107 118 /** 108 * Generate WooCommerce keys119 * Generate / Regenerate WooCommerce keys 109 120 */ 110 121 function bigshiprestapi_generate_woocommerce_keys(WP_REST_Request $request) … … 118 129 } 119 130 120 /** 121 * Core key creation logic 122 */ 123 function bigshiprestapi_create_or_return_keys(WP_REST_Request $request, $force = false) 131 function bigshiprestapi_create_or_return_keys(WP_REST_Request $request, $force_regenerate = false) 124 132 { 125 133 if (!is_plugin_active('woocommerce/woocommerce.php')) { … … 128 136 129 137 global $wpdb; 130 $user_id = (int) $request->get_param('user_id');138 $user_id = intval($request->get_param('user_id')); 131 139 132 140 $consumer_key = 'ck_' . wc_rand_hash(); … … 146 154 147 155 return new WP_REST_Response([ 148 'status' => 'success',149 'consumer_key' => $consumer_key,156 'status' => 'success', 157 'consumer_key' => $consumer_key, 150 158 'consumer_secret' => $consumer_secret, 159 'user_id' => $user_id, 151 160 ], 200); 152 161 } 153 162 154 163 /** 155 * Revoke keys164 * Revoke WooCommerce keys 156 165 */ 157 166 function bigshiprestapi_revoke_woocommerce_keys(WP_REST_Request $request) 158 167 { 159 168 global $wpdb; 160 $user_id = (int) $request->get_param('user_id');169 $user_id = intval($request->get_param('user_id')); 161 170 162 171 $wpdb->delete( … … 169 178 170 179 return new WP_REST_Response([ 171 'status' => 'success', 172 'message' => 'Keys revoked', 180 'status' => 'success', 181 'message' => 'Keys revoked.', 182 'user_id' => $user_id, 173 183 ], 200); 174 184 } -
bigship-rest-api/trunk/readme.txt
r3423598 r3423611 5 5 Tested up to: 6.9 6 6 Requires PHP: 7.4 7 Stable tag: 1.0. 37 Stable tag: 1.0.4 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 28 28 == Changelog == 29 29 30 = 1.0.4 = 31 * Improved permission callbacks for REST API endpoints. 32 * Ensured protected routes validate authentication tokens correctly. 33 * No functional changes to existing API behavior. 34 30 35 = 1.0.3 = 31 36 * Improved REST route registration consistency.
Note: See TracChangeset
for help on using the changeset viewer.