gwsn / bizhost-auth-bundle
对Bizhost Auth API进行用户认证
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.4
- gwsn/bizhost-auth-sdk: 1.0.*
- symfony/config: ^6.4||^7.0
- symfony/dependency-injection: ^6.4||^7.0
- symfony/http-kernel: ^6.4||^7.0
- symfony/security-bundle: ^6.4||^7.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
有关Symfony Bundle的信息,请查看此包:gwsn/bizhost-auth-bundle
安装
您可以通过composer安装此包
composer require gwsn/bizhost-auth-bundle
首次配置以开始使用
您需要为应用程序请求新的clientId和clientSecret
-
转到
bizhost认证门户
https://auth.bizhost.nl/ -
转到
注册新应用程序
并按照向导操作。
(给它起一个名字,比如我的是 'example-app-authentication') -
当创建应用程序时,记下以下详细信息
-
'应用程序标识符',这将作为您的
$clientId
-
'应用程序密钥',这将作为您的
$clientSecret
(请确保记下这个,因为它只会显示一次)示例
- 认证元url:
https://auth.bizhost.nl/.well-known/oauth-authorization-server
- 认证元url:
Bizhost认证包的基本设置
启用包
将包添加到您的 config/bundles.php
文件
... Bizhost\Authentication\Bundle\AuthenticateBundle::class => ['all' => true], ...
设置Security.yaml
如果您想进行完整认证,其中应用程序将使用代码流重定向到bizhost auth。
将提供者、自定义认证器和访问控制添加到您的 config/packages/security.yaml
文件
security: enable_authenticator_manager: true providers: authenticated_account_provider: id: Bizhost\Authentication\Bundle\Authenticate\AuthenticatedAccountProvider firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: pattern: ^/(account|auth) stateless: false custom_authenticators: - Bizhost\Authentication\Bundle\Authenticate\BizhostAuthAuthenticator entry_point: Bizhost\Authentication\Bundle\Authenticate\BizhostAuthAuthenticator logout: path: app_logout access_control: - { path: ^/account, roles: ROLE_USER } - { path: ^/profile, roles: ROLE_USER }
如果您想进行完整认证,其中应用程序将使用代码流重定向到bizhost auth。
将提供者、自定义认证器和访问控制添加到您的 config/packages/security.yaml
文件
security: enable_authenticator_manager: true firewalls: api: pattern: ^/api stateless: true access_token: token_handler: Bizhost\Authentication\Bundle\Authenticate\BizhostAuthAccessTokenAuthenticator access_control: - { path: ^/api, roles: ROLE_USER }
路由
要开始认证,将使用入口点将用户重定向到bizhost auth服务器。因此,应该触发重定向的每个访问控制后面的路由。
认证成功后,它将重定向用户到/auth/success,您应该在您的 config/routes.yaml
或 config/routes/*.yaml
文件中添加处理此情况的路由
添加退出路由以处理用户退出也是一个好习惯。
auth.callback: path: /auth/callback controller: App\Authentication\Controller\AuthController::handleCallback methods: [ GET ] auth.login.success: path: /auth/success controller: App\Authentication\Controller\AuthController::handleSuccess methods: [ GET ]
控制器
创建一个控制器来处理回调和成功路由。
<?php class AuthController extends AbstractController { public function handleSuccess(): Response { return new RedirectResponse('/home'); } public function handleCallback(): Response { return new RedirectResponse('/home'); } }
环境变量
此包期望以下环境变量被设置为正确的值
BIZHOST_AUTH_CLIENT_ID
和 BIZHOST_AUTH_CLIENT_SECRET
是从bizhost认证门户获得的值。BIZHOST_AUTH_REDIRECT_URL
是在认证成功后用户被重定向到的url,应指向您的应用程序和位于防火墙范围内的路径。
BIZHOST_AUTH_API_URL
是bizhost认证服务器的url。对于测试,我们使用 https://auth-test.bizhost.nl BIZHOST_AUTH_ISSUER_META_DATA_PATH
如果路径不同于默认的 /.well-known/oauth-authorization-server
。您可以在这里更改路径。
在您的 .env
文件中的环境变量示例
###> bizhost/auth-bundle ### BIZHOST_AUTH_API_URL='https://auth.bizhost.nl' BIZHOST_AUTH_CLIENT_ID='client_id_value' BIZHOST_AUTH_CLIENT_SECRET='client_secret_value' BIZHOST_AUTH_REDIRECT_URL='http://url-to-redirect-to-after-authentication' # Optional BIZHOST_AUTH_ISSUER_META_DATA_PATH='/.well-known/oauth-authorization-server' ###< bizhost/auth-bundle ###
AccountService的使用
AccountService是一个可以用来获取认证用户的service,您可以使用它来更新用户并更新以下属性
- firstname
- insertion
- lastname
- userMetadata
- appMetadata
角色可以在Bizhost Auth中设置,如果您需要自定义角色,您可以在appMetadata中设置它们。
要获取当前登录的帐户,您可以使用Bizhost\Authentication\Bundle\Service\AccountService
service并调用getAuthorizedAccount()
。请确保您使用的是正确的AccountService,即来自Bundle的,而不是来自SDK的!
use Bizhost\Authentication\Bundle\Service\AccountService; class Example ; { public function __construct( private AccountService $accountService ) { } publi function exampleMethod() { $authenticatedAccount = $this->accountService->getAuthorizedAccount(); # To get the account $account = $authenticatedAccount->getAccount(); # To get the token and if you need the AccessToken: $token = $authenticatedAccount->getToken(); } }
测试
$ composer run-script test
安全
如果您发现任何与安全相关的问题,请通过电子邮件发送至 support@bizhost.nl 而不是使用问题跟踪器。
许可证
MIT许可证(MIT)。请参阅 许可证文件 获取更多信息。