Refactor IGNY8 Bridge to use API key authentication exclusively
- Removed email/password authentication and related settings from the plugin. - Updated API connection logic to utilize only the API key for authentication. - Simplified the admin interface by removing webhook-related settings and messages. - Enhanced the settings page with improved UI and status indicators for API connection. - Added a new REST API endpoint to check plugin status and connection health. - Updated styles for a modernized look and feel across the admin interface.
This commit is contained in:
@@ -26,41 +26,31 @@ class Igny8API {
|
||||
private $base_url = 'https://api.igny8.com/api/v1';
|
||||
|
||||
/**
|
||||
* Access token
|
||||
* API key (used as access token)
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $access_token = null;
|
||||
|
||||
/**
|
||||
* Whether authentication is via API key (true) or tokens (false)
|
||||
* Whether authentication is via API key (always true now)
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $api_key_auth = false;
|
||||
|
||||
/**
|
||||
* Refresh token
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $refresh_token = null;
|
||||
private $api_key_auth = true;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Only uses API key for authentication
|
||||
*/
|
||||
public function __construct() {
|
||||
if (function_exists('igny8_get_secure_option')) {
|
||||
$this->access_token = igny8_get_secure_option('igny8_access_token');
|
||||
$this->refresh_token = igny8_get_secure_option('igny8_refresh_token');
|
||||
$api_key = igny8_get_secure_option('igny8_api_key');
|
||||
} else {
|
||||
$this->access_token = get_option('igny8_access_token');
|
||||
$this->refresh_token = get_option('igny8_refresh_token');
|
||||
$api_key = get_option('igny8_api_key');
|
||||
}
|
||||
|
||||
// If an API key is provided, prefer it as the access token
|
||||
// API key is the only authentication method
|
||||
if (!empty($api_key)) {
|
||||
$this->access_token = $api_key;
|
||||
$this->api_key_auth = true;
|
||||
@@ -68,96 +58,32 @@ class Igny8API {
|
||||
}
|
||||
|
||||
/**
|
||||
* Login and store tokens
|
||||
* Connect using API key
|
||||
*
|
||||
* @param string $email User email
|
||||
* @param string $password User password
|
||||
* @param string $api_key API key from IGNY8 app
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function login($email, $password) {
|
||||
$response = wp_remote_post($this->base_url . '/auth/login/', array(
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json'
|
||||
),
|
||||
'body' => json_encode(array(
|
||||
'email' => $email,
|
||||
'password' => $password
|
||||
)),
|
||||
'timeout' => 30
|
||||
));
|
||||
|
||||
$body = $this->parse_response($response);
|
||||
|
||||
if ($body['success']) {
|
||||
$this->access_token = $body['data']['access'];
|
||||
$this->refresh_token = $body['data']['refresh'];
|
||||
|
||||
if (function_exists('igny8_store_secure_option')) {
|
||||
igny8_store_secure_option('igny8_access_token', $this->access_token);
|
||||
igny8_store_secure_option('igny8_refresh_token', $this->refresh_token);
|
||||
} else {
|
||||
update_option('igny8_access_token', $this->access_token);
|
||||
update_option('igny8_refresh_token', $this->refresh_token);
|
||||
}
|
||||
update_option('igny8_email', $email);
|
||||
|
||||
// Store token refresh time
|
||||
$timestamp = current_time('timestamp');
|
||||
update_option('igny8_token_refreshed_at', $timestamp);
|
||||
update_option('igny8_access_token_issued', $timestamp);
|
||||
update_option('igny8_last_api_health_check', $timestamp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh access token
|
||||
*
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function refresh_token() {
|
||||
if (!$this->refresh_token) {
|
||||
public function connect($api_key) {
|
||||
if (empty($api_key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$response = wp_remote_post($this->base_url . '/auth/refresh/', array(
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json'
|
||||
),
|
||||
'body' => json_encode(array(
|
||||
'refresh' => $this->refresh_token
|
||||
)),
|
||||
'timeout' => 30
|
||||
));
|
||||
// Store API key
|
||||
if (function_exists('igny8_store_secure_option')) {
|
||||
igny8_store_secure_option('igny8_api_key', $api_key);
|
||||
} else {
|
||||
update_option('igny8_api_key', $api_key);
|
||||
}
|
||||
|
||||
$body = $this->parse_response($response);
|
||||
$this->access_token = $api_key;
|
||||
$this->api_key_auth = true;
|
||||
|
||||
if ($body['success']) {
|
||||
$this->access_token = $body['data']['access'];
|
||||
|
||||
// Refresh token may be updated
|
||||
if (isset($body['data']['refresh'])) {
|
||||
$this->refresh_token = $body['data']['refresh'];
|
||||
if (function_exists('igny8_store_secure_option')) {
|
||||
igny8_store_secure_option('igny8_refresh_token', $this->refresh_token);
|
||||
} else {
|
||||
update_option('igny8_refresh_token', $this->refresh_token);
|
||||
}
|
||||
}
|
||||
|
||||
if (function_exists('igny8_store_secure_option')) {
|
||||
igny8_store_secure_option('igny8_access_token', $this->access_token);
|
||||
} else {
|
||||
update_option('igny8_access_token', $this->access_token);
|
||||
}
|
||||
|
||||
// Test connection by making a simple API call
|
||||
$test_response = $this->get('/auth/sites/');
|
||||
|
||||
if ($test_response['success']) {
|
||||
$timestamp = current_time('timestamp');
|
||||
update_option('igny8_token_refreshed_at', $timestamp);
|
||||
update_option('igny8_access_token_issued', $timestamp);
|
||||
|
||||
update_option('igny8_last_api_health_check', $timestamp);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -271,18 +197,8 @@ class Igny8API {
|
||||
|
||||
$body = $this->parse_response($response);
|
||||
|
||||
// Handle 401 - token expired
|
||||
if (!$body['success'] && wp_remote_retrieve_response_code($response) == 401 && !$this->api_key_auth) {
|
||||
// Try to refresh token (only for email/password auth, not API key)
|
||||
if ($this->refresh_token()) {
|
||||
// Retry request
|
||||
$response = wp_remote_get($url, array(
|
||||
'headers' => $this->get_headers(),
|
||||
'timeout' => 30
|
||||
));
|
||||
$body = $this->parse_response($response);
|
||||
}
|
||||
}
|
||||
// API keys don't expire, so no refresh logic needed
|
||||
// If 401, the API key is invalid or revoked
|
||||
|
||||
return $body;
|
||||
}
|
||||
@@ -303,19 +219,7 @@ class Igny8API {
|
||||
|
||||
$body = $this->parse_response($response);
|
||||
|
||||
// Handle 401 - token expired
|
||||
if (!$body['success'] && wp_remote_retrieve_response_code($response) == 401) {
|
||||
// Try to refresh token
|
||||
if ($this->refresh_token()) {
|
||||
// Retry request
|
||||
$response = wp_remote_post($this->base_url . $endpoint, array(
|
||||
'headers' => $this->get_headers(),
|
||||
'body' => json_encode($data),
|
||||
'timeout' => 60
|
||||
));
|
||||
$body = $this->parse_response($response);
|
||||
}
|
||||
}
|
||||
// API keys don't expire, so no refresh logic needed
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user