Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Formatting changed to match original repo
  • Loading branch information
graham73may committed Mar 21, 2016
commit 4ddbb3a1484767102726d87181299da339a18f48
83 changes: 38 additions & 45 deletions basic-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
* Plugin URI: https://github.com/WP-API/Basic-Auth
*/

function json_basic_auth_handler($user)
{
function json_basic_auth_handler( $user ) {
global $wp_json_basic_auth_error;

global $wp_json_basic_auth_error;
$wp_json_basic_auth_error = null;

$wp_json_basic_auth_error = null;

// Don't authenticate twice
if (!empty($user)) {
return $user;
}
// Don't authenticate twice
if ( ! empty( $user ) ) {
return $user;
}

if (!isset($_SERVER['PHP_AUTH_USER']) && (isset($_SERVER['HTTP_AUTHORIZATION']) || isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']))) {
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
Expand All @@ -29,51 +27,46 @@ function json_basic_auth_handler($user)

list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($header, 6)));
}

// Check that we're trying to authenticate
if ( !isset( $_SERVER['PHP_AUTH_USER'] ) ) {
return $user;
}

// Check that we're trying to authenticate
if (!isset($_SERVER['PHP_AUTH_USER'])) {
return $user;
}

$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];

/**
* In multi-site, wp_authenticate_spam_check filter is run on authentication. This filter calls
* get_currentuserinfo which in turn calls the determine_current_user filter. This leads to infinite
* recursion and a stack overflow unless the current function is removed from the determine_current_user
* filter during authentication.
*/
remove_filter('determine_current_user', 'json_basic_auth_handler', 20);
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];

$user = wp_authenticate($username, $password);
/**
* In multi-site, wp_authenticate_spam_check filter is run on authentication. This filter calls
* get_currentuserinfo which in turn calls the determine_current_user filter. This leads to infinite
* recursion and a stack overflow unless the current function is removed from the determine_current_user
* filter during authentication.
*/
remove_filter( 'determine_current_user', 'json_basic_auth_handler', 20 );

add_filter('determine_current_user', 'json_basic_auth_handler', 20);
$user = wp_authenticate( $username, $password );

if (is_wp_error($user)) {
$wp_json_basic_auth_error = $user;
add_filter( 'determine_current_user', 'json_basic_auth_handler', 20 );

return null;
}
if ( is_wp_error( $user ) ) {
$wp_json_basic_auth_error = $user;
return null;
}

$wp_json_basic_auth_error = true;
$wp_json_basic_auth_error = true;

return $user->ID;
return $user->ID;
}
add_filter( 'determine_current_user', 'json_basic_auth_handler', 20 );

add_filter('determine_current_user', 'json_basic_auth_handler', 20);

function json_basic_auth_error($error)
{
function json_basic_auth_error( $error ) {
// Passthrough other errors
if ( ! empty( $error ) ) {
return $error;
}

// Passthrough other errors
if (!empty($error)) {
return $error;
}
global $wp_json_basic_auth_error;

global $wp_json_basic_auth_error;

return $wp_json_basic_auth_error;
return $wp_json_basic_auth_error;
}

add_filter('json_authentication_errors', 'json_basic_auth_error');
add_filter( 'json_authentication_errors', 'json_basic_auth_error' );