function amazon_login_process()
{
    if (LoginWithAmazonUtility::shouldProcessAmazonLogin() || LoginWithAmazonUtility::shouldReregister()) {
        $access_token = LoginWithAmazonUtility::getAcessToken();
        if ($access_token) {
            // Ensure CSRF token present and valid
            $csrf_token = LoginWithAmazonUtility::getCsrfToken();
            if ($csrf_token && LoginWithAmazonUtility::verifyCsrfToken($csrf_token)) {
                $email = LoginWithAmazonUtility::getEmailFromAccessToken($access_token);
                if ($email) {
                    // Find or create an Amazon user
                    $user = LoginWithAmazonUtility::findOrCreateUserByEmail($email);
                    if (is_wp_error($user)) {
                        // Display error on the login form.
                        LoginWithAmazonUtility::addLoginError($user->get_error_message());
                    } else {
                        // Log in the Amazon user and redirect to the homepage.
                        LoginWithAmazonUtility::createSessionFromUser($user);
                    }
                } else {
                    // Could not retrieve email with the token. Provide a general login error to the user.
                    $error_msg = "There was an error when attempting to log you in.";
                    LoginWithAmazonUtility::addLoginError(__($error_msg, LoginWithAmazonUtility::$I18N_DOMAIN));
                }
            }
        } else {
            add_action('login_enqueue_scripts', 'loginwithamazon_enqueue_nonsecure_script');
        }
    }
}
function loginwithamazon_add_footer_script()
{
    $popup = 'false';
    if (!empty($_SERVER['HTTPS'])) {
        $popup = 'true';
    }
    $csrf = LoginWithAmazonUtility::hmac($_SESSION[LoginWithAmazonUtility::$CSRF_AUTHENTICATOR_KEY]);
    ?>
    <div id="amazon-root"></div>
    <script type="text/javascript">

        window.onAmazonLoginReady = function() {
            amazon.Login.setClientId('<?php 
    echo get_option('loginwithamazon_client_id');
    ?>
');
            amazon.Login.setUseCookie(true);
            <?php 
    if (isset($_GET['loggedout']) && $_GET['loggedout'] == 'true') {
        ?>
            amazon.Login.logout();
            <?php 
    }
    ?>
        };
        (function(d) {
            var a = d.createElement('script'); a.type = 'text/javascript';
            a.async = true; a.id = 'amazon-login-sdk';
            a.src = 'https://api-cdn.amazon.com/sdk/login1.js';
            d.getElementById('amazon-root').appendChild(a);
        })(document);

        function activateLoginWithAmazonButtons(elementId) {
            document.getElementById(elementId).onclick = function() {
                var options = {
                    scope: 'profile',
                    state: '<?php 
    echo $csrf;
    ?>
',
                    popup: <?php 
    echo $popup;
    ?>
                };
                amazon.Login.authorize(options, '<?php 
    echo str_replace('http://', 'https://', site_url('wp-login.php'));
    ?>
?amazonLogin=1');

                return false;
            };
        }
    </script>

<?php 
}
Ejemplo n.º 3
0
 /**
  * Set a login form message and call the filter so that it will display
  *
  * @param string $message
  */
 public static function addLoginError($message)
 {
     self::$login_error_add = $message;
     add_filter('login_message', array('LoginWithAmazonUtility', 'displayErrorOnLoginForm'), 10, 0);
 }