-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexternal-api-authentication.php
269 lines (228 loc) · 9.59 KB
/
external-api-authentication.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
<?php
/**
* Plugin Name: External API Authentication
* Plugin URI: https://github.com/raddevon/wp-external-api-auth
* Description: Replaces WordPress authentication with external authentication via API.
* Version: 0.1.1
* Author: Devon Campbell
* Author URI: http://raddevon.com
* License: GPL2
*/
/* Copyright 2014 Devon Campbell (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/************************************
* Global Variables
************************************/
$eapia_plugin_name = "External API Authentication";
/************************************
* Plugin Options
************************************/
add_action( 'admin_menu', 'eapia_options_menu' );
function eapia_options_menu() {
global $eapia_plugin_name;
add_action( 'admin_init' , 'eapia_register_settings');
$eapia_options_page_hook = add_options_page( $eapia_plugin_name . ' Settings', $eapia_plugin_name, 'manage_options', 'eapia-settings', 'eapia_render_options');
add_action( 'admin_print_styles-' . $eapia_options_page_hook, 'eapia_admin_print_styles' );
}
function eapia_admin_print_styles() {
wp_enqueue_style( 'eapia-admin-css', plugin_dir_url(__FILE__) . 'css/admin.css' );
}
function eapia_register_settings() {
register_setting( 'eapia-settings-group', 'eapia-login-endpoint');
register_setting( 'eapia-settings-group', 'eapia-username-key');
register_setting( 'eapia-settings-group', 'eapia-password-key');
register_setting( 'eapia-settings-group', 'eapia-username');
register_setting( 'eapia-settings-group', 'eapia-email');
add_settings_section( 'eapia-general-settings', 'General', 'eapia_general_options', 'eapia-settings');
add_settings_field(
'eapia-login-endpoint',
'API authentication endpoint',
'eapia_render_text_input_field',
'eapia-settings',
'eapia-general-settings',
array(
'field' => 'eapia-login-endpoint',
'help' => '<strong>Required.</strong> The plugin will send a POST request to this URL.',
'type' => 'url'
)
);
add_settings_field(
'eapia-username-key',
'Username key',
'eapia_render_text_input_field',
'eapia-settings',
'eapia-general-settings',
array(
'field' => 'eapia-username-key',
'help' => 'The username will be sent to the API with this key. <em>(Default: username)</em>',
'type' => 'text'
)
);
add_settings_field(
'eapia-password-key',
'Password key',
'eapia_render_text_input_field',
'eapia-settings',
'eapia-general-settings',
array(
'field' => 'eapia-password-key',
'help' => 'The password will be sent to the API with this key. <em>(Default: password)</em>',
'type' => 'text'
)
);
add_settings_section( 'eapia-mapping-settings', 'User Mapping', 'eapia_mapping_options', 'eapia-settings');
add_settings_field(
'eapia-username',
'Username',
'eapia_render_text_input_field',
'eapia-settings',
'eapia-mapping-settings',
array(
'field' => 'eapia-username',
'help' => 'The username will be sent to the API with this key. <em>(Default: username)</em>',
'type' => 'text'
)
);
add_settings_field(
'eapia-email',
'Email',
'eapia_render_text_input_field',
'eapia-settings',
'eapia-mapping-settings',
array(
'field' => 'eapia-email',
'help' => 'The password will be sent to the API with this key. <em>(Default: password)</em>',
'type' => 'text'
)
);
}
function eapia_render_text_input_field($args) {
$field = $args['field'];
$help = $args['help'];
$type = $args['type'];
$value = get_option($field); ?>
<input type="<?php _e($type); ?>" name="<?php _e($field); ?>" value="<?php _e(get_option($field)); ?>" />
<p><?php _e($help); ?></p>
<?php
}
function eapia_mapping_options(){ ?>
<p>The plugin must create a WordPress user account for users authenticated externally. Use this section to map required WordPress user information to returned information from your API.</p>
<?php }
function eapia_general_options(){
return;
}
function eapia_render_options(){
global $eapia_plugin_name;
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}?>
<div class="wrap">
<h2><?php _e( $eapia_plugin_name . ' Settings') ; ?></h2>
<form method="post" action="options.php">
<?php
settings_fields( 'eapia-settings-group' );
settings_fields( 'eapia-settings-group' );
do_settings_sections( 'eapia-settings' );
settings_errors();
submit_button(); ?>
</form>
</div>
<?php
}
/************************************
* Authentication
************************************/
add_filter( 'authenticate', 'eapia_auth', 10, 3 );
function eapia_auth( $user, $username, $password ){
$endpoint = get_option( 'eapia-login-endpoint' );
$username_key = get_option( 'eapia-username-key', 'username' ) ?: 'username';
$password_key = get_option( 'eapia-password-key', 'password' ) ?: 'password';
// Makes sure we have an endpoint set
if(!$endpoint ) return;
// Build the POST request
$login_data = array(
$username_key => $username,
$password_key => $password );
$response = wp_remote_post( $endpoint, $login_data);
$ext_auth = json_decode( $response['body'], true );
if( $ext_auth['result'] == 0 ) {
// User does not exist, send back an error message
$user = new WP_Error( 'denied', __("<strong>Error</strong>: Your username or password are incorrect.") );
} else if( $ext_auth['result'] == 1 ) {
// External user exists, try to load the user info from the WordPress user table
$userobj = new WP_User();
$user = $userobj->get_data_by( 'email', $ext_auth['email'] ); // Does not return a WP_User object <img src="http://ben.lobaugh.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley" />
$user = new WP_User($user->ID); // Attempt to load up the user with that ID
if( $user->ID == 0 ) {
// The user does not currently exist in the WordPress user table.
// You have arrived at a fork in the road, choose your destiny wisely
// If you do not want to add new users to WordPress if they do not
// already exist uncomment the following line and remove the user creation code
//$user = new WP_Error( 'denied', __("<strong>ERROR</strong>: Not a valid user for this system") );
// Setup the minimum required user information for this example
$userdata = array( 'user_email' => $ext_auth['email'],
'user_login' => $ext_auth['email'],
'first_name' => $ext_auth['first_name'],
'last_name' => $ext_auth['last_name']
);
$new_user_id = wp_insert_user( $userdata ); // A new user has been created
// Load the new user info
$user = new WP_User ($new_user_id);
}
}
// Comment this line if you wish to fall back on WordPress authentication
// Useful for times when the external service is offline
remove_action('authenticate', 'wp_authenticate_username_password', 20);
return $user;
}
/************************************
* Alert
************************************/
/**
* Generic function to show a message to the user using WP's
* standard CSS classes to make use of the already-defined
* message colour scheme.
*
* @param $message The message you want to tell the user.
* @param $errormsg If true, the message is an error, so use
* the red message style. If false, the message is a status
* message, so use the yellow information message style.
*/
function showMessage($message, $errormsg = false)
{
if ($errormsg) {
echo '<div id="message" class="error">';
}
else {
echo '<div id="message" class="updated fade">';
}
echo "<p><strong>$message</strong></p></div>";
}
/**
* Just show our message (with possible checking if we only want
* to show message to certain users.
*/
function showAdminMessages()
{
// Only show to admins
if (current_user_can('manage_options') && !get_option( 'eapia-login-endpoint' )) {
showMessage("You must <a href='options-general.php?page=eapia-settings.php'>provide your API's authentication endpoint</a> to use External API Authentication.", true);
}
}
/**
* Call showAdminMessages() when showing other admin
* messages. The message only gets shown in the admin
* area, but not on the frontend of your WordPress site.
*/
add_action('admin_notices', 'showAdminMessages');
?>