forked from DAT-ETSIT/wp-openid-siu-upm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenID.php
984 lines (877 loc) · 41.8 KB
/
OpenID.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
<?php
class OpenID
{
private ?string $metadata_url;
private ?string $redirect_uri;
private ?string $client_id;
private ?string $client_secret;
private ?string $default_role;
private ?bool $is_network;
private ?array $metadata;
private ?array $user_mapping;
private ?array $user_fields;
private ?string $login_button_text;
/**
* @var mixed|string
*/
private ?string $login_separator_text;
/**
* @var mixed|string
*/
private ?string $login_image;
private ?int $login_image_id = null;
private ?string $default_image;
/**
* @var false|mixed
*/
private ?bool $take_over_login;
private ?string $take_over_login_secret;
public function __construct()
{
// General Options
$this->is_network = is_plugin_active_for_network('wp-openid');
$this->metadata_url = defined('WP_OPENID_METADATA_URL') ? WP_OPENID_METADATA_URL : ($this->is_network ? get_site_option('openid_metadata_url') : get_option('openid_metadata_url'));
$this->redirect_uri = defined('WP_OPENID_REDIRECT_URI') ? WP_OPENID_REDIRECT_URI : ($this->is_network ? get_site_option('openid_redirect_uri') : get_option('openid_redirect_uri'));
$this->client_id = defined('WP_OPENID_CLIENT_ID') ? WP_OPENID_CLIENT_ID : ($this->is_network ? get_site_option('openid_client_id') : get_option('openid_client_id'));
$this->client_secret = defined('WP_OPENID_CLIENT_SECRET') ? WP_OPENID_CLIENT_SECRET : ($this->is_network ? get_site_option('openid_client_secret') : get_option('openid_client_secret'));
$this->default_role = defined('WP_OPENID_DEFAULT_ROLE') ? WP_OPENID_DEFAULT_ROLE : ($this->is_network ? get_site_option('openid_default_role') : get_option('openid_default_role'));
$this->take_over_login = defined('WP_OPENID_TAKE_OVER_LOGIN') ? WP_OPENID_TAKE_OVER_LOGIN : ($this->is_network ? get_site_option('openid_take_over_login') : get_option('openid_take_over_login'));
$this->take_over_login_secret = defined('WP_OPENID_TAKE_OVER_LOGIN_SECRET') ? WP_OPENID_TAKE_OVER_LOGIN_SECRET : ($this->is_network ? get_site_option('openid_take_over_login_secret') : get_option('openid_take_over_login_secret'));
// Styling
if ($login_button_text = defined('WP_OPENID_LOGIN_BUTTON_TEXT') ? WP_OPENID_LOGIN_BUTTON_TEXT : ($this->is_network ? get_site_option('openid_login_button_text') : get_option('openid_login_button_text'))) {
$this->login_button_text = $login_button_text;
} else {
$this->login_button_text = 'Login with OpenID';
}
if ($login_separator_text = defined('WP_OPENID_LOGIN_SEPARATOR_TEXT') ? WP_OPENID_LOGIN_SEPARATOR_TEXT : ($this->is_network ? get_site_option('openid_login_separator_text') : get_option('openid_login_separator_text'))) {
$this->login_separator_text = $login_separator_text;
} else {
$this->login_separator_text = '--- or ---';
}
$this->default_image = plugins_url('assets/images/openid.svg', __FILE__);
if ($login_image_id = $this->is_network ? get_site_option('openid_login_image_id') : get_option('openid_login_image_id')) {
if ($login_image = wp_get_attachment_url($login_image_id)) {
$this->login_image_id = $login_image_id;
$this->login_image = $login_image;
} else {
$this->login_image = $this->default_image;
}
} else {
$this->login_image = $this->default_image;
}
// User Mapping
if ($user_mapping = defined('WP_OPENID_USER_MAPPING') ? WP_OPENID_USER_MAPPING : ($this->is_network ? get_site_option('openid_user_mapping') : get_option('openid_user_mapping'))) {
$this->user_mapping = $user_mapping;
} else {
$this->user_mapping = [
'user_login' => 'preferred_username',
'user_email' => 'email',
'user_url' => 'website',
'display_name' => 'name',
'first_name' => 'given_name',
'last_name' => 'family_name',
'nickname' => 'nickname',
];
}
$this->metadata = [];
if ($this->metadata_url) {
$this->metadata = $this->_get_metadata();
}
$this->user_fields = [
'sub',
'email',
'name',
'given_name',
'family_name',
'middle_name',
'preferred_username',
'nickname',
'picture',
'profile',
'website',
];
}
public static function make(): self
{
return new self();
}
public function boot(): void
{
add_action('login_message', [$this, 'openid_login_page_button']);
add_action('admin_init', [$this, 'admin_init']);
add_action('admin_menu', [$this, 'admin_menu'], 99);
add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
register_deactivation_hook(__FILE__, [$this, 'deactivate']);
add_action('login_init', [$this, 'login_init']);
}
public function admin_enqueue_scripts($page): void
{
// If we are on the settings or options-general page, enqueue the scripts
if ($page === 'settings_page_openid') {
// Load the WordPress media scripts
wp_enqueue_media();
// Load the plugin scripts
wp_enqueue_script('openid-admin', plugins_url('assets/js/admin-image-select.js', __FILE__), ['jquery'], WP_OPENID_VER);
}
}
public function login_init(): void
{
// We don't want to get in the way of the logout function.
if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'logout') {
return;
}
// Load the OpenID routes
if (isset($_REQUEST['openid']) && $_REQUEST['openid'] === 'login') {
$this->login_redirect();
exit();
} elseif (isset($_REQUEST['openid']) && $_REQUEST['openid'] === 'callback') {
$this->login_callback();
exit();
}
// If we are "taking over" the login page, we need to disable the default login form and only show ours.
// This is easily achieved by taking over the login_init action, rendering the header (where our form is shown) and then exiting.
// We also need to check to see if they have the fallback query string, and if so, disable the take_over_login functionality.
if (isset($_REQUEST['fallback']) && $_REQUEST['fallback'] === $this->take_over_login_secret) {
// We need to disable the take_over_login functionality.
$this->take_over_login = false;
// We add a filter on set_url_scheme to change the login_post and login url to the fallback URL.
// This is so that the login form will post to the fallback URL, and we can disable the take_over_login functionality.
add_filter('site_url', function ($url, $scheme, $orig_scheme) {
if ($orig_scheme === 'login_post' || $orig_scheme === 'login') {
return add_query_arg('fallback', $this->take_over_login_secret, $url);
}
return $url;
}, 10, 3);
// Let's also add a message to the login form, to let the user know they have used the fallback URL.
add_filter('login_message', function () {
return '</br><div id="login_error">You have used the Fallback URL to enable the password form. This is only visible for you.</div>';
});
}
if ($this->take_over_login) {
// Because we hook login_message to show our button, we can just show the default header and footer, ignoring
// everything else in the login page.
login_header(__('Log In'));
login_footer();
// We exit here, to prevent the default login form from being shown. This also prevents the form being submitted.
exit();
}
// If we are not taking over the login page, we can just return here, and the default login form will be shown.
// Which uses the login_message to show our button!
}
/**
* @throws Exception
*/
public function login_redirect(): bool
{
// Redirect to OpenID , passing the state and nonce
// Implementation taken from: https://developer.openid.com/docs/guides/sign-into-web-app-redirect/php/main/#redirect-to-the-sign-in-page
$state = $this->_create_oauth_state();
// Create the PKCE code verifier and code challenge
$hash = hash('sha256', $state['verifier'], true);
$code_challenge = rtrim(strtr(base64_encode($hash), '+/', '-_'), '=');
return wp_redirect(add_query_arg([
'response_type' => 'code',
'client_id' => $this->client_id,
'state' => $state['state'],
'redirect_uri' => esc_url(add_query_arg('openid', 'callback', $this->redirect_uri)),
'code_challenge' => $code_challenge,
'code_challenge_method' => 'S256',
'scope' => 'openid profile email',
], $this->metadata['authorization_endpoint']));
}
public function login_callback(): bool
{
if (!$state = $this->_get_oauth_state()) {
die("state not found");
}
// Check the state
if (empty($_GET['state']) || $_GET['state'] != $state['state']) {
die("state does not match");
}
if (!empty($_GET['error'])) {
die("authorization server returned an error: " . $_GET['error']);
}
if (empty($_GET['code'])) {
die("this is unexpected, the authorization server redirected without a code or an error");
}
// Exchange the authorization code for an access token by making a request to the token endpoint,
// using the authorization code. The authorization code is a one-time use code, and if the token endpoint
// returns us a set of tokens instead of an error, we can assume the user and token are valid.
$token = $this->_get_token($_GET['code']);
// Because we've asked the OpenID server for the openid scope, the response will contain an id_token
// We do not need to validate the token, as it's been retrieved from the OpenID server - not the user.
// We can just decode it and use the claims.
if (empty($token['id_token'])) {
die("No id_token returned");
}
// Decode the id_token
$claim = json_decode(base64_decode(explode('.', $token['id_token'])[1]), true);
// Find or create a WordPress user for the claim, based on the user field mapping
$user = $this->_user_from_claim($claim);
// Log the user in
$this->_login_user($user);
// Delete the state
$this->_delete_oauth_state();
// Redirect to the admin page
return wp_redirect($this->is_network ? network_admin_url() : admin_url());
}
private function _login_user(WP_User $user): void
{
if (is_user_logged_in()) {
wp_logout();
}
add_filter('authenticate', [$this, 'allow_programmatic_login'], 10, 3);
$user = wp_signon(['user_login' => $user->user_login]);
remove_filter('authenticate', [$this, 'allow_programmatic_login'], 10, 3);
if (is_a($user, 'WP_User')) {
wp_set_current_user($user->ID, $user->user_login);
}
}
public function allow_programmatic_login($user, $username, $password): WP_User
{
return get_user_by('login', $username);
}
private function _get_token(string $code): array
{
// Exchange the authorization code for an access token by making a request to the token endpoint
if (!$state = $this->_get_oauth_state()) {
die("state not found3");
}
$response = wp_safe_remote_post($this->metadata['token_endpoint'], [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
],
'body' => [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => esc_url(add_query_arg('openid', 'callback', $this->redirect_uri)),
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code_verifier' => $state['verifier'],
],
'sslverify' => true,
]);
if (is_wp_error($response)) {
die("Error getting token from token server: " . $response->get_error_message());
}
return json_decode($response['body'], true);
}
private function _user_from_claim(array $claim): WP_User
{
// Check if we already have a user with this OpenID Subject ID
if ($user = get_users([
'meta_key' => 'openid_id',
'meta_value' => $claim['sub'],
'number' => 1,
])[0] ?? null) {
return $user;
}
// Check if we have a user with this username - we need to use the user_mapping to map the username from the OpenID provider
// We can fall back to the preferred_username claim if the user_mapping doesn't contain a user_login key
if ($user = get_user_by('login', $claim[$this->user_mapping['user_login']] ?? $claim['preferred_username'])) {
// We have a user with this username, so update their meta to include the OpenID Subject ID
update_user_meta($user->ID, 'openid_id', $claim['sub']);
return $user;
}
// Check if we have a user with this email address - we need to use the user_mapping to map the email from the OpenID provider
// We can fall back to the email claim if the user_mapping doesn't contain a user_email key
if ($user = get_user_by('email', $claim[$this->user_mapping['user_email']] ?? $claim['email'])) {
// We have a user with this email address, so update their meta to include the OpenID Subject ID
update_user_meta($user->ID, 'openid_id', $claim['sub']);
return $user;
}
// We don't have a user with this OpenID Subject ID, username or email address, so create one, using the
// user_mapping to map the fields from the OpenID provider
// We can fall back to the preferred_username claim if the user_mapping doesn't contain a user_login key
$user_data = [
'user_login' => $claim[$this->user_mapping['user_login']] ?? $claim['preferred_username'],
'user_email' => $claim[$this->user_mapping['user_email']] ?? $claim['email'],
'user_pass' => wp_generate_password(),
'role' => $this->default_role,
'meta_input' => [
'openid_id' => $claim['sub']
],
];
// We loop through the user_mapping to map the fields from the OpenID provider.
foreach ($this->user_mapping as $key => $value) {
// We don't want to overwrite the user_login or user_email fields, as we've already set those above
if ($key === 'user_login' || $key === 'user_email') {
continue;
}
// If the value is null or the property doesn't exist in the claim, we don't want to set it
if ($value === null || !isset($claim[$value])) {
continue;
}
// Set the mapped property on the user
$user_data[$key] = $claim[$value];
}
// Create the user, and return it
$user_id = wp_insert_user($user_data);
if (is_wp_error($user_id)) {
die("Error Creating User: " . $user_id->get_error_message());
}
return get_user_by('id', $user_id);
}
public function openid_login_page_button(): void
{
// If we have an issuer, client_id and client_secret, we can display the login button
if (isset($this->metadata['issuer']) && $this->client_id && $this->client_secret) {
?>
<style>
.openid-logo {
background-image: url("<?php echo esc_url($this->login_image); ?>");
overflow: hidden;
background-position: 45% 45%;
background-repeat: no-repeat;
background-size: cover;
height: 100px;
margin-bottom: 20px;
}
</style>
<form style="padding-bottom: 26px; text-align: center;">
<div class="openid-logo"></div>
<a href="<?php echo esc_url(add_query_arg('openid', 'login', site_url('/wp-login.php'))) ?>" class="button">
<?php printf(
esc_html__($this->login_button_text, 'openid')
); ?>
</a>
</form>
<?php
if (!$this->take_over_login) {
// If we are not taking over the login page, we can display the separator text
?>
<p style="margin-top: 20px; text-align: center;">
<?php esc_html_e($this->login_separator_text, 'openid'); ?>
</p>
<?php
}
}
}
/**
* @throws Exception
*/
public function admin_init(): void
{
// General options
register_setting('openid', 'openid_metadata_url');
register_setting('openid', 'openid_redirect_uri');
register_setting('openid', 'openid_client_id');
register_setting('openid', 'openid_client_secret');
register_setting('openid', 'openid_default_role');
register_setting('openid', 'openid_user_mapping');
// Styling options
register_setting('openid', 'openid_login_button_text');
register_setting('openid', 'openid_login_separator_text');
register_setting('openid', 'openid_login_image_id');
// Advanced options
register_setting('openid', 'openid_take_over_login');
register_setting('openid', 'openid_take_over_login_secret');
// Set a random secret if we don't have one
// This secret is used to bypass the "take_over_login" check to ensure there is a way for admins to login,
// even if the OpenID provider is down or unavailable
if (!get_option('openid_take_over_login_secret')) {
update_option('openid_take_over_login_secret', bin2hex(random_bytes(32)));
}
add_action('network_admin_edit_openid', [$this, 'save_settings']);
add_filter('plugin_action_links_wp-openid/wp-openid.php', function ($links) {
$links[] = sprintf(
'<a href="%s">%s</a>',
esc_url($this->is_network ? network_admin_url('settings.php?page=openid') : admin_url('options-general.php?page=openid')),
esc_html__('Settings', 'openid')
);
return $links;
});
}
public function admin_menu(): void
{
add_options_page('OpenID Authentication', 'OpenID', $this->is_network ? 'manage_network_options' : 'manage_options', 'openid', [$this, 'settings_page']);
}
public function settings_page(): void
{
?>
<div class="wrap">
<h1>
<?php esc_html_e('OpenID Authentication', 'openid'); ?>
</h1>
<form action="<?php echo esc_url($this->is_network ? network_admin_url('edit.php?action=openid') : admin_url('options.php')); ?>"
method="post" autocomplete="off">
<?php settings_fields('openid'); ?>
<?php do_settings_sections('openid'); ?>
<p>
Any compliant OpenID Connect provider should work, but this plugin has been tested with Okta and
Keycloak.
</p>
<h2 class="title">
<?php esc_html_e('Step 1', 'openid'); ?>
</h2>
<p>
<p>Enter your OpenID Metadata URL in the field below. If you are using Okta, your URL will look like
<code>https://dev-123456.okta.com/.well-known/openid-configuration</code>.</p>
<p>If you are using
Keycloak, your URL will look like
<code>https://keycloak.example.com/auth/realms/example/.well-known/openid-configuration</code>.
</p>
<table class="form-table">
<tr>
<th scope="row">
Metadata URL
</th>
<td>
<label>
<input type="url" name="openid_metadata_url"
value="<?php echo esc_url($this->metadata_url); ?>"
size="40"<?php echo esc_attr(defined('WP_OPENID_METADATA_URL') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
<?php
// if there is an error, we can show the user the URL is busted
if ($this->metadata && array_key_exists('error', $this->metadata)) {
?>
<tr>
<th scope="row">
<?php esc_html_e('Error', 'openid'); ?>
</th>
<td>
<code><?php echo esc_html($this->metadata['error']); ?></code>
</td>
</tr>
<?php
// If we have a valid metadata URL, show the issuer and authorization endpoint
} elseif ($this->metadata && array_key_exists('issuer', $this->metadata)) {
?>
<tr>
<th scope="row">
<?php esc_html_e('Issuer', 'openid'); ?>
</th>
<td>
<code><?php echo esc_url($this->metadata['issuer']); ?></code>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Authorization Endpoint', 'openid'); ?>
</th>
<td>
<code><?php echo esc_url($this->metadata['authorization_endpoint']); ?></code>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Token Endpoint', 'openid'); ?>
</th>
<td>
<code><?php echo esc_url($this->metadata['token_endpoint']); ?></code>
</td>
</tr>
<?php
}
?>
</table>
<h2 class="title">
<?php esc_html_e('Step 2', 'openid'); ?>
</h2>
<p>
Create a new application in your OpenID provider. The application type should be <code>Web</code>.
</p>
<p>
The following settings should be used:
</p>
<table class="form-table">
<tr>
<th scope="row">
<?php esc_html_e('Name', 'openid'); ?>
</th>
<td>
<code><?php echo get_bloginfo(); ?></code>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Grant type', 'openid'); ?>
</th>
<td>
<code>Authorization Code</code>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Sign-in redirect URIs', 'openid'); ?>
</th>
<td>
<code><?php echo esc_url(add_query_arg('openid', 'callback', site_url('/wp-login.php'))); ?></code>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Sign-out redirect URIs', 'openid'); ?>
</th>
<td>
<code><?php echo esc_url(home_url()); ?></code>
</td>
</tr>
</table>
<h2 class="title">
<?php esc_html_e('Step 3', 'openid'); ?>
</h2>
<p>
Enter your Client ID and Client Secret in the fields below.
</p>
<table class="form-table">
<tr>
<th scope="row">
<?php esc_html_e('Client ID', 'openid'); ?>
</th>
<td>
<label>
<input type="text" name="openid_client_id"
value="<?php echo esc_attr($this->client_id); ?>"
size="40"<?php echo esc_attr(defined('OPENID_CLIENT_ID') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Client Secret', 'openid'); ?>
</th>
<td>
<label>
<input type="password" name="openid_client_secret"
value="<?php echo esc_attr($this->client_secret); ?>"
size="40"<?php echo esc_attr(defined('OPENID_CLIENT_SECRET') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
</table>
<h2 class="title">
<?php esc_html_e('Step 4', 'openid'); ?>
</h2>
<p>
Enter where you want to be redirected after login. By default, it should be <code><?php echo esc_url(add_query_arg('openid', 'login', site_url('/wp-login.php'))) ?></code>
</p>
<table class="form-table">
<tr>
<th scope="row">
Redirect URI
</th>
<td>
<label>
<input type="url" name="openid_redirect_uri"
value="<?php echo esc_url($this->redirect_uri); ?>"
size="40"<?php echo esc_attr(defined('WP_OPENID_REDIRECT_URI') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
</table>
<h2 class="title">
<?php esc_html_e('Step 5', 'openid'); ?>
</h2>
<p>
User attributes can be mapped to WordPress user fields. The following attributes are supported:
</p>
<table class="form-table">
<thead>
<tr>
<th scope="col">
WordPress User Field
</th>
<th scope="col">
OpenID Attribute
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><code>user_login</code> The user's login username.</p>
</td>
<td>
<?php $this->_render_attribute_select('user_login'); ?>
</td>
</tr>
<tr>
<td>
<p><code>user_url</code> The user's URL.</p>
</td>
<td>
<?php $this->_render_attribute_select('user_url'); ?>
</td>
</tr>
<tr>
<td>
<p><code>user_email</code> The user's email address.</p>
</td>
<td>
<?php $this->_render_attribute_select('user_email'); ?>
</td>
</tr>
<tr>
<td>
<p><code>display_name</code> The user's display name.</p>
</td>
<td>
<?php $this->_render_attribute_select('display_name'); ?>
</td>
</tr>
<tr>
<td>
<p><code>nickname</code> The user's nickname.</p>
</td>
<td>
<?php $this->_render_attribute_select('nickname'); ?>
</td>
</tr>
<tr>
<td>
<p><code>first_name</code> The user's first name.</p>
</td>
<td>
<?php $this->_render_attribute_select('first_name'); ?>
</td>
</tr>
<tr>
<td>
<p><code>last_name</code> The user's last name.</p>
</td>
<td>
<?php $this->_render_attribute_select('last_name'); ?>
</td>
</tr>
<tr>
<td>
<p><code>role</code> User's role.</p>
</td>
<td>
<?php $roles = get_editable_roles(); ?>
<label>
<select name="openid_default_role">
<option value=""><?php esc_html_e('None', 'openid'); ?></option>
<?php foreach ($roles as $role => $data): ?>
<option value="<?php echo esc_attr($role); ?>"<?php selected($this->default_role, $role); ?>>
<?php echo esc_html($data['name']); ?>
</option>
<?php endforeach; ?>
</select>
</label>
</td>
</tr>
</tbody>
</table>
<h2 class="title">
<?php esc_html_e('Step 6', 'openid'); ?>
</h2>
<p>
Options for Style and Behavior.
</p>
<p>To keep your site secure from brute-force attempts, you can disable the default WordPress
login page. If you enable this option, you can use the Fallback URL to access the default
WordPress login function. Your Fallback URL should be kept secure! When you hit it, the password
form will be enabled for 1 hour, for your browser session.</p>
<table class="form-table">
<tr>
<th scope="row">Hide Default Login Form</th>
<td>
<label>
<input type="checkbox" name="openid_take_over_login"
<?php echo esc_attr(defined('WP_OPENID_TAKE_OVER_LOGIN') ? ' disabled readonly' : ''); ?>
value="1"<?php checked($this->take_over_login); ?>>
<?php esc_html_e('Hide the default WordPress login form.', 'openid'); ?>
</label>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Fallback URL', 'openid'); ?>
</th>
<td>
<code>
<?php echo esc_url(add_query_arg('fallback', $this->take_over_login_secret, wp_login_url())); ?>
</code>
<input type="hidden" name="openid_take_over_login_secret"
value="<?php echo esc_attr(get_option('openid_take_over_login_secret')); ?>">
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Login Button Text', 'openid'); ?>
</th>
<td>
<label>
<input type="text" name="openid_login_button_text"
value="<?php echo esc_attr($this->login_button_text); ?>"
size="40"<?php echo esc_attr(defined('WP_OPENID_LOGIN_BUTTON_TEXT') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Login Separator Text', 'openid'); ?>
</th>
<td>
<label>
<input type="text" name="openid_login_separator_text"
value="<?php echo esc_attr($this->login_separator_text); ?>"
size="40"<?php echo esc_attr(defined('WP_OPENID_LOGIN_SEPARATOR_TEXT') ? ' disabled readonly' : ''); ?>>
</label>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e('Login Page Image', 'openid'); ?>
</th>
<td>
<table>
<tr>
<td>
<img id="openid_image_preview"
src="<?php echo esc_url($this->login_image); ?>"
style="max-width: 150px; height: auto;" alt="Login page image"
data-default-image="<?php echo esc_url($this->default_image); ?>"/>
</td>
</tr>
<tr>
<td>
<input type="hidden" name="openid_login_image_id" id="openid_login_image_id"
value="<?php echo esc_attr($this->login_image_id); ?>"
class="regular-text"/>
<input type='button' class="button-primary"
value="<?php esc_attr_e('Select Image', 'openid'); ?>"
id="openid_media_manager"/>
<input type='button' class="button-link-delete"
value="<?php esc_attr_e('Remove Image', 'openid'); ?>"
id="openid_remove_image"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function save_settings(): void
{
// We need to check the nonce here because to make sure the user intended to change these values.
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'openid-options')) {
wp_die(esc_html__('Invalid nonce.', 'openid'));
}
// Check permissions
if (!current_user_can('manage_network_options')) {
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'openid'));
}
// Check the referer
check_admin_referer('openid-options');
// Validate and save the settings
update_site_option('openid_metadata_url', esc_url_raw(filter_var($_POST['openid_metadata_url'], FILTER_VALIDATE_URL) ?? '', ['https']));
update_site_option('openid_redirect_uri', esc_url_raw(filter_var($_POST['openid_redirect_uri'], FILTER_VALIDATE_URL) ?? '', ['https']));
update_site_option('openid_client_id', sanitize_text_field($_POST['openid_client_id'] ?? ''));
update_site_option('openid_client_secret', sanitize_text_field($_POST['openid_client_secret'] ?? ''));
update_site_option('openid_default_role', sanitize_text_field($_POST['openid_default_role'] ?? ''));
update_site_option('openid_user_mapping', sanitize_text_field($_POST['openid_user_mapping'] ?? ''));
// Redirect back to the settings page
wp_redirect($_POST['_wp_http_referer']);
}
public function deactivate(): void
{
if ($this->is_network) {
delete_site_option('openid_metadata_url');
delete_site_option('openid_redirect_uri');
delete_site_option('openid_client_id');
delete_site_option('openid_client_secret');
delete_site_option('default_role');
delete_site_option('user_mapping');
} else {
delete_option('openid_metadata_url');
delete_option('openid_redirect_uri');
delete_option('openid_client_id');
delete_option('openid_client_secret');
delete_option('openid_default_role');
delete_option('openid_user_mapping');
}
}
private function _get_metadata(): array
{
// validate the url has been set
if (!$this->metadata_url) {
return [];
}
// We cache on the hash of the url, to expire the cache if its changed
$hash = md5($this->metadata_url);
// if we have it cached, use that, if not - cache it
$metadata = get_transient('openid_metadata_' . $hash);
if (empty($metadata)) {
$response = wp_remote_get($this->metadata_url);
if (is_wp_error($response)) {
return ['error' => $response->get_error_message()];
}
// grab the decoded body
$metadata = json_decode(wp_remote_retrieve_body($response), true);
// we don't want to cache an empty result, so this is needed
if (empty($metadata)) {
return [];
}
// cache for 2 hrs
set_transient('openid_metadata_' . $hash, $metadata, 24 * HOUR_IN_SECONDS);
}
return $metadata;
}
private function _render_attribute_select(string $option): void
{
?>
<label>
<select name="openid_user_mapping[<?= $option ?>]">
<option value="">— <?php esc_html_e('Do not map', 'openid'); ?> —</option>
<?php foreach ($this->user_fields as $name) { ?>
<option value="<?php echo esc_attr($name); ?>"<?php selected($this->user_mapping[$option] ?? false, $name); ?>>
<?php echo esc_html($name); ?>
</option>
<?php } ?>
</select>
</label>
<?php
}
/**
* @throws Exception
*/
private function _create_oauth_state(): array
{
// If we have a session cookie, delete it.
if (isset($_COOKIE['openid_session'])) {
delete_transient('openid_oauth_state_' . $_COOKIE['openid_session']);
}
// Create a random hash for the user, and store it in a cookie.
$session = bin2hex(random_bytes(32));
setcookie('openid_session', $session, time() + 3600);
// Create a random state and verifier
$oauth_state = [
'state' => bin2hex(random_bytes(10)),
'verifier' => bin2hex(random_bytes(50)),
];
// Store the state and verifier in a transient, so we can verify the response later.
set_transient('openid_oauth_state_' . $session, $oauth_state, 60 * MINUTE_IN_SECONDS);
return $oauth_state;
}
/**
* @return array|false
*/
private function _get_oauth_state()
{
// Return the state and verifier from the transient, if it exists.
if (isset($_COOKIE['openid_session'])) {
return get_transient('openid_oauth_state_' . $_COOKIE['openid_session']) ?? false;
}
return false;
}
private function _delete_oauth_state(): void
{
// Delete the state and verifier from the transient, if it exists.
if (isset($_COOKIE['openid_session'])) {
delete_transient('openid_oauth_state_' . $_COOKIE['openid_session']);
}
// Delete the session cookie
setcookie('openid_session', '', time() - 3600);
}
}