使用PHP实现OpenID

7
我对实现OpenID很感兴趣,已经阅读了相关内容,但仍有一些方面让我有点困惑。
我看过多个交互流程图和详细的步骤说明,例如this one,但它们都跳过了关于成功登录后发生的事情的细节。我所读的所有内容都说类似于“成功登录后,用户将被重定向回站点。”那么,我的站点如何知道登录成功了呢?是设置cookie,还是获得POST返回,还是其他什么?
例如,这是我包含的链接中的详细信息。
9. User POSTs response to OpenID Server.
10. User is redirected to either the success URL or the failure URL returned in (5) depending on the User response

//this is the step that it says tells me I've had a succes/failure upon login
5. Consumer inspects the HTML document header for <link/> tags with the attribute rel set to openid.server and, optionally, openid.delegate. The Consumer uses the values in these tags to construct a URL with mode checkid_setup for the Identity Server and redirects the User Agent. This checkid_setup URL encodes, among other things, a URL to return to in case of success and one to return to in the case of failure or cancellation of the request

我不太确定如何解释这个。具体是什么让我知道登录成功了?据我所知,似乎是在头部设置了某些内容,但我如何访问它?假设我发现登录成功并已登录,那么是否意味着我可以继续设置与我的站点相关的cookie/会话?
编辑-我找到了LightOpenID,它似乎符合我的需求,但我仍然有些不确定。
我在本地主机上进行了测试,并成功使用Google登录。登录后,我收到一个类似于以下的URL:
User https://www.google.com/accounts/o8/id?id=sdlkfjlkwliej9392010fjos has logged in.

检查代码,它是由以下生成的。
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';

我假设这意味着我只需检查$openid->validate()以进行登录?对于给定的Google帐户,$openid->identity每次都相同吗?我假设是的,否则就没有办法每次跟踪用户。如果用户已经登录,那么我可以设置cookie、会话和其他有趣的东西,对吧?

更多信息请参见:https://dev59.com/-2865IYBdhLWcg3wFqdV - ethrbunny
之前的答案可能已经完成,可以参考https://dev59.com/Am035IYBdhLWcg3wW-31#15848917。 - Thierry M.S.
1个回答

1
这是我使用的一些代码:

require '../../php/lightopenid-lightopenid/openid.php';

if( isset( $_COOKIE[ 'claimed_id' ] ))
{
    $claimed_id = $_COOKIE[ 'claimed_id' ];
    try
    {

            if(!isset($_GET['openid_mode']))
            {
                            $openid = new LightOpenID;
                            $openid->identity = 'https://www.google.com/accounts/o8/id';
                            header('Location: ' . $openid->authUrl());
            }
            elseif($_GET['openid_mode'] == 'cancel')
            {
                    unset( $claimed_id );
                    setcookie( "claimed_id", 0, time() - 3600, "/" );
            }
            else
            {
                    $openid = new LightOpenID;

                    if( $openid->validate() )
                    {
                    // different login
                            if ( $_REQUEST[ 'openid_claimed_id' ] != $claimed_id )
                            {
                                    unset( $claimed_id );
                                    setcookie( "claimed_id", 0, time() - 3600, "/" );
                            }
                    }
                    else
                    {
                    // cant validate
                            unset( $claimed_id );
                            setcookie( "claimed_id", 0, time() - 3600, "/" );
                    }
            }
    }
    catch(ErrorException $e)
    {
            echo "Authentication error.";
            error_log( $e->getMessage() );
            exit;
    }
}

// fall through to rest of code...

谢谢!你介意看一下我的编辑,看看我想的是否正确吗? - user1104854
据我所知...已经有一段时间了..在成功后,您会从OAuth中获得一些位。检查_REQUEST头,例如:foreach($ _REQUEST as $ key => $ value) { if(preg_match("/ ^(p | iid | claimed_id)$ /",$ key)) echo $ _REQUEST [$ key] .\ n";} - ethrbunny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接