wakeonweb / kong-oauth2-server-bundle
Kong OAuth2 Server Bundle
Requires
- guzzlehttp/guzzle: ~6.3
This package is auto-updated.
Last update: 2024-09-08 07:19:43 UTC
README
注意:该包处理与Kong的技术通信并提供授权页面。其他安全内容应由应用程序管理(表单登录、重置密码等)。
配置
/.env
KONG_ADMIN_URL=http://localhost:8001
KONG_API_URL=https://localhost:8443
KONG_PROVISION_KEY=34cbcb435c02b87c9cd9d68ac6c86e2c
/config/packages/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server:
kong:
admin_url: '%env(KONG_ADMIN_URL)%'
api_url: '%env(KONG_API_URL)%'
provision_key: '%env(KONG_PROVISION_KEY)%'
cancel_path: /logout
/config/routes/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server.authenticate:
path: /authenticate
controller: WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController
wakeonweb_kong_oauth2_server.login:
path: /api/login
controller: WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\LoginController
/config/packages/security.yaml
security:
# ...
firewalls:
anonymous: true
api:
pattern: ^/api/login$
anonymous: ~
json_login:
check_path: /api/login
main:
form_login:
login_path: /login
check_path: /login
access_control:
- { path: /api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /, roles: IS_AUTHENTICATED_FULLY }
OAuth2
OAuth2规范描述了4种授权类型
- 授权码授权
- 隐式授权
- 客户端凭据授权
- 客户端凭据授权
其中3种直接支持。目前的"客户端凭据授权"非常具体,将在以后实现。
授权码授权
"授权码授权"流程由WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController
处理。只需确保添加路由以处理即可。
/config/routes/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server.authenticate:
path: /authenticate
controller: WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController
客户端应用程序必须将最终用户路由到以下URL: http://sso.com/authorize?client_id=<id>&response_type=code
。Kong和该包将处理其余部分。
隐式授权
"隐式授权"流程由WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController
处理。只需确保添加路由以处理即可。
/config/routes/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server.authenticate:
path: /authenticate
controller: WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController
客户端应用程序必须将最终用户路由到以下URL: http://sso.com/authorize?client_id=<id>&response_type=token
。Kong和该包将处理其余部分。
客户端凭据授权
"资源所有者密码凭据授权"流程由WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\LoginController
处理。只需确保添加路由和防火墙以处理即可。
/config/routes/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server.login:
path: /api/login
controller: WakeOnWeb\Bundle\KongOAuth2ServerBundle\Controller\LoginController
/config/packages/security.yaml
security:
# ...
firewalls:
anonymous: true
api:
pattern: ^/api/login$
anonymous: ~
json_login:
check_path: /api/login
access_control:
- { path: /api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /, roles: IS_AUTHENTICATED_FULLY }
客户端凭据授权
尚不支持
IdentityResolver
用户身份可以使用IdentityResolver
从UserInterface
解析出来。默认使用"用户名作为身份"策略。这意味着将使用UserInterface::getUsername()
的结果作为用户ID。您可以通过实现自己的IdentityResolver
来更改此行为。
src/User/IdentityAsIs.php
<?php
namespace App\User;
use WakeOnWeb\Bundle\KongOAuth2ServerBundle\User\IdentityResolver;
use Symfony\Component\Security\Core\User\UserInterface;
class IdentityAsIs implements IdentityResolver
{
public function resolveIdentity(UserInterface $user): string
{
return $user->getId();
}
}
/config/packages/wakeonweb_kong_oauth2_server.yaml
wakeonweb_kong_oauth2_server:
# ...
identity_resolver: App\User\IdentityAsIs