简体   繁体   English

PHP 使用 Google oAuth 将站点集成到 Thingsboard,无需每次都要求用户登录

[英]PHP Site integration to Thingsboard using Google oAuth, without asking user to login everytime

I am trying to use google oAuth - openid to link Thingsboard (IoT open source) with one another site.我正在尝试使用 google oAuth - openid 将 Thingsboard(物联网开源)与另一个站点链接。 I am able to do that successfully once, when I login to google account.当我登录谷歌帐户时,我能够成功地做到这一点。 But my requirement is to use the auth code / access token next time and get the request authenticated.但我的要求是下次使用身份验证码/访问令牌并通过身份验证请求。 User should not be asked to login to the google account again.不应要求用户再次登录谷歌帐户。

Code:代码:

<?php
ob_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName("My APP");
$client->setClientId('Redacted');
$client->setClientSecret('Redacted');
$client->setRedirectUri('REDIRECT URLS'); //This same page.
$client->setAccessType("offline");
$client->addScope("email");
$client->addScope("profile");
$client->addScope("openid");
$client->setPrompt("consent");
$client->setApprovalPrompt("force");
$strtoken = getToken();//This will get the token from the file, if token exists.

if($strtoken <> '') //If the token exists retrived from the file, no need to ask user to login. 
{​​​​
    $new_token = "";
    $accessToken="";
    echo "strtoken" . $strtoken;
    $accessToken = json_decode(file_get_contents("/home/www/token.txt"), true);
    $ref_token = $accessToken['refresh_token'];
    $access_token= $accessToken['access_token'];

    $client->setAccessToken($accessToken);
    if ($client->isAccessTokenExpired()) {​​​​
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        updateToken(json_encode($client->getAccessToken()));
    }​​​​
    header("Location: IOT URL"); //The Thingsboard home page url. 
    die;
}​​​​

if(isset($_GET['code']))
{​​​​
    $client->authenticate($_GET['code']);
    $arrtoken = $client->getAccessToken();
    $access_token = $arrtoken['access_token'];
    $tokens_encoded = json_encode($arrtoken);
    $client->setAccessToken($arrtoken['access_token']);
    updateToken($tokens_encoded);
    header("Location: IOT URL");//The Thingsboard home page url. 
    die();
}​​​​
if($strtoken == "" && !(isset($_GET['code'])))
{​​​​?>
    <a class="login-btn" href="<?php echo $client->createAuthUrl(); ?>">Login</a><br>
<?php}​​​​
function updateToken($token)
{​​​​
    $myfile = fopen("token.txt", "w+");
    fwrite($myfile, $token); 
    fclose($myfile);
}​​​​

function getToken()
{​​​​
    $token = "";
    $filename = "/home/www/token.txt";
    $fh = fopen($filename,'r');
    while ($line = fgets($fh)) 
    {​​​​
      $token  = $line;
    }​​​​
    fclose($fh);    
    return $token;    
}​​​​
?>

I can see from comments that you appear to have a refresh token in that file.我可以从评论中看到,您似乎在该文件中有一个刷新令牌。 That's good now check that the refresh token is in this value $ref_token = $accessToken['refresh_token'];很好,现在检查刷新令牌是否在此值中 $ref_token = $accessToken['refresh_token']; So $ref_token should be the same value thats in the the file.所以 $ref_token 应该与文件中的值相同。

Once thats done you need to make sure that its set on the client完成后,您需要确保它在客户端上设置

$client->refreshToken($ref_token);

Once that is set you should be able to use一旦设置好,您应该可以使用

$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());

Remember though if you get a expired token at this point its the refresh token thats expired and you need to authorize the user again to request a new one.请记住,如果此时您获得过期令牌,则刷新令牌已过期,您需要再次授权用户以请求新令牌。

clarification.澄清。

From comments i think you are a little confused as to what your code is going to allow you to do.从评论中我认为你对你的代码将允许你做什么有点困惑。

$client->addScope("email");
$client->addScope("profile");
$client->addScope("openid");

What you are doing is requesting authorization of the user to access their profile data.您正在做的是请求用户授权访问他们的个人资料数据。 This means that you could use the Google people api to request information of the user.这意味着您可以使用 Google people api 来请求用户信息。 your app has been granted consent to access the users data this is called Authorization.您的应用已获准访问用户数据,这称为授权。

The following statement from comments以下来自评论的声明

When I redirect the user to Thingsboard dashboard url, it is not recognizing this oauth user and throws to login page.当我将用户重定向到 Thingsboard 仪表板 url 时,它无法识别此 oauth 用户并引发登录页面。

In order to access a Thingsboard Dashboard, your user needs to be logged into Thingsboard.为了访问 Thingsboard 仪表板,您的用户需要登录到 Thingsboard。 This is called Authentication or signin you are authenticating that the user behind the machine is in in fact the owner of the account, they have a login and password so they can sign in.这称为身份验证或登录,您正在验证机器背后的用户实际上是帐户的所有者,他们有登录名和密码,因此他们可以登录。

The first thing you need to understand that authorization and authencation are two different things.您需要了解的第一件事是授权和身份验证是两件不同的事情。

Then you need to understand as far as authentication goes.然后,您需要了解身份验证。 Google and Thingsboard are two different companies. Google 和 Thingsboard 是两家不同的公司。 They each have their own authorization server which allow their users to sign in to their applications.他们每个人都有自己的授权服务器,允许他们的用户登录他们的应用程序。

Google can not help you sign in to Thingsboard web app only Thingsboard has access to do that. Google 无法帮助您登录 Thingsboard web 应用程序,只有 Thingsboard 有权执行此操作。

So no php code you can create using a Google Oauth is going to allow you to signin to Thingsboard web application.因此,您可以使用 Google Oauth 创建的 php 代码将允许您登录 Thingsboard web 应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM