| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: Simple JWT Login |
| 5 |
Plugin URI: https://simplejwtlogin.com |
| 6 |
Description: Simple-JWT-Login REST API Plugin. Allows you to login / register to WordPress using JWT. |
| 7 |
Author: Nicu Micle |
| 8 |
Author URI: https://profiles.wordpress.org/nicu_m/ |
| 9 |
Text Domain: simple-jwt-login |
| 10 |
Domain Path: /i18n |
| 11 |
License: GPLv3 |
| 12 |
License URI: https://github.com/nicumicle/simple-jwt-login/blob/master/LICENSE |
| 13 |
Version: 4.0.1 |
| 14 |
*/ |
| 15 |
|
| 16 |
use SimpleJWTLogin\Modules\SimpleJWTLoginSettings; |
| 17 |
use SimpleJWTLogin\Plugin\AdminUI; |
| 18 |
use SimpleJWTLogin\Plugin\CronCleanup; |
| 19 |
use SimpleJWTLogin\Plugin\Lifecycle; |
| 20 |
use SimpleJWTLogin\Plugin\LoginPageIntegration; |
| 21 |
use SimpleJWTLogin\Plugin\OAuthTwoFactorLoginHandler; |
| 22 |
use SimpleJWTLogin\Plugin\Shortcodes; |
| 23 |
use SimpleJWTLogin\Plugin\UserApiKeysPage; |
| 24 |
use SimpleJWTLogin\Repositories\ApiKey\ApiKeyRepository; |
| 25 |
use SimpleJWTLogin\Repositories\AuditLog\AuditLogRepository; |
| 26 |
use SimpleJWTLogin\Repositories\RefreshToken\RefreshTokenRepository; |
| 27 |
use SimpleJWTLogin\Repositories\RevokedToken\RevokedTokenRepository; |
| 28 |
use SimpleJWTLogin\Repositories\WebhookLog\WebhookLogRepository; |
| 29 |
use SimpleJWTLogin\Repositories\Wordpress\WordPressRepository; |
| 30 |
use SimpleJWTLogin\Routes\RouteRegistrar; |
| 31 |
use SimpleJWTLogin\Services\Oauth\AbstractOauth; |
| 32 |
|
| 33 |
if (! defined('ABSPATH')) { |
| 34 |
/** @phpstan-ignore-next-line */ |
| 35 |
exit; |
| 36 |
} // Exit if accessed directly |
| 37 |
|
| 38 |
define('SIMPLE_JWT_LOGIN_VERSION', '4.0.1'); |
| 39 |
define('SIMPLE_JWT_LOGIN_DB_VERSION', '1.10'); |
| 40 |
define('SIMPLE_JWT_LOGIN_PLUGIN_FILE', __FILE__); |
| 41 |
|
| 42 |
include_once 'autoload.php'; |
| 43 |
require_once(ABSPATH . 'wp-admin/includes/user.php'); |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
// Shared settings (used by LoginPageIntegration and CronCleanup) |
| 48 |
$simpleJwtLoginWordPressRepository = WordPressRepository::getInstance(); |
| 49 |
$simpleJwtLoginJwtSettings = new SimpleJWTLoginSettings($simpleJwtLoginWordPressRepository); |
| 50 |
|
| 51 |
// User-facing API Keys menu (for non-admin users when the setting is enabled). |
| 52 |
// Only relevant inside wp-admin, so skip building it on front-end/REST/cron requests. |
| 53 |
if (is_admin()) { |
| 54 |
$simpleJwtLoginUserApiKeysPage = new UserApiKeysPage($simpleJwtLoginJwtSettings); |
| 55 |
add_action('admin_menu', array($simpleJwtLoginUserApiKeysPage, 'registerMenuEntry')); |
| 56 |
} |
| 57 |
|
| 58 |
// Login page integration |
| 59 |
//phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 60 |
$simpleJwtLoginLoginPage = new LoginPageIntegration($_REQUEST, $simpleJwtLoginJwtSettings); |
| 61 |
add_action('login_head', array($simpleJwtLoginLoginPage, 'enqueueLoginAssets')); |
| 62 |
add_action('login_message', array($simpleJwtLoginLoginPage, 'showLoginMessage')); |
| 63 |
add_action('login_footer', array($simpleJwtLoginLoginPage, 'renderLoginFooter')); |
| 64 |
|
| 65 |
// Browser-based OAuth + 2FA page (wp-login.php?action=sjl-oauth-2fa) |
| 66 |
//phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
| 67 |
$simpleJwtLoginOAuthTwoFactor = new OAuthTwoFactorLoginHandler($_SERVER, $_GET, $_POST, $simpleJwtLoginJwtSettings); |
| 68 |
add_action( |
| 69 |
'login_form_' . AbstractOauth::BROWSER_2FA_ACTION, |
| 70 |
array($simpleJwtLoginOAuthTwoFactor, 'handleAction') |
| 71 |
); |
| 72 |
|
| 73 |
// Shortcodes |
| 74 |
//phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 75 |
$simpleJwtLoginShortcodes = new Shortcodes($_REQUEST, $simpleJwtLoginJwtSettings); |
| 76 |
add_shortcode('simple-jwt-login:request', array($simpleJwtLoginShortcodes, 'handleRequestShortcode')); |
| 77 |
add_shortcode('simple-jwt-login-oauth', array($simpleJwtLoginShortcodes, 'handleOauthShortcode')); |
| 78 |
|
| 79 |
// Shared repositories |
| 80 |
$simpleJwtLoginRefreshTokenRepo = new RefreshTokenRepository($wpdb); |
| 81 |
$simpleJwtLoginAuditLogRepo = new AuditLogRepository($wpdb); |
| 82 |
$simpleJwtLoginWebhookLogRepo = new WebhookLogRepository($wpdb); |
| 83 |
$simpleJwtLoginApiKeyRepo = new ApiKeyRepository($wpdb); |
| 84 |
$simpleJwtLoginRevokedTokenRepo = new RevokedTokenRepository($wpdb); |
| 85 |
|
| 86 |
// Admin UI - only needed inside wp-admin, so skip building it on front-end/REST/cron requests. |
| 87 |
if (is_admin()) { |
| 88 |
//phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
| 89 |
$simpleJwtLoginAdminUI = new AdminUI( |
| 90 |
$_SERVER, |
| 91 |
$_POST, |
| 92 |
$simpleJwtLoginJwtSettings, |
| 93 |
$simpleJwtLoginAuditLogRepo, |
| 94 |
$simpleJwtLoginWebhookLogRepo, |
| 95 |
$simpleJwtLoginApiKeyRepo, |
| 96 |
$simpleJwtLoginRevokedTokenRepo |
| 97 |
); |
| 98 |
add_action('admin_menu', array($simpleJwtLoginAdminUI, 'registerMenuEntry')); |
| 99 |
add_filter( |
| 100 |
'plugin_action_links_' . plugin_basename(__FILE__), |
| 101 |
array($simpleJwtLoginAdminUI, 'addPluginActionLinks') |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
// Lifecycle (activation, deactivation, uninstall, migration, i18n) |
| 106 |
$simpleJwtLoginLifecycle = new Lifecycle( |
| 107 |
$simpleJwtLoginRefreshTokenRepo, |
| 108 |
$simpleJwtLoginAuditLogRepo, |
| 109 |
$simpleJwtLoginWebhookLogRepo, |
| 110 |
$simpleJwtLoginApiKeyRepo, |
| 111 |
$simpleJwtLoginRevokedTokenRepo, |
| 112 |
$simpleJwtLoginWordPressRepository |
| 113 |
); |
| 114 |
register_activation_hook(__FILE__, array($simpleJwtLoginLifecycle, 'activate')); |
| 115 |
register_deactivation_hook(__FILE__, array($simpleJwtLoginLifecycle, 'deactivate')); |
| 116 |
register_uninstall_hook(__FILE__, 'SimpleJWTLogin\\Plugin\\Lifecycle::uninstall'); |
| 117 |
add_action('init', array($simpleJwtLoginLifecycle, 'loadTranslations')); |
| 118 |
add_action('plugins_loaded', array($simpleJwtLoginLifecycle, 'checkDbVersion')); |
| 119 |
|
| 120 |
// Cron cleanup |
| 121 |
$simpleJwtLoginCron = new CronCleanup( |
| 122 |
$simpleJwtLoginJwtSettings, |
| 123 |
$simpleJwtLoginRefreshTokenRepo, |
| 124 |
$simpleJwtLoginAuditLogRepo, |
| 125 |
$simpleJwtLoginWebhookLogRepo, |
| 126 |
$simpleJwtLoginRevokedTokenRepo |
| 127 |
); |
| 128 |
add_action('simple_jwt_login_cleanup_refresh_tokens', array($simpleJwtLoginCron, 'cleanupRefreshTokens')); |
| 129 |
add_action('simple_jwt_login_cleanup_audit_logs', array($simpleJwtLoginCron, 'cleanupAuditLogs')); |
| 130 |
add_action('simple_jwt_login_cleanup_webhook_logs', array($simpleJwtLoginCron, 'cleanupWebhookLogs')); |
| 131 |
add_action('simple_jwt_login_cleanup_revoked_tokens', array($simpleJwtLoginCron, 'cleanupRevokedTokens')); |
| 132 |
|
| 133 |
// REGISTER REST Routes |
| 134 |
add_action( |
| 135 |
'rest_api_init', |
| 136 |
function () use ( |
| 137 |
$simpleJwtLoginWordPressRepository, |
| 138 |
$simpleJwtLoginJwtSettings, |
| 139 |
$simpleJwtLoginRefreshTokenRepo, |
| 140 |
$simpleJwtLoginAuditLogRepo, |
| 141 |
$simpleJwtLoginApiKeyRepo, |
| 142 |
$simpleJwtLoginWebhookLogRepo, |
| 143 |
$simpleJwtLoginRevokedTokenRepo |
| 144 |
) { |
| 145 |
//phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 146 |
$registrar = new RouteRegistrar($_SERVER, $_REQUEST, $_COOKIE); |
| 147 |
$registrar |
| 148 |
->withWordPressRepository($simpleJwtLoginWordPressRepository) |
| 149 |
->withSettings($simpleJwtLoginJwtSettings) |
| 150 |
->withRefreshTokenRepo($simpleJwtLoginRefreshTokenRepo) |
| 151 |
->withAuditLogRepo($simpleJwtLoginAuditLogRepo) |
| 152 |
->withApiKeyRepo($simpleJwtLoginApiKeyRepo) |
| 153 |
->withWebhookLogRepo($simpleJwtLoginWebhookLogRepo) |
| 154 |
->withRevokedTokenRepo($simpleJwtLoginRevokedTokenRepo) |
| 155 |
->register(); |
| 156 |
} |
| 157 |
); |
| 158 |
|
| 159 |
// 3rd-party integrations |
| 160 |
include_once '3rd-party/force_login.php'; |
| 161 |
include_once '3rd-party/wp-graphql.php'; |
| 162 |
include_once '3rd-party/woocommerce.php'; |
| 163 |
|