开发者

How to implement openid google, yahoo and others

开发者 https://www.devze.com 2023-03-06 22:32 出处:网络
I am trying to implement OpenID and for that I download http://www.openidenabled.com/php-openid and from that I picked up Auth folder with changing any thing to localhost directory and created a index

I am trying to implement OpenID and for that I download http://www.openidenabled.com/php-openid and from that I picked up Auth folder with changing any thing to localhost directory and created a index.php file and whose code is as below:

<?php
    if (!isset($_POST['submit'])) {
?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <title>OPENID TESTING</title>
        </head>
        <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            Sign in with your OpenID: <br/>
            <input type="text" name="id" size="30" />
            <br />
            <input type="submit" name="submit" value="Log In" />
        </form>
        </body>
        </html>
<?php
    } else {

        // check for form input
        if (trim($_POST['id'] == '')) {
            die("ERROR: Please enter a valid OpenID.");
        }

        // include files
        require_once "Auth/OpenID/Consumer.php";
        require_once "Auth/OpenID/FileStore.php";

        // start session (needed for YADIS)
        session_start();

        // create file storage area for OpenID data
        $store = new Auth_OpenID_FileStore('./oid_store');

        // create OpenID co开发者_运维技巧nsumer
        $consumer = new Auth_OpenID_Consumer($store);

        // begin sign-in process
        // create an authentication request to the OpenID provider

        $auth = $consumer->begin($_POST['id']);
        if (!$auth) {
            die("ERROR: Please enter a valid OpenID.");
        }

        // redirect to OpenID provider for authentication
        $url = $auth->redirectURL('http://localhost/', 'http://localhost/oid_return.php');
        header('Location: ' . $url);
    }
?>

Now when I try to fill id from worldpress or myopenid.com is recognised and transfer me to the provider page for authentication but this is not happening with Google, yahoo or others service providers case. What I have to implement for Google or Yahoo


I am using the PHP LightOpenID library (see on gitorious) to make users able to log into my website with their Google account. For other OpenID provider, you just have to change the $openid->identity field. It handles all the authentication flow for us. You don't need to bother about token and stuff.

Here the page where I display the "Login with Google" link :

<?php
require_once 'openid.php';
$openid = new LightOpenID;

$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
$openid->returnUrl = 'http://my-website.com/landing-login.php'
?>

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>

When the click on the link, a Google page will appear ask him to authenticate and/or authorize you to retrieve his email.

Then he will be redirect to the landing page $openid->returnUrl. The code for that page should be :

<?php
require_once 'openid.php';
$openid = new LightOpenID;

if ($openid->mode) {
    if ($openid->mode == 'cancel') {
        // User has canceled authentication
    } elseif($openid->validate()) {
        // Yeah !
        $data = $openid->getAttributes();
        $email = $data['contact/email'];
    } else {
        // The user has not logged in via Google
    }
} else {
    // The user does not come from the link of the first page
}
?>

If you want to retrieve more info from the user, you have to add them to $openid->required in the first page. For instance :

$openid->required = array(
  'contact/email',
  'namePerson/first',
  'namePerson/last'
);

will let you, if the user accepts it, to get his first and last names as well in the second page :

$name = $data['namePerson/first'] . " " . $data['namePerson/last'];

Hope that helps !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号