attraction/google-iap-authentication

使用 Google IAP 进行用户身份验证

此包的官方仓库似乎已删除,因此该包已被冻结。

2.0.0 2021-01-13 16:36 UTC

This package is auto-updated.

Last update: 2024-05-14 00:13:56 UTC


README

一个简单的类,帮助您通过 Google Identity-Aware Proxy 验证访问您资源的用户。

安装

建议您使用 Composer 安装 google-iap-authentication。

$ composer require attraction/google-iap-authentication:^2.0

这将安装 google-iap-authentication 及所有所需的依赖项。google-iap-authentication 需要 PHP 7.3 或更高版本。

使用

use Attraction\GoogleIAPAuthentication;

// Project ID and Number are available in Google Cloud Project Settings - https://console.cloud.google.com/iam-admin/settings
$projectId = 'YOUR_PROJECT_ID';
$projectNumber = 'YOUR_PROJECT_NUMBER';

// You can also use your preferred framework to fetch the headers, here we use getallheaders() to make it simple.
$idToken = getallheaders()['X-Goog-Iap-Jwt-Assertion'] ?? '';

$iap = new GoogleIAPAuthentication($projectId, $projectNumber);

try {
    $user = $iap->validateAssertion($idToken);
} catch(Throwable $e) {
    print(sprintf('Unable to proceed: %s',$e->getMessage()));
}

print(sprintf('User logged in - Email Address: %s - User ID: %s',$user->emailAddress,$user->userId));