ciricihq / api-bundle
Symfony API 管理推送通知
Requires
- php: >=5.3.3
- friendsofsymfony/oauth-server-bundle: ~1.4
- friendsofsymfony/rest-bundle: ~1.5
- friendsofsymfony/user-bundle: *
- jms/serializer-bundle: *
- nelmio/api-doc-bundle: ^2.9
- nelmio/cors-bundle: ^1.4
Requires (Dev)
- doctrine/doctrine-bundle: ~1.2
- doctrine/doctrine-fixtures-bundle: 2.2.*
- doctrine/orm: ~2.2,>=2.2.3,<2.5
- liip/functional-test-bundle: ~1.2
- phpunit/phpunit: ~4.3
- raulfraile/ladybug: ~1.0
- raulfraile/ladybug-bundle: ~1.0.0
- symfony/framework-bundle: ~2.3
This package is auto-updated.
Last update: 2024-09-08 06:39:10 UTC
README
用户访问API以允许用户登录。
认证是通过oauth2规范完成的。用于处理oauth2认证的包是FOSOAuthServerBundle
要了解认证过程,可以从这篇教程开始: OAuth2解释
安装
为了使用此包,您必须将以下行添加到config.yml中
# FOSUser configs fos_user: db_driver: orm firewall_name: api user_class: Cirici\ApiBundle\Entity\User from_email: address: webmaster@api.cirici.com sender_name: webmaster resetting: token_ttl: 21600 # 6 hours i guess email: template: 'FOSUserBundle:Resetting:email.txt.twig' from_email: address: webmaster@api.cirici.com sender_name: webmaster # FOSRest configs fos_rest: param_fetcher_listener: true view: view_response_listener: force routing_loader: default_format: json serializer: serialize_null: true # FOSOAuth configs fos_oauth_server: db_driver: orm client_class: Cirici\ApiBundle\Entity\Client access_token_class: Cirici\ApiBundle\Entity\AccessToken refresh_token_class: Cirici\ApiBundle\Entity\RefreshToken auth_code_class: Cirici\ApiBundle\Entity\AuthCode service: user_provider: fos_user.user_manager options: supported_scopes: user # In order to override the User entity you have to add the next lines # this changes the pointer of all the relationships to user bundle doctrine: orm: resolve_target_entities: Cirici\ApiBundle\Model\UserInterface: Cirici\YourBrandNewBundle\Entity\SomeUserEntity
还应将以下行添加到routing.yml
中
cirici_api: resource: "@CiriciApiBundle/Resources/config/routing.yml" prefix: / fos_oauth_server_token: resource: "@FOSOAuthServerBundle/Resources/config/routing/token.xml" fos_oauth_server_authorize: resource: "@FOSOAuthServerBundle/Resources/config/routing/authorize.xml" cirici_oauth_server_auth_login: pattern: /oauth/v2/auth_login defaults: { _controller: CiriciApiBundle:Security:login } cirici_oauth_server_auth_login_check: pattern: /oauth/v2/auth_login_check defaults: { _controller: FOSUserBundle:Security:check }
在routing.yml中启用用户调用
cirici_oauth_server_user: pattern: /api/user defaults: { _controller: CiriciApiBundle:ApiUser:user }
创建oauth2客户端
要允许用户登录API,首先应创建客户端,客户端是能够通过我们的oauth服务器进行认证的实体类型。客户端可以是使用API的移动应用、允许其他API访问我们的API、为Web用户等。
php app/console cirici:oauth-server:client:create --redirect-uri="CLIENT_HOST" --grant-type="authorization_code" --grant-type="password" --grant-type="refresh_token" --grant-type="token" --grant-type="client_credentials"
您可以定义此客户端将接受哪些授权类型,在示例中我们启用了所有类型。但是,密码授权类型是与此项目一起使用计划中的类型。
创建客户端时,您将获得以下重要信息(您可以在其数据库表中查看)
- public_id: 客户端ID
- secret: 使用此客户端所需的密钥
获取访问令牌(登录)
要使用Web表单进行登录,您可以查看以下URL
/oauth/v2/auth_login
或者您可以发送以下参数
- client_id
- client_secret
- grant_type = 'password'
- username
- password
通过POST(或可能是header)发送到以下URL
/oauth/v2/token
结果您将获得以下信息
- access_token: 请求的信息
- expires_in: 令牌的寿命
- token_type: 令牌类型
- scope: 应用到令牌的范围
- refresh_token: 用于更新访问令牌的RefreshToken值
使用RefreshToken
访问令牌的有效期为1小时(3600秒),因此每次过时,我们都应使用RefreshToken请求新的访问令牌。
当访问令牌过期时,应执行以下操作以刷新访问令牌
发送以下参数
- client_id
- client_secret
- grant_type = 'refresh_token'
- refresh_token
到
/oauth/v2/token
结果您将得到与访问令牌调用相同的响应,但带有新的访问令牌
您可以在项目的测试中查看流程。
用户实体
重置密码
要请求重置密码,您应该通过POST发送以下参数
- username(应该是用户电子邮件或用户名,在我们的情况下我们将使用电子邮件地址)
到
/api/resetting/send-email
此调用将向指定的用户发送电子邮件,其中包含重置其密码的URL。此调用将适合重置密码表单的Web表单。
问题
如果您在Doctrine代理类生成方面遇到问题,应运行
app/console cache:warmup --env=prod --no-debug