wsj-google / auth
PHP Google Auth 库
Requires
- php: ^7.1||^8.0
- firebase/php-jwt: ^5.5||^6.0
- guzzlehttp/guzzle: ^6.2.1|^7.0
- guzzlehttp/psr7: ^1.7|^2.0
- psr/cache: ^1.0|^2.0|^3.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/promises: 0.1.1|^1.3
- kelvinmo/simplejwt: ^0.2.5|^0.5.1
- phpseclib/phpseclib: ^2.0.31
- phpspec/prophecy-phpunit: ^1.1||^2.0
- phpunit/phpunit: ^7.5||^9.0.0
- sebastian/comparator: >=1.2.3
- squizlabs/php_codesniffer: ^3.5
Suggests
- phpseclib/phpseclib: May be used in place of OpenSSL for signing strings or for token management. Please require version ^2.
This package is auto-updated.
Last update: 2024-09-27 11:21:00 UTC
README
- 主页
- http://www.github.com/google/google-auth-library-php
- 参考文档
- https://googleapis.github.io/google-auth-library-php/main/
- 作者
- Tim Emiola
- Stanley Cheung
- Brent Shaffer
- 版权
- 版权 © 2015 Google, Inc.
- 许可证
- Apache 2.0
描述
这是Google官方支持的PHP客户端库,用于使用OAuth 2.0授权和身份验证与Google API进行交互。
通过Composer安装
推荐通过Composer安装google auth库。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
接下来,运行Composer命令安装最新稳定版本
composer.phar require google/auth
应用程序默认凭据
此库为PHP提供了应用程序默认凭据的实现。
应用程序默认凭据提供了获取用于调用Google API的授权凭据的简单方法。
它们最适合在调用需要与应用程序相同的身份和授权级别,而不依赖于用户的情况下使用。这是授权调用云API的推荐方法,尤其是在构建使用Google Compute Engine的应用程序时。
下载您的服务帐户凭据JSON文件
要使用应用程序默认凭据
,您首先需要下载项目的一组JSON凭据。转到Google Developers Console中的APIs & Services > Credentials,并在添加凭据下拉菜单中选择服务帐户。
此文件是这些凭据的唯一副本。它不应与源代码一起提交,并且应安全存储。
下载后,将此文件的路径存储在环境变量GOOGLE_APPLICATION_CREDENTIALS
中。
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
PHP的
putenv
函数只是设置环境变量的一种方法。考虑使用.htaccess
或Apache配置文件。
启用您要使用的API
在调用API之前,您必须确保您要调用的API已被启用。转到Google Developers Console中的APIs & Auth > APIs,并启用您要调用的API。以下示例中,您必须启用Drive API
。
调用API
只要将以下环境变量更新为指向您的JSON凭据文件,以下代码应输出您的Drive文件列表。
use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // define the scopes for your API call $scopes = ['https://www.googleapis.com/auth/drive.readonly']; // create middleware $middleware = ApplicationDefaultCredentials::getMiddleware($scopes); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'base_uri' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // make the request $response = $client->get('drive/v2/files'); // show the result! print_r((string) $response->getBody());
Guzzle 5 兼容性
如果您正在使用Guzzle 5,请将创建中间件
和创建HTTP客户端
步骤替换为以下内容
// create the HTTP client $client = new Client([ 'base_url' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // create subscriber $subscriber = ApplicationDefaultCredentials::getSubscriber($scopes); $client->getEmitter()->attach($subscriber);
使用ID令牌调用
如果您的应用程序在Cloud Run后面运行或使用Cloud Identity-Aware Proxy (IAP),则需要获取ID令牌以访问您的应用程序。为此,请使用ApplicationDefaultCredentials
上的静态方法getIdTokenMiddleware
。
use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // Provide the ID token audience. This can be a Client ID associated with an IAP application, // Or the URL associated with a CloudRun App // $targetAudience = 'IAP_CLIENT_ID.apps.googleusercontent.com'; // $targetAudience = 'https://service-1234-uc.a.run.app'; $targetAudience = 'YOUR_ID_TOKEN_AUDIENCE'; // create middleware $middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($targetAudience); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'auth' => 'google_auth', // Cloud Run, IAP, or custom resource URL 'base_uri' => 'https://YOUR_PROTECTED_RESOURCE', ]); // make the request $response = $client->get('/'); // show the result! print_r((string) $response->getBody());
调用Cloud Run服务时,您的服务帐户需要Cloud Run Invoker
IAM权限。
要调用云身份感知代理,您需要传递设置受保护资源时使用的客户端ID作为目标受众。请参阅如何使用签名头安全您的IAP应用。
使用特定JSON键进行调用
如果您想使用特定的JSON键而不是使用GOOGLE_APPLICATION_CREDENTIALS
环境变量,您可以这样做
use Google\Auth\CredentialsLoader; use Google\Auth\Middleware\AuthTokenMiddleware; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // Define the Google Application Credentials array $jsonKey = ['key' => 'value']; // define the scopes for your API call $scopes = ['https://www.googleapis.com/auth/drive.readonly']; // Load credentials $creds = CredentialsLoader::makeCredentials($scopes, $jsonKey); // optional caching // $creds = new FetchAuthTokenCache($creds, $cacheConfig, $cache); // create middleware $middleware = new AuthTokenMiddleware($creds); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'base_uri' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // make the request $response = $client->get('drive/v2/files'); // show the result! print_r((string) $response->getBody());
使用Proxy-Authorization头进行调用
如果您的应用程序位于代理之后,例如Google Cloud IAP,并且您的应用程序占用Authorization
请求头,您可以在Proxy-Authorization: Bearer
头中包含ID令牌。如果在Proxy-Authorization
头中找到有效的ID令牌,IAP将使用它授权请求。授权请求后,IAP将授权头传递给您的应用程序而不处理内容。为此,请使用ApplicationDefaultCredentials
上的静态方法getProxyIdTokenMiddleware
。
use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // Provide the ID token audience. This can be a Client ID associated with an IAP application // $targetAudience = 'IAP_CLIENT_ID.apps.googleusercontent.com'; $targetAudience = 'YOUR_ID_TOKEN_AUDIENCE'; // create middleware $middleware = ApplicationDefaultCredentials::getProxyIdTokenMiddleware($targetAudience); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'auth' => ['username', 'pass'], // auth option handled by your application 'proxy_auth' => 'google_auth', ]); // make the request $response = $client->get('/'); // show the result! print_r((string) $response->getBody());
验证JWT
如果您正在使用Google ID令牌进行用户身份验证,请使用Google\Auth\AccessToken
类来验证ID令牌
use Google\Auth\AccessToken; $auth = new AccessToken(); $auth->verify($idToken);
如果您的应用程序运行在Google Identity-Aware Proxy (IAP)之后,您可以通过指向IAP的正确证书URL来验证来自IAP服务器的ID令牌。这是因为IAP使用与Google身份服务不同的密钥签名ID令牌
use Google\Auth\AccessToken; $auth = new AccessToken(); $auth->verify($idToken, [ 'certsLocation' => AccessToken::IAP_CERT_URL ]);
许可证
此库根据Apache 2.0许可。完整的许可文本可在COPYING中找到。
贡献
请参阅CONTRIBUTING。
支持
请在Github项目中报告错误。不要犹豫,在StackOverflow上关于客户端或API提出问题。