altaviatech / kong-oauth2-server-bundle

Kong OAuth2 Server Bundle

安装: 83

依赖: 0

建议者: 0

安全: 0

类型:symfony-bundle

v1.0.0 2019-05-24 10:20 UTC

This package is auto-updated.

Last update: 2020-06-25 14:48:16 UTC


README

注意: 该包处理与Kong的技术通信并提供授权页面。其他安全功能应由应用程序(表单登录、重置密码等)管理。

配置

/.env

KONG_ADMIN_URL=https://:8001
KONG_API_URL=https://:8443
KONG_PROVISION_KEY=34cbcb435c02b87c9cd9d68ac6c86e2c

_/config/packages/altaviatech_kong_oauth2_server.yaml

altaviatech_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/altaviatech_kong_oauth2_server.yaml

altaviatech_kong_oauth2_server.authenticate:
    path: /authenticate
    controller: AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController

altaviatech_kong_oauth2_server.login:
    path: /api/login
    controller: AltaviaTech\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种支持即插即用。目前“客户端凭据授权”非常具体,将在以后实现。

授权码授权

“授权码授权”工作流程由 AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController 处理。只需确保添加到该控制器的路由以处理它。

_/config/routes/altaviatech_kong_oauth2_server.yaml

altaviatech_kong_oauth2_server.authenticate:
    path: /authenticate
    controller: AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController

客户端应用程序必须将最终用户重定向到以下URL: http://sso.com/authorize?client_id=<id>&response_type=code。Kong和该包将完成其余工作。

隐式授权

“隐式授权”工作流程由 AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController 处理。只需确保添加到该控制器的路由以处理它。

_/config/routes/altaviatech_kong_oauth2_server.yaml

altaviatech_kong_oauth2_server.authenticate:
    path: /authenticate
    controller: AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\AuthenticateController

客户端应用程序必须将最终用户重定向到以下URL: http://sso.com/authorize?client_id=<id>&response_type=token。Kong和该包将完成其余工作。

资源所有者密码凭据授权

“资源所有者密码凭据授权”工作流程由 AltaviaTech\Bundle\KongOAuth2ServerBundle\Controller\LoginController 处理。只需确保添加到该控制器的路由和安全性防火墙以处理它。

_/config/routes/altaviatech_kong_oauth2_server.yaml

altaviatech_kong_oauth2_server.login:
    path: /api/login
    controller: AltaviaTech\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

用户身份可以从 UserInterface 使用 IdentityResolver 解析。默认使用“用户名作为身份”策略。这意味着将使用 UserInterface::getUsername() 的结果作为用户ID。您可以通过实现自己的 IdentityResolver 来更改此行为。

src/User/IdentityAsIs.php

<?php

namespace App\User;

use AltaviaTech\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/altaviatech_kong_oauth2_server.yaml

altaviatech_kong_oauth2_server:
    # ...
    identity_resolver: App\User\IdentityAsIs