feat: add admin-controlled authentication debug mode

This commit is contained in:
2026-08-23 16:01:09 +03:30
parent 6419f82ea5
commit 572a740d46
11 changed files with 187 additions and 42 deletions
@@ -4,6 +4,8 @@ namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
use Dabestaniha\AuthenticateBridge\App\Http\Requests\MahakAuthenticateRequest;
use Dabestaniha\AuthenticateBridge\App\Support\JwtDecoder;
use RuntimeException;
use Throwable;
use UnexpectedValueException;
class AuthenticateController extends InvocableController
@@ -31,18 +33,14 @@ class AuthenticateController extends InvocableController
{
$jwt_secret = get_configured_option('jwt-secret');
try {
$decoded = JwtDecoder::decodeHs256($token, $jwt_secret);
$data = $decoded['data'] ?? null;
$decoded = JwtDecoder::decodeHs256($token, $jwt_secret);
$data = $decoded['data'] ?? null;
if (!is_array($data)) {
throw new UnexpectedValueException('JWT data must be an object.');
}
return $data;
} catch (\Throwable $e) {
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
if (!is_array($data)) {
throw new UnexpectedValueException('JWT data must be an object.');
}
return $data;
}
public function find_or_create_user(string $identifier_name, string $identifier_value, string $name): int
@@ -63,17 +61,33 @@ class AuthenticateController extends InvocableController
$user_id = wp_create_user($username, $random_password, $email);
if (is_wp_error($user_id)) {
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
throw new RuntimeException(
'WordPress could not create the user: '.$user_id->get_error_message()
);
}
wp_update_user([
$updated_user_id = wp_update_user([
'ID' => $user_id,
'display_name' => $name,
]);
if (is_wp_error($updated_user_id)) {
throw new RuntimeException(
'WordPress could not update the user: '.$updated_user_id->get_error_message()
);
}
return $user_id;
}
protected function handleException(Throwable $exception): void
{
$status = $exception->getCode() === 403 ? 403 : 500;
$fallbackView = $exception instanceof UnexpectedValueException ? 'invalid-token' : 'server-error';
mahak_render_error($exception, $status, $fallbackView);
}
public function login_user(int $user_id): bool
{