forked from jaketblank/launchkey-wordpress
/
launchkey.php
352 lines (310 loc) · 12.4 KB
/
launchkey.php
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/*
Plugin Name: LaunchKey
Plugin URI: https://wordpress.org/plugins/launchkey/
Description: LaunchKey eliminates the need and liability of passwords by letting you log in and out of WordPress with your smartphone or tablet.
Version: 1.3.0
Author: LaunchKey, Inc.
Text Domain: launchkey
Author URI: https://launchkey.com
License: GPLv2 Copyright (c) 2014 LaunchKey, Inc.
*/
require_once __DIR__ . '/vendor/autoload.php';
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
/**
* Create/update tables utilized by the plugin
* @since 1.1.0
*/
function launchkey_create_tables() {
global $wpdb;
$table_name = $wpdb->prefix . 'launchkey_sso_sessions';
$sql = "CREATE TABLE {$table_name} (
id VARCHAR(255) NOT NULL,
seen DATETIME NOT NULL,
UNIQUE KEY {$table_name}_id (id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
function launchkey_cron() {
global $wpdb;
$table_name = $wpdb->prefix . 'launchkey_sso_sessions';
$dt = new DateTime( "- 1 hour" );
$dt->setTimezone( new DateTimeZone( "UTC" ) );
$wpdb->query(
$wpdb->prepare( "DELETE FROM {$table_name} WHERE seen < %s", $dt->format( "Y-m-d H:i:s" ) )
);
}
function launchkey_cron_remove() {
$timestamp = wp_next_scheduled( 'launchkey_cron_hook' );
wp_unschedule_event( $timestamp, 'launchkey_cron_hook' );
}
function launchkey_is_mu_plugin() {
return strpos( __FILE__, WPMU_PLUGIN_DIR ) === 0;
}
function launchkey_is_activated() {
return is_plugin_active( __FILE__ ) || launchkey_is_mu_plugin();
}
/**
* Initialize LaunchKey WordPress Plugin
*
* This function will perform the entire initialization for the plugin. The initialization is encapsulated into
* a funciton to protect against global variable collision.
*
* @since 1.0.0
* Enclose plug-in initialization to protect against global variable corruption
*/
function launchkey_plugin_init() {
global $wpdb;
/**
* Register activation hooks for the plugin
* @since 1.1.0
*/
register_activation_hook( __FILE__, 'launchkey_create_tables' );
/**
* Remove the scheduled cron
* @since 1.1.0
*/
register_deactivation_hook( __FILE__, 'launchkey_cron_remove' );
/**
* @since 1.1.0
* Add the cron hook and schedule if not scheduled
*/
add_action( 'launchkey_cron_hook', 'launchkey_cron' );
if ( ! wp_next_scheduled( 'launchkey_cron_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'launchkey_cron_hook' );
}
/**
* Language domain for the plugin
*/
$language_domain = 'launchkey';
/**
* Register plugin text domain with language files
*
* @see load_plugin_textdomain
* @link https://developer.wordpress.org/reference/hooks/plugins_loaded/
*/
add_action( 'plugins_loaded', function () use ( $language_domain ) {
load_plugin_textdomain( $language_domain, false, plugin_basename( __FILE__ ) . '/languages/' );
} );
/**
* Create an AES encryption class for encryption/decryption of the secret options
* @link https://docs.launchkey.com/glossary.html#term-aes
*/
$crypt_aes = new \phpseclib\Crypt\AES();
/**
* Use an MD5 hash of the auth key as the crypto key. The crypto key is used as it would normally affect all auth
* procedures as it is used as a salt for passwords. An md5 hash is used as it will be a constant value based on
* the AUTH_KEY but guaranteed to be exactly thirty-two (32) characters as is needed by AES encryption.
*/
$crypt_aes->setKey( md5( AUTH_KEY ) );
// Create an options handler that will encrypt and decrypt the plugin options as necessary
$options_handler = new LaunchKey_WP_Options( $crypt_aes );
/**
* The pre_update_option_launchkey filter will process the "launchkey" option directly
* before updating the data in the database.
*
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/pre_update_option_option/
* @see LaunchKey_WP_Options::pre_update_option_filter
*/
add_filter( 'pre_update_option_launchkey', array( $options_handler, 'pre_update_option_filter' ) );
add_filter( 'pre_update_site_option_launchkey', array( $options_handler, 'pre_update_option_filter' ) );
/**
* The pre_update_option_filter filter will process the "launchkey" option directly
* before adding the data in the database.
*
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/pre_update_option_option/
* @see LaunchKey_WP_Options::pre_update_option_filter
*/
add_filter( 'pre_add_option_launchkey', array( $options_handler, 'pre_update_option_filter' ) );
add_filter( 'pre_add_site_option_launchkey', array( $options_handler, 'pre_update_option_filter' ) );
/**
* The option_launchkey filter will process the "launchkey" option directly
* after retrieving the data from the database.
*
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/option_option/
* @see LaunchKey_WP_Options::post_get_option_filter
*/
add_filter( 'option_launchkey', array( $options_handler, 'post_get_option_filter' ) );
add_filter( 'site_option_launchkey', array( $options_handler, 'post_get_option_filter' ) );
$is_multi_site = is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) );
$options = $is_multi_site ? get_site_option( LaunchKey_WP_Admin::OPTION_KEY ) : get_option( LaunchKey_WP_Admin::OPTION_KEY );
/**
* Handle upgrades if in the admin and not the latest version
*/
if ( is_admin() && launchkey_is_activated() && $options && $options[ LaunchKey_WP_Options::OPTION_VERSION ] < 1.1 ) {
launchkey_create_tables();
}
/**
* If the pre-1.0.0 option style was already used, create a 1.0.0 option and remove the old options. They are
* removed as the secret_key was stored plain text in the database.
*
* @since 1.0.0
*/
if ( get_option( 'launchkey_app_key' ) || get_option( 'launchkey_secret_key' ) ) {
$launchkey_options[ LaunchKey_WP_Options::OPTION_ROCKET_KEY ] = get_option( 'launchkey_app_key' );
$launchkey_options[ LaunchKey_WP_Options::OPTION_SECRET_KEY ] = get_option( 'launchkey_secret_key' );
$launchkey_options[ LaunchKey_WP_Options::OPTION_SSL_VERIFY ] =
( defined( 'LAUNCHKEY_SSLVERIFY' ) && LAUNCHKEY_SSLVERIFY ) || true;
$launchkey_options[ LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE ] =
LaunchKey_WP_Implementation_Type::OAUTH;
$launchkey_options[ LaunchKey_WP_Options::OPTION_LEGACY_OAUTH ] = true;
$updated = $is_multi_site ? update_network_option( LaunchKey_WP_Admin::OPTION_KEY, $launchkey_options ) :
update_option( LaunchKey_WP_Admin::OPTION_KEY, $launchkey_options );
if ( $updated ) {
delete_option( 'launchkey_app_key' );
delete_option( 'launchkey_secret_key' );
} else {
throw new RuntimeException( 'Unable to upgrade LaunchKey meta-data. Failed to save setting ' .
LaunchKey_WP_Admin::OPTION_KEY );
}
} elseif ( ! $options ) {
$is_multi_site ? add_site_option( LaunchKey_WP_Admin::OPTION_KEY, array() ) : add_option( LaunchKey_WP_Admin::OPTION_KEY, array() );
$options = $is_multi_site ? get_site_option( LaunchKey_WP_Admin::OPTION_KEY ) : get_option( LaunchKey_WP_Admin::OPTION_KEY );
}
/**
* Get the WP global facade
* @see LaunchKey_WP_Global_Facade
*/
$facade = new LaunchKey_WP_Global_Facade();
/**
* Create a templating object and point it at the correct directory for template files.
*
* @see LaunchKey_WP_Template
*/
$template = new LaunchKey_WP_Template( __DIR__ . '/templates', $facade, $language_domain );
// Prevent XXE Processing Vulnerability
libxml_disable_entity_loader( true );
// Get the plugin options to determine which authentication implementation should be utilized
$logger = new LaunchKey_WP_Logger( $facade );
$launchkey_client = null;
$client = null;
// Only register the pieces that need to interact with LaunchKey if it's been configured
if ( LaunchKey_WP_Implementation_Type::SSO === $options[ LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE ] &&
! empty( $options[ LaunchKey_WP_Options::OPTION_SSO_ENTITY_ID ] )
) {
$container = new LaunchKey_WP_SAML2_Container( $logger );
SAML2_Compat_ContainerSingleton::setContainer( $container );
$securityKey = new XMLSecurityKey( XMLSecurityKey::RSA_SHA1, array( 'type' => 'public' ) );
$securityKey->loadKey( $options[ LaunchKey_WP_Options::OPTION_SSO_CERTIFICATE ], false, true );
$saml_response_service = new LaunchKey_WP_SAML2_Response_Service( $securityKey, $facade );
$saml_request_service = new LaunchKey_WP_SAML2_Request_Service( $securityKey );
$client = new LaunchKey_WP_SSO_Client(
$facade,
$template,
$options[ LaunchKey_WP_Options::OPTION_SSO_ENTITY_ID ],
$saml_response_service,
$saml_request_service,
$wpdb,
$options[ LaunchKey_WP_Options::OPTION_SSO_LOGIN_URL ],
$options[ LaunchKey_WP_Options::OPTION_SSO_LOGOUT_URL ],
$options[ LaunchKey_WP_Options::OPTION_SSO_ERROR_URL ],
$is_multi_site
);
} elseif ( LaunchKey_WP_Implementation_Type::OAUTH ===
$options[ LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE ] &&
! empty( $options[ LaunchKey_WP_Options::OPTION_SECRET_KEY ] )
) {
/**
* If the implementation type is OAuth, use the OAuth client
* @see LaunchKey_WP_OAuth_Client
*/
$client = new LaunchKey_WP_OAuth_Client( $facade, $template, $is_multi_site);
} elseif ( ! empty( $options[ LaunchKey_WP_Options::OPTION_SECRET_KEY ] ) ) {
$launchkey_client = \LaunchKey\SDK\Client::wpFactory(
$options[ LaunchKey_WP_Options::OPTION_ROCKET_KEY ],
$options[ LaunchKey_WP_Options::OPTION_SECRET_KEY ],
$options[ LaunchKey_WP_Options::OPTION_PRIVATE_KEY ],
$options[ LaunchKey_WP_Options::OPTION_SSL_VERIFY ]
);
$client = new LaunchKey_WP_Native_Client( $launchkey_client, $facade, $template, $language_domain, $is_multi_site);
add_filter( 'init', function () use ( $facade ) {
wp_enqueue_script(
'launchkey-script',
plugins_url( '/public/launchkey-login.js', __FILE__ ),
array( 'jquery' ),
'1.1.1',
true
);
} );
}
if ( $client ) {
/**
* Register the non-admin actions for authentication client. These actions will handle all of the
* authentication work for the plugin.
*
* @see LaunchKey_WP_Client::register_actions
* @see LaunchKey_WP_OAuth_Client::register_actions
* @see LaunchKey_WP_Native_Client::register_actions
*/
$client->register_actions();
/**
* Create the a user profile object and register its actions. These actions will handle all functionality
* related to a user customizing their authentication related options.
*
* @see LaunchKey_WP_User_Profile
*/
$profile = new LaunchKey_WP_User_Profile( $facade, $template, $language_domain, $is_multi_site );
$profile->register_actions();
/**
* Hideous workaround for the wp-login.php page not printing styles in the header like it should.
*
* @since 1.0.0
*/
if ( ! has_action( 'login_enqueue_scripts', 'wp_print_styles' ) ) {
add_action( 'login_enqueue_scripts', 'wp_print_styles', 11 );
}
}
if ( is_admin() || ( $is_multi_site && is_network_admin() ) ) {
/**
* If we are in the admin, create an admin object and register its actions. These actions
* will manage setting of options and user management for the plugin.
*
* @see is_admin
* @see LaunchKey_WP_Admin
*/
$launchkey_admin = new LaunchKey_WP_Admin( $facade, $template, $language_domain, $is_multi_site );
$launchkey_admin->register_actions();
$config_wizard = new LaunchKey_WP_Configuration_Wizard(
$facade, $launchkey_admin, $is_multi_site, $launchkey_client
);
$config_wizard->register_actions();
}
/**
* Add a filter to enqueue styles for the plugin
*
* @since 1.0.0
*
* @see add_filter
* @see wp_enqueue_style
* @link https://developer.wordpress.org/reference/functions/add_filter/
* @link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
*/
add_filter( 'init', function () use ( $facade ) {
wp_enqueue_style(
'launchkey-style',
plugins_url( '/public/launchkey.css', __FILE__ ),
array(),
'1.0.1',
false
);
} );
/**
* Handle activation when a "must use" plugin
*/
if ( launchkey_is_mu_plugin() ) {
$mu_activated_option = "launchkey_activated";
if ( ! get_option( $mu_activated_option ) ) {
do_action( "activate_" . plugin_basename( __FILE__ ) );
add_option( $mu_activated_option, true );
}
}
}
// Run the initialization function above
launchkey_plugin_init();