happyr / google-site-authenticator-bundle
使用Google API验证您的网站(而不是您的用户)。可以作为域内授权代理使用
Requires
- php: ^7.1
- google/apiclient: ~1.1, <=1.1.7
- psr/cache: ^1.0
- psr/cache-implementation: ^1.0
- symfony/config: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/event-dispatcher: ^4.4 || ^5.0
- symfony/framework-bundle: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
Requires (Dev)
- cache/adapter-bundle: ^1.0
- cache/void-adapter: ^1.0
- matthiasnoback/symfony-dependency-injection-test: ^1.0
- phpunit/phpunit: ^4.5 || ^5.4
- symfony/browser-kit: ^4.4 || ^5.0
- symfony/filesystem: ^4.4 || ^5.0
README
在某些情况下,您可能希望网站代表您(而不是您的用户)进行API请求。例如,当您想从Google Analytics获取网站数据或将数据库转储上传到Google Drive时。Google为此提供的解决方案是域内授权代理。但该解决方案需要您是Google Apps的付费客户。我需要一个免费的解决方案,因此我创建了此捆绑包。
此捆绑包使用标准的OAuth进行Web应用程序,但验证的是您的Google账户(或账户),而不是您的用户。它将访问令牌保存到缓存中,直到您手动撤销它。
请阅读此README文件的全部内容,以了解如何开始和验证
安装
使用composer获取此捆绑包。您还必须获取PSR-6缓存实现。
$ php composer.phar require happyr/google-site-authenticator-bundle cache/redis-adapter
在AppKernel中激活此捆绑包。
new Happyr\GoogleSiteAuthenticatorBundle\HappyrGoogleSiteAuthenticatorBundle(),
包含routing.yml并确保它受到正常用户的保护。
// app/config/routing.yml happyr_google_site_authenticator: resource: "@HappyrGoogleSiteAuthenticatorBundle/Resources/config/routing.yml" prefix: /admin
获取API凭证
您可以在Google控制台中找到所有信息。进入控制台,在侧边栏中单击“API”,以选择您想要使用的API。
要检索API密钥和秘密,请单击侧边栏中的“凭证”,然后单击“创建新ClientID”。并为web应用程序创建一个客户端ID。确保指定正确的授权重定向URI。如果您使用了上面的配置,则应使用以下URL
http://www.domain.com/admin/authenticate-google/return-url
完成操作后,您将获得一个客户端ID和一个客户端秘密。在下一节中保存这些信息。
配置
此捆绑包将获取一个访问令牌并将其保存到缓存中。PHPCacheAdapterBundle是此用途的优秀捆绑包。您可以使用许多预定义的缓存提供程序之一,例如;文件系统、apc、mongodb等。有关缓存的信息,请参阅http://www.php-cache.com/
它还允许您创建自己的缓存提供程序。以下是一个示例配置
cache_adapter: providers: my_redis: factory: 'cache.factory.redis'
要配置Happyr Google Site Authenticator捆绑包,您需要添加您的API凭证并选择一个扩展Psr\Cache\CacheItemInterface
的服务。如果您使用的是上面的配置,则可以使用以下值
happyr_google_site_authenticator: cache_service: 'cache.provider.my_redis' tokens: google_drive: client_id: '00000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com' client_secret: 'xxxxx-xxxxx_xxxxxxxxxxxx' redirect_url: 'http://www.domain.com/admin/authenticate-google/return-url' scopes: ['https://www.googleapis.com/auth/drive']
您可以在此处找到所有可用的作用域。
上述配置将配置一个名为google_drive的令牌。当然,您还可以配置更多令牌。要获取具有这些凭证的Google_Client实例
$clientProvider = $this->get('happyr.google.client_provider'); $client = $clientProvider->getClient('google_drive'); // or don't use the client provider $client = $this->get('google.client.google_drive'); // if you only have one token configured $client = $this->get('google.client');
验证
为了确保您获取了一个访问令牌,您需要导航到http://www.domain.com/admin/authenticate-google
并单击验证。您将被要求使用您的Google账户登录并授予权限。获取的访问令牌将被缓存服务保存。您需要确保此信息被存储很长时间。
验证后,您可以使用happyr.google.client_provider
获取一个经过验证的客户。