retitalia/authentication-bundle

此捆绑包实现了通过oauth2的sso google认证

该软件包的规范存储库似乎已丢失,因此已冻结此软件包。

1.4.4 2019-03-15 09:10 UTC

README

此捆绑包实现了与Google的oauth2认证。

注意

捆绑包正在积极开发中,目前不应使用。

文档

此捆绑包与Guard集成,并验证用户是否已登录。如果没有登录,将通过oauth2调用Google进行认证。授权过程正在开发中,并依赖于宿主应用。

安装

步骤1:下载捆绑包

打开命令控制台,进入您的项目目录,并执行以下命令以下载此捆绑包的最新稳定版本

$ composer require retitalia/authentication-bundle

如果返回有关无法安装oauth2库的错误,这是由于捆绑包paragonie的问题。在这种情况下,请执行以下命令

$ composer require paragonie/random_compat 2.0.17

然后重复捆绑包安装程序

此命令需要您全局安装Composer,如Composer文档的安装章节中所述。

步骤2:启用捆绑包

然后,通过将其添加到项目中app/AppKernel.php文件中注册的捆绑包列表来启用捆绑包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new retItalia\AuthenticationBundle\retItaliaAuthenticationBundle(),
            new KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle(),
        );

        // ...
    }

    // ...
}

在app/config/security.yml中添加

在firewalls:下,与dev相同的级别

	main:
            anonymous: ~
            logout:
              path:   /logout
              target: /
            guard:
              authenticators:
                - authenticator
	providers:
		dbal:
		  entity:
		      class: retItaliaAuthenticationBundle:SaUtente
		      property: idUtente
	providers:
		dbal:
		  entity:
		      class: retItaliaAuthenticationBundle:SaUtente
		      property: idUtente
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/authentication, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/non-autorizzato, roles: IS_AUTHENTICATED_ANONYMOUSLY }

添加这样的角色是个好主意

        role_hierarchy:
            ROLE_ADMIN:       ROLE_USER
            ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

在app/config/routing.yml中添加

	ret_italia_authentication:
	    resource: "@retItaliaAuthenticationBundle/Controller/"
	    type:     annotation
	    prefix:   /

在app/config/config.yml中添加配置


//..
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    globals:
        # the value is the service's id
//..
        userAuthorizedFunctions: "@userAuthorizedFunctions"
//..
//..
# Doctrine Configuration
doctrine:
    dbal:
        connections:
            bdc:
                driver: '%bdc_driver%'
                host: '%bdc_host%'
                port: '%bdc_port%'
                dbname: '%bdc_name%'
                user: '%bdc_user%'
                password: '%bdc_password%'
                charset: UTF8
//..	
knpu_oauth2_client:
    clients:
      # will create service: "knpu.oauth2.client.google"
      # an instance of: KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient
      # composer require league/oauth2-google
      google_main:
          type: google
          # add and configure client_id and client_secret in parameters.yml
          client_id: '%google_client_id%'
          client_secret: '%google_client_secret%'
          # a route name you'll create
          redirect_route: '%google_redirect_url%'
          redirect_params: {}

ret_italia_authentication:
    parameters:
        google_client_id: '%google_client_id%'
        google_client_secret: '%google_client_secret%'
        google_redirect_url: '%google_redirect_url%'
	application_id: '%application_id%'

在app/config/parameters.yml中添加

parameters:
    bdc_host: <bdc server>
    bdc_port: <bdc port>
    bdc_name: <bdc name>
    bdc_user: <bdc user>
    bdc_password: <bdc password>
    bdc_driver: oci8
//..
    google_client_id: '<google_client_id>'
    google_client_secret: '<google_client_secret>'
    google_redirect_url: '<google_redirect_url>'
    application_id: <application_id>

对于测试和开发环境,google_redirect_url必须与'connect_google_check'相同,因此对于parameters.yml,对于本地生产环境,google_redirect_url='connect_google_check_dev'

在app/config/parameters.yml中,您还必须添加必须授权进行google认证的应用程序

scope_auth: ['<application-1>','<application-2>']

如果您不知道必须使用哪个应用程序,可以留空该参数

scope_auth: []

参数的正确值可以从https://gitlab.com/retitalia/contenitore-bundle-comuni获取

许可证

此捆绑包采用MIT许可证。

用法

认证是自动的,它通过oauth2调用Google SSO并执行过程。

授权基于特定数据库。用户必须在正确的表中启用,并且必须具有指定应用的权限。

它通过application_id参数测试parameters.yml中指定的应用。

但是,还有一些可以手动调用的其他有用的功能。

测试指定功能

要测试登录用户是否具有指定功能的权限,只需以这种方式调用isFunctionAuthorized即可

if ($this->get('userAuthorizedFunctions')->isFunctionAuthorized(<functionId>)
{
}
else
{
}

不需要传递登录用户,功能已经知道。

测试用户是否具有角色

要测试登录用户是否具有指定的角色,可以通过这种方式调用hasRole

if ($this->get('userAuthorizedFunctions')->hasRole(<roleId>)
{
}
else
{
}

不需要传递登录用户,功能已经知道。