function crowd_authenticate_rest($user, $username, $password, $crowd_config) { $crowd = new CrowdREST($crowd_config); $authenticated_username = $crowd->authenticateUser($username, $password); if ($authenticated_username == null) { return new WP_Error($code = null, $message = "username or password incorrect"); } else { $user = get_userdatabylogin($authenticated_username); if (!$user || strtolower($user->user_login) != strtolower($username)) { //No user, can we create? switch (get_option('crowd_login_mode')) { case 'mode_create_all': // create the new user using crowd data $new_user_id = crowd_create_rest_wp_user($authenticated_username); if (!is_a($new_user_id, 'WP_Error')) { //It worked return new WP_User($new_user_id); } else { do_action('wp_login_failed', $authenticated_username); return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd credentials are correct and user creation is allowed but an error occurred creating the user in Wordpress. Actual WordPress error: ' . $new_user_id->get_error_message())); } break; case 'mode_create_group': if (crowd_is_in_rest_group($authenticated_username)) { // create the new user using crowd data $new_user_id = crowd_create_rest_wp_user($authenticated_username); if (!is_a($new_user_id, 'WP_Error')) { //It worked return new WP_User($new_user_id); } else { do_action('wp_login_failed', $authenticated_username); return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd credentials are correct and user creation is allowed and you are in the correct group but an error occurred creating the user in Wordpress. Actual WordPress error: ' . $new_user_id->get_error_message())); } } else { do_action('wp_login_failed', $authenticated_username); return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login credentials are correct and user creation is allowed but Crowd user was not in the correct group.')); } break; default: do_action('wp_login_failed', $authenticated_username); return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd Login mode does not permit account creation.')); } } else { //Wordpress user exists, should we check group membership? if (get_option('crowd_login_mode') == 'mode_create_group') { if (crowd_is_in_rest_group($authenticated_username)) { return new WP_User($user->ID); } else { do_action('wp_login_failed', $authenticated_username); return new WP_Error('invalid_username', __('<strong>Crowd Login Error</strong>: Crowd credentials were correct but user is not in the correct group.')); } } else { //Otherwise, we're ready to return the user return new WP_User($user->ID); } } } }
// // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. header("Content-Type: text/plain"); require 'Crowd-REST.php'; $fields = array('crowd_url' => false, 'app_name' => false, 'app_credential' => true, 'username' => false, 'password' => true); if ($_POST && array_key_exists('crowd_url', $_POST)) { $username = $_POST['username']; $password = $_POST['password']; $_POST['verify_ssl_peer'] = false; $crowd = new CrowdREST($_POST); $messages = array(); $auth_response = $crowd->authenticateUser($username, $password); $messages[] = "Got auth response of '{$auth_response}'"; if (!empty($auth_response)) { $messages[] = "Authentication of '{$auth_response}' sucessful."; } else { $messages[] = "Authentication of '{$username}' failed (check log)."; } $userinfo = $crowd->getUserInfo($auth_response); if ($userinfo) { $messages[] = "Got user info for '{$username}'"; $msg = "<table><tr><th>field</th><th>value</th></tr>\n"; foreach ($userinfo as $field => $value) { $msg = "{$msg}<tr><td>{$field}</td><td>{$value}</td></tr>\n"; }