ats / user-bundle
Requires
- php: >=7.0
- alcaeus/mongo-php-adapter: ^1.1
- ats/core-bundle: ^1.0
- ats/email-bundle: ^1.0
- doctrine/doctrine-bundle: ^1.9
- doctrine/mongodb-odm: ^1.2
- doctrine/mongodb-odm-bundle: ^3.4
- friendsofsymfony/oauth-server-bundle: 1.6.*
- jms/serializer-bundle: ~2.3
- symfony/cache: 3.4.*
- symfony/config: 3.4.*
- symfony/console: 3.4.*
- symfony/dependency-injection: 3.4.*
- symfony/event-dispatcher: 3.4.*
- symfony/filesystem: 3.4.*
- symfony/finder: 3.4.*
- symfony/form: 3.4.*
- symfony/http-foundation: 3.4.*
- symfony/http-kernel: 3.4.*
- symfony/inflector: 3.4.*
- symfony/intl: 3.4.*
- symfony/monolog-bundle: ^3.1.0
- symfony/routing: 3.4.*
- symfony/security: 3.4.*
- symfony/security-bundle: 3.4.*
- symfony/templating: 3.4.*
- symfony/translation: 3.4.*
- symfony/twig-bridge: 3.4.*
- symfony/twig-bundle: 3.4.*
- symfony/validator: 3.4.*
- symfony/yaml: 3.4.*
Requires (Dev)
- doctrine/doctrine-fixtures-bundle: 3.0.2
- phpstan/phpstan: ^0.10.3
- phpstan/phpstan-symfony: ^0.10.1
- squizlabs/php_codesniffer: ^3.3
- symfony/browser-kit: 3.4.*
- symfony/debug: 3.4.*
- symfony/dom-crawler: 3.4.*
- symfony/framework-bundle: 3.4.*
- symfony/phpunit-bridge: 3.4.*
- symfony/process: 3.4.*
- symfony/var-dumper: 3.4.*
- symfony/web-server-bundle: 3.4.*
Provides
- ext-mongo: *
README
通用
基于 OAuth2 的功能齐全且 RESTful 的用户包
安装
更新你的
composer.json
文件以添加 ATS 私有 Packagist{ "repositories": [ // ... { "type": "composer", "url": "https://packagist.ats-digital.com" } // ... ], // ... }
使用 composer 安装
$ php composer require ats/user-bundle dev-master
配置
- 包注册
要开始使用此包,你需要更新你的 AppKernel.php
文件并注册以下包
<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new FOS\OAuthServerBundle\FOSOAuthServerBundle(),
new ATS\CoreBundle\ATSCoreBundle(),
new ATS\UserBundle\ATSUserBundle(),
// ...
];
}
// ...
}
- 包配置
默认配置足够,除非你可能需要覆盖它以自定义包的使用,为此,更新你的 config.yml
文件并添加以下配置部分,参见[配置参考][config-references]
配置 SwiftMailer
swiftmailer:
transport: "<transport>" # exp. gmail
username: "<username>"
password: "<password>"
spool:
type: file
path: '%kernel.project_dir%/var/spool/app/%kernel.environment%'
antiflood:
threshold: 99
sleep: 5
- 安全配置
更新你的 security.yml
文件并在其中添加以下配置,不要犹豫去查看 Symfony [文档][symfony-security] 以获取更多信息
# app/config/security.yml
security:
encoders:
ATS\UserBundle\Document\User: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
ats_user_provider:
id: ATS\UserBundle\Provider\UserProvider
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
main:
pattern: ^/ # might be something else
fos_oauth: true
stateless: true
anonymous: false
# put your own access control rules in here
# access_control:
# - { path: ^/, role: ROLE_USER }
用法
- 创建客户端
$ php bin/console ats:user:client:create <name>
前面的命令将输出类似以下内容
Client Credentials
==================
+----------+-------------+-----------------+
| name | client_id | client_secret |
+----------+-------------+-----------------+
| <name> | <client_id> | <client_secret> |
+----------+-------------+-----------------+
创建用户
$ php bin/console ats:user:create <usenrame> <email> <password>
请求 OAuth2 Bearer 访问令牌
使用你在步骤 1 中生成的 client_id
和 client_secret
以及你在步骤 2 中创建的用户名和密码(通过将 grant_type
设置为 password),发送 GET 请求,你的请求应该看起来像这样
$ curl -X GET 'http://127.0.0.1:8000/oauth/v2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&redirect_uri=<redirect_uri>&username=<username>&password=<password>'
此请求将返回一个包含 access_token
的 json 格式响应
{
"access_token": <access_token>,
"expires_in": 3600,
"token_type": "bearer",
"scope": <scopes>,
"refresh_token": <refresh_token>
}
- 使用
access_token
登录
要使用之前生成的 access_token
登录到你的应用程序,你只需将其添加到请求的 header
中的 Authorization
头键,并在其前面添加关键字 Bearer
$ curl -H 'Authorization: Bearer <access_token>' http://127.0.0.1:8000
- 刷新 OAuth2 Bearer 访问令牌
当你的访问令牌过期时,你可以通过发送使用你在步骤 1 中生成的 client_id
和 client_secret
以及你在步骤 2 中创建的用户名和密码(通过在步骤 3 中提供的 refresh_token
设置 grant_type
为 refresh_token)的 GET 请求来刷新它,你的请求应该看起来像这样
$ curl -X GET 'http://127.0.0.1:8000/oauth/v2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&redirect_uri=<redirect_uri>&username=<username>&password=<password>&refresh_token=<refresh_token>'
此请求将返回一个包含新的 access_token
的 json 格式响应
{
"access_token": <access_token>,
"expires_in": 3600,
"token_type": "bearer",
"scope": <scopes>,
"refresh_token": <refresh_token>
}
就是这样。
示例
- 创建客户端
$ php bin/console ats:user:client:create webapp
输出
Created client +--------+-----------------------------------------------------------------------------+----------------------------------------------------+ | name | client_id | client_secret | +--------+-----------------------------------------------------------------------------+----------------------------------------------------+ | webapp | 5c0bad1027ff86203709f9a1_3gngcksw79escc0k0c0g4gc00k8kscwo4wks08kc8sk8w4gco4 | 1n8ahgynngxwcs8g8gs8cgg08o8gogk0k8sgogco0cocc8ck4w | +--------+-----------------------------------------------------------------------------+----------------------------------------------------+
2. Create a user:
$ php bin/console ats:user:create myusername [email protected] mypassword
3. Request a OAuth2 Bearer access token:
response:
{
"access_token": "N2Y5NTc1ZThiNjgyYWU3NTE1OGZjNTZlYWVhODJkYmQ5NmEzM2I4NzA1YTRmYzU4MGU2MWI3ZGZkNzUwMmI3Yg",
"expires_in": 3600,
"token_type": "bearer",
"scope": "user admin super_admin",
"refresh_token": "MWRkNjdkNDYwNjBlNjVkMjVmNTMzNGI1Mjc4YWUzMzg3YTY4MTQ5MDFlN2EwMGZmZThjYmI3YzFmMzkzYzQ5ZA"
}
4. Login using the `access_token`
$ curl -H 'Authorization: Bearer N2Y5NTc1ZThiNjgyYWU3NTE1OGZjNTZlYWVhODJkYmQ5NmEzM2I4NzA1YTRmYzU4MGU2MWI3ZGZkNzUwMmI3Yg' http://127.0.0.1:8000
3. Refresh a OAuth2 Bearer access token:
response:
{
"access_token": "YTFmNDBlYmZkNDdhNGM1NTNkODY0ODNkNDQ4MmM4YWRmMGQ0ZDM3MDViNjUzMTNlNmYyYzc0MWI1NGQ4NTMyOQ",
"expires_in": 3600,
"token_type": "bearer",
"scope": "user admin super_admin",
"refresh_token": "ZThlNDI3NjViNzY0NmQyNDM5MzYzYzNiOGMzYmRjYjcxNGU4MDQzZjUwZGE1YTUzZWRmOTFhMTI1YWE5Yzg3OQ"
}
### Tests
---
To run unit test:
$ php ./vendor/bin/simple-phpunit --coverage-text --colors=never --strict-coverage --disallow-test-output -c phpunit.xml.dist
### Table of contents
---
[Configuration references][config-references]
---
Enjoy!
[symfony-security]: <https://symfony.com/doc/3.4/security.html>
[config-references]: <https://gitlab.ats-digital.com/ats/user-bundle/blob/master/doc/ConfigurationReferences.md>