HandleResponse($wresult, ACS_APPLICATION_REALM, ACS_TOKEN_TYPE); $validator = new TokenValidator(); $validator->Validate($token, ACS_APPLICATION_REALM, ACS_NAMESPACE, ACS_TOKEN_SIGNING_KEY, ACS_TOKEN_SIGNING_KEY_OLD); $claims = $validator->GetClaims($token); } catch (Exception $e) { $user = new WP_Error('login_error', $e->getMessage()); return $user; } //check for required UUID and IDP claims if ( empty($claims[USER_UUID]) ) { $user = new WP_Error('login_error', 'No user ID was returned from the selected identity provider'); return $user; } elseif ( empty($claims[USER_IDP]) ) { $user = new WP_Error('login_error', 'No identity provider claim was returned'); return $user; } //look up the user profile based on the unique ID and identity provider claims received $user = acs_get_user($claims[USER_IDP], $claims[USER_UUID]); //create user account if one doesn't exist if (!$user) { //show the account creation form if (!array_key_exists('user_login', $_POST)) { acs_create_user_form($wresult, $claims); exit; } //handle the response from the account creation form else { $user_login = sanitize_user( $_POST['user_login'] ); $user_email = empty($claims[USER_EMAIL]) ? apply_filters('user_registration_email', $_POST['user_email']) : $claims[USER_EMAIL]; $errors = new WP_Error(); //check username if (username_exists($user_login)) { $errors->add( 'username_exists', __( 'ERROR: This username is already registered, please choose another one.' ) ); } elseif (!validate_username($user_login)) { $errors->add( 'invalid_username', __( 'ERROR: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); } elseif (empty($user_login)) { $errors->add( 'empty_username', __( 'ERROR: Please enter a valid username.' ) ); } //check email if ( $user_email == '' || !is_email($user_email)) { $errors->add( 'invalid_email', __( 'ERROR: Please enter a valid e-mail address.' ) ); } elseif (email_exists($user_email)) { $errors->add( 'email_exists', __( 'ERROR: This email is already registered. Please log in using a different account.' ) ); } //display form if errors occurred if ( $errors->get_error_code() ) { acs_create_user_form($wresult, $claims, $errors, $user_login, $user_email); exit; } } //map user identity claims received to WordPress user attributes $userData['user_login'] = acs_escape_string($user_login); $userData['user_pass'] = sha1(strval(rand()).$claims[USER_UUID]); //creates a random password so the local account is protected $userData['display_name'] = acs_escape_string($user_login); $userData['nickname'] = acs_escape_string($user_login); $userData['user_email'] = acs_escape_string($user_email); $userData['wp_capabilities'] = "subscriber"; $userData['user_registered'] = date('Y-m-d H:i:s'); //write new WordPress user account to database $user_id = wp_insert_user($userData); if (is_numeric($user_id)) { $user = new WP_user($user_id); //store metadata for the identity provider used, and the unique ID. add_user_meta( $user_id, acs_user_uuid, acs_escape_string($claims[USER_UUID]) ); add_user_meta( $user_id, acs_identity_provider, acs_escape_string($claims[USER_IDP]) ); } elseif ($user_id instanceof WP_Error) { $user = $user_id; } else { $user = new WP_Error('login_error', 'Problem creating an acccount (non-numeric ID returned)'); } //debug //foreach ($userData as $key => $value) //{ // print ""; //} //print ""; //print ""; } } //handle WordPress login normally if not receving a token from ACS else { $username = sanitize_user($username); $password = trim($password); $user = apply_filters('authenticate', null, $username, $password); if ( $user == null ) { $user = new WP_Error('authentication_failed', __('ERROR: Invalid username or incorrect password.')); } $ignore_codes = array('empty_username', 'empty_password'); if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) { do_action('wp_login_failed', $username); } } return $user; } endif; //This function looks up a user account based on the unique ID and identity provider claims received from ACS function acs_get_user($idp, $uuid) { //return false if arguments are empty if ( empty($idp) || empty($uuid) ) return false; global $wpdb; $sql = $wpdb->prepare("SELECT a.* FROM $wpdb->usermeta as a LEFT JOIN $wpdb->usermeta as b ON a.user_id = b.user_id WHERE (a.meta_key = %s AND a.meta_value = %s) AND (b.meta_key = %s AND b.meta_value = %s)", acs_user_uuid, $uuid, acs_identity_provider, $idp); $lookup = $wpdb->get_results( $sql ); //return false if no data found if ( is_null($lookup) || !count($lookup) ) return false; //if multiple accounts were found for a user, return an error if (count($lookup) != 1) { $user = new WP_Error('login_error', 'More than one WordPress user ID was returned. This is an error. Please contact the WordPress administrator. '.$sql); return $user; } $row_id = @$lookup[0]->user_id; //ensure the user ID is valid if (intval($row_id) == 0) { $user = new WP_Error('login_error', 'An invalid WordPress user ID was returned. Please contact the WordPress administrator.'); return $user; } $user = new WP_User($row_id); return $user; } //This function displays an account creation form so users can enter a username, plus an email address if we didn't get that information from the identity provider function acs_create_user_form($wresult, $claims, $errors = null, $user_login = null, $user_email = null) { login_header(__('Registration Form'), '
', $errors); ?> div.SignInContent { text-align: center; margin-left: auto; margin-right: auto; position: relative; width: 100%; height: 100%; } div.Header { padding:10px 10px; text-align: left; margin-left: auto; margin-right: auto; margin-bottom: 10px; } div.LeftArea { width: 100%; height: 100%; } button.IdentityProvider { width: 250px; height: 30px; text-align: center; border: solid 1px #BBBBBB; margin-left: auto; margin-right: auto; margin-bottom: 5px; position: relative; cursor: pointer; font-size: 15px; color: blue; background: #F7F7F7; background: -webkit-gradient(linear, left top, left bottom, from(#FFFFFF), to(#EEEEEE)); background: -moz-linear-gradient(bottom, #EEEEEE, #FFFFFF); filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#FFFFFF, EndColorStr=#EEEEEE); } img.IdentityProviderImage { vertical-align: middle; postion: relative; } button.IdentityProvider:hover { background: #EEEEEE; background: -moz-linear-gradient(bottom, #DDDDDD, #FFFFFF); background: -webkit-gradient(linear, left top, left bottom, from(#FFFFFF), to(#DDDDDD)); filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#FFFFFF, EndColorStr=#DDDDDD); } label { color: red; } '; } //This function generates the login page function acs_login_form() { print '