etcpasswd/oauth-bundle

Symfony2的OAuth防火墙

安装数: 18,932

依赖关系: 0

建议者: 0

安全性: 0

星标: 39

关注者: 3

分支: 10

开放性问题: 8

类型:symfony-bundle

dev-master 2012-01-17 12:41 UTC

This package is not auto-updated.

Last update: 2024-09-22 03:31:43 UTC


README

此捆绑包仍在开发中,事物可能会发生变化!

安装

使用svn

$ git clone https://github.com/mazen/EtcpasswdOAuthBundle.git vendor/bundles/Etcpasswd/OAuthBundle
$ git clone https://github.com/kriswallsmith/Buzz.git vendor/buzz

在您的自动加载器中注册命名空间

# app/autoload.php
$loader->registerNamespaces(array(
    'Etcpasswd'        => __DIR__.'/../vendor/bundles',
    'Buzz'             => __DIR__.'/../vendor/buzz/lib',
    // .. your other namespaces
));

在您的应用程序内核中注册捆绑包

# app/AppKernel.php
$bundles = array(
    new Etcpasswd\OAuthBundle\EtcpasswdOAuthBundle(),
    // .. other bundles
);

配置您的安全防火墙

# app/config/security.yml

firewalls:
    oauth:
      anonymous: true
      logout: true
      pattern: ^/
      oauth:
        auth_provider: api provider
        client_id:     client id
        client_secret: secret
        uid:           email
        scope:         requested scope
        login_path:    /login
        check_path:    /auth
        failure_path:  /

factories:
  - "%kernel.root_dir%/../vendor/bundles/Etcpasswd/OAuthBundle/Resources/config/security_factories.xml"

请注意,您不需要为登录路径或检查路径构建任何控制器。它们仅用于内部标识何时需要发生登录。

此外,请注意,您仍然需要提供一个用户提供者。此捆绑包仅根据OAuth服务进行用户验证,但不会创建任何User对象。

指定多个OAuth2提供者

此包还允许同时使用多个提供者进行登录。您需要做的就是将这些提供者添加到security.yml中

示例

firewalls:
    main:
      anonymous: true
      logout: true
      pattern: ^/

      oauth_github:
        auth_provider: "github"
        client_id: xxx
        client_secret: xxx
        scope: repo,user
        login_path: /login/github
        check_path: /auth/github
        failure_path:  /

      oauth_facebook:
        auth_provider: "facebook"
        client_id:     xxx
        client_secret: xxx
        scope:         ""
        login_path:    /login/facebook
        check_path:    /auth/facebook
        failure_path:  /

      oauth_google:
        auth_provider: "google"
        client_id:     xxx
        client_secret: xxx
        scope:         "https://www.googleapis.com/auth/plus.me"
        login_path:    /login/google
        check_path:    /auth/google
        failure_path:  /

调用/login/github、/login/facebook或/login/google将使用正确的oauth提供者。

内置OAuth提供者

此捆绑包包含以下内置提供者

  • GitHub
  • Facebook
  • Google

关于Google的说明:您至少需要提供https://www.googleapis.com/auth/plus.me作用域才能获取用户名

授权用户

此捆绑包不提供用户授权和/或持久状态的方法。您应该查看https://github.com/FriendsOfSymfony/FOSUserBundle。如果您无论如何都要使用这些用户,而无需将它们持久化到数据库中,则可以将此捆绑包提供的提供者添加到您的安全配置中。这将允许您通过Security Context服务访问accessToken,以查询给定提供者的其他API服务。

示例 security.yml

security:
  firewalls:
    main:
      anonymous: true
      logout: true
      pattern: ^/

      oauth_github:
        auth_provider: "github"
        client_id:     xxx
        client_secret: xxx
        scope: repo,user
        login_path: /login/github
        check_path: /auth/github
        failure_path:  /

  role_hierarchy:
    ROLE_ADMIN: [ROLE_USER]

  providers:
    main:
      id: etcpasswd_oauth.user.provider
  
  access_control: ~  

  factories:
    - "%kernel.root_dir%/../vendor/bundles/Etcpasswd/OAuthBundle/Resources/config/security_factories.xml"