如何检查用户是否还在用PHP OAUTH登录?

问题描述 投票:0回答:1

我正在构建一个对Azure AD进行OAUTH认证的Web应用,我的意思是,你通过你的Azure AD Office365账户登录到我的应用,登录成功后,我将Azure AD用户的GUID与我的Mysql数据库中的用户进行匹配,我在用户登录的会话信息中设置。

我使用了这个howto来了解它的基础知识。https:/kvaes.wordpress.com20161021azure-using-php-to-go-all-oauth2-on-the-management-api。

我用于将用户登录到我的PHP应用程序的基本代码。

//User is not logged in, try to log in
$provider = new TheNetworg\OAuth2\Client\Provider\Azure([
 'clientId' => getsetting('aadsso_clientid'),
 'clientSecret' => getsetting('aadsso_secret'),
 'redirectUri' => getsetting('aadsso_redirecturl')
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    try {
      // Try to get an access token (using the authorization code grant)
      $token = $provider->getAccessToken('authorization_code', [
          'code' => $_GET['code'],
          'resource' => 'https://graph.windows.net',
      ]);
    } catch (Exception $e) {

        exit ("Connection error. Contact the administrator");
    }

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details (AAD login succeeded)
        $me = $provider->get("me", $token);

        //Some code here LEFT OUT, to find the user in my MySQL database.

        $_SESSION['loggedin'] = 1;
        $_SESSION['aadguid']  = $me['objectId'];



    } catch (Exception $e) {


        // Failed to get user details
        exit ("Connection error. Contact the administrator");
    }

}

很好用!但是,当我设置"$_SESSION['loggedin'] = 1; "时,用户已经登录到应用程序中。

我真的很想了解如何让用户在注销Azure ADOffice365会话时,退出我的应用程序。例如,他登录到我的应用程序,进入outlook.office.com,在那里点击注销。当他回到我的应用程序并尝试加载页面时,我的应用程序应该 "看到 "他已经注销了 Azure AD。

如何实现这一点,而又不损失我的应用程序的性能?

我在这方面相当菜鸟,但请给我一些提示。

php oauth-2.0 azure-active-directory
1个回答
1
投票

这可能和你使用的代码不一样,但microsoft有一个关于PHP如何认证的例子。https:/docs.microsoft.comen-cagraphtutorialsphp?tutorial-step=3。你会看到,在这个例子中的某个地方,他们实现了laravel的注销网址路由。

然后在您的应用程序注册下的身份验证-> 有一个部分称为注销网址,如果你指定一个网址,那么如果用户登录了azure SSO,它将发送一个调用到该注销网址,有可能执行你实现的注销网址。

我知道这并不是您所要求的在您的代码中添加它,但请看一下这个例子,您可能会想测试一下。

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.