/README.md

https://github.com/trikoder/oauth2-bundle · Markdown · 230 lines · 156 code · 74 blank · 0 comment · 0 complexity · 3039a60d448ee3a03f7a7ddddc022978 MD5 · raw file

  1. # Trikoder OAuth 2 Bundle
  2. [![Build Status](https://travis-ci.org/trikoder/oauth2-bundle.svg?branch=master)](https://travis-ci.org/trikoder/oauth2-bundle)
  3. [![Latest Stable Version](https://poser.pugx.org/trikoder/oauth2-bundle/v/stable)](https://packagist.org/packages/trikoder/oauth2-bundle)
  4. [![License](https://poser.pugx.org/trikoder/oauth2-bundle/license)](https://packagist.org/packages/trikoder/oauth2-bundle)
  5. [![Code coverage](https://codecov.io/gh/trikoder/oauth2-bundle/branch/master/graph/badge.svg)](https://codecov.io/gh/trikoder/oauth2-bundle)
  6. Symfony bundle which provides OAuth 2.0 authorization/resource server capabilities. The authorization and resource server actors are implemented using the [thephpleague/oauth2-server](https://github.com/thephpleague/oauth2-server) library.
  7. ## Important notes
  8. This bundle provides the "glue" between [thephpleague/oauth2-server](https://github.com/thephpleague/oauth2-server) library and Symfony.
  9. It implements [thephpleague/oauth2-server](https://github.com/thephpleague/oauth2-server) library in a way specified by its official documentation.
  10. For implementation into Symfony project, please see [bundle documentation](docs/basic-setup.md) and official [Symfony security documentation](https://symfony.com/doc/current/security.html).
  11. ## Status
  12. This package is currently in the active development.
  13. ## Features
  14. * API endpoint for client authorization and token issuing
  15. * Configurable client and token persistance (includes [Doctrine](https://www.doctrine-project.org/) support)
  16. * Integration with Symfony's [Security](https://symfony.com/doc/current/security.html) layer
  17. ## Requirements
  18. * [PHP 7.2](http://php.net/releases/7_2_0.php) or greater
  19. * [Symfony 4.4](https://symfony.com/roadmap/4.4) or [Symfony 5.x](https://symfony.com/roadmap/5.0)
  20. ## Installation
  21. 1. Require the bundle and a PSR 7/17 implementation with Composer:
  22. ```sh
  23. composer require trikoder/oauth2-bundle nyholm/psr7
  24. ```
  25. If your project is managed using [Symfony Flex](https://github.com/symfony/flex), the rest of the steps are not required. Just follow the post-installation instructions instead! :tada:
  26. > **NOTE:** This bundle requires a PSR 7/17 implementation to operate. We recommend that you use [nyholm/psr7](https://github.com/Nyholm/psr7). Check out this [document](docs/psr-implementation-switching.md) if you wish to use a different implementation.
  27. 1. Create the bundle configuration file under `config/packages/trikoder_oauth2.yaml`. Here is a reference configuration file:
  28. ```yaml
  29. trikoder_oauth2:
  30. authorization_server: # Required
  31. # Full path to the private key file.
  32. # How to generate a private key: https://oauth2.thephpleague.com/installation/#generating-public-and-private-keys
  33. private_key: ~ # Required, Example: /var/oauth/private.key
  34. # Passphrase of the private key, if any.
  35. private_key_passphrase: null
  36. # The plain string or the ascii safe string used to create a Defuse\Crypto\Key to be used as an encryption key.
  37. # How to generate an encryption key: https://oauth2.thephpleague.com/installation/#string-password
  38. encryption_key: ~ # Required
  39. # The type of value of "encryption_key".
  40. encryption_key_type: plain # One of "plain"; "defuse"
  41. # How long the issued access token should be valid for, used as a default if there is no grant type specific value set.
  42. # The value should be a valid interval: http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
  43. access_token_ttl: PT1H
  44. # How long the issued refresh token should be valid for, used as a default if there is no grant type specific value set.
  45. # The value should be a valid interval: http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
  46. refresh_token_ttl: P1M
  47. # How long the issued authorization code should be valid for.
  48. # The value should be a valid interval: http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
  49. auth_code_ttl: ~ # Deprecated ("trikoder_oauth2.authorization_server.auth_code_ttl" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.authorization_code.auth_code_ttl" instead.)
  50. # Whether to require code challenge for public clients for the authorization code grant.
  51. require_code_challenge_for_public_clients: ~ # Deprecated ("trikoder_oauth2.authorization_server.require_code_challenge_for_public_clients" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.authorization_code.require_code_challenge_for_public_clients" instead.)
  52. # Whether to enable the authorization code grant.
  53. enable_auth_code_grant: ~ # Deprecated ("trikoder_oauth2.authorization_server.enable_auth_code_grant" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.authorization_code.enable" instead.)
  54. # Whether to enable the client credentials grant.
  55. enable_client_credentials_grant: ~ # Deprecated ("trikoder_oauth2.authorization_server.enable_client_credentials_grant" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.client_credentials.enable" instead.)
  56. # Whether to enable the implicit grant.
  57. enable_implicit_grant: ~ # Deprecated ("trikoder_oauth2.authorization_server.enable_implicit_grant" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.implicit.enable" instead.)
  58. # Whether to enable the password grant.
  59. enable_password_grant: ~ # Deprecated ("trikoder_oauth2.authorization_server.enable_password_grant" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.password.enable" instead.)
  60. # Whether to enable the refresh token grant.
  61. enable_refresh_token_grant: ~ # Deprecated ("trikoder_oauth2.authorization_server.enable_refresh_token_grant" is deprecated, use "trikoder_oauth2.authorization_server.grant_types.refresh_token.enable" instead.)
  62. # Enable and configure grant types.
  63. grant_types:
  64. authorization_code:
  65. # Whether to enable the authorization code grant.
  66. enable: true
  67. # How long the issued access token should be valid for the authorization code grant.
  68. access_token_ttl: ~
  69. # How long the issued refresh token should be valid for the authorization code grant.
  70. refresh_token_ttl: ~
  71. # How long the issued authorization code should be valid for.
  72. # The value should be a valid interval: http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
  73. auth_code_ttl: PT10M
  74. # Whether to require code challenge for public clients for the authorization code grant.
  75. require_code_challenge_for_public_clients: true
  76. client_credentials:
  77. # Whether to enable the client credentials grant.
  78. enable: true
  79. # How long the issued access token should be valid for the client credentials grant.
  80. access_token_ttl: ~
  81. implicit:
  82. # Whether to enable the implicit grant.
  83. enable: true
  84. # How long the issued access token should be valid for the implicit grant.
  85. access_token_ttl: ~
  86. password:
  87. # Whether to enable the password grant.
  88. enable: true
  89. # How long the issued access token should be valid for the password grant.
  90. access_token_ttl: ~
  91. # How long the issued refresh token should be valid for the password grant.
  92. refresh_token_ttl: ~
  93. refresh_token:
  94. # Whether to enable the refresh token grant.
  95. enable: true
  96. # How long the issued access token should be valid for the refresh token grant.
  97. access_token_ttl: ~
  98. # How long the issued refresh token should be valid for the refresh token grant.
  99. refresh_token_ttl: ~
  100. resource_server: # Required
  101. # Full path to the public key file.
  102. # How to generate a public key: https://oauth2.thephpleague.com/installation/#generating-public-and-private-keys
  103. public_key: ~ # Required, Example: /var/oauth/public.key
  104. # Scopes that you wish to utilize in your application.
  105. # This should be a simple array of strings.
  106. scopes: []
  107. # Configures different persistence methods that can be used by the bundle for saving client and token data.
  108. # Only one persistence method can be configured at a time.
  109. persistence: # Required
  110. doctrine:
  111. # Name of the entity manager that you wish to use for managing clients and tokens.
  112. entity_manager: default
  113. in_memory: ~
  114. # The priority of the event listener that converts an Exception to a Response.
  115. exception_event_listener_priority: 10
  116. # Set a custom prefix that replaces the default "ROLE_OAUTH2_" role prefix.
  117. role_prefix: ROLE_OAUTH2_
  118. ```
  119. 1. Enable the bundle in `config/bundles.php` by adding it to the array:
  120. ```php
  121. Trikoder\Bundle\OAuth2Bundle\TrikoderOAuth2Bundle::class => ['all' => true]
  122. ```
  123. 1. Update the database so bundle entities can be persisted using Doctrine:
  124. ```sh
  125. bin/console doctrine:schema:update --force
  126. ```
  127. 1. Import the routes inside your `config/routes.yaml` file:
  128. ```yaml
  129. oauth2:
  130. resource: '@TrikoderOAuth2Bundle/Resources/config/routes.xml'
  131. ```
  132. You can verify that everything is working by issuing a `POST` request to the `/token` endpoint.
  133. ** NOTE ** It is recommended to control the access to the authorization endpoint
  134. so that only logged in users can approve authorization requests.
  135. You should review your `security.yml` file. Here is a sample configuration:
  136. ```yaml
  137. security:
  138. access_control:
  139. - { path: ^/authorize, roles: IS_AUTHENTICATED_REMEMBERED }
  140. ```
  141. ## Configuration
  142. * [Basic setup](docs/basic-setup.md)
  143. * [Controlling token scopes](docs/controlling-token-scopes.md)
  144. * [Password grant handling](docs/password-grant-handling.md)
  145. * [Implementing custom grant type](docs/implementing-custom-grant-type.md)
  146. ## Contributing
  147. Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
  148. ## Versioning
  149. This project adheres to [Semantic Versioning 2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
  150. However, starting with version 4, we only promise to follow SemVer on structural elements marked with the [@api tag](https://github.com/php-fig/fig-standards/blob/2668020622d9d9eaf11d403bc1d26664dfc3ef8e/proposed/phpdoc-tags.md#51-api).
  151. ## Changes
  152. All the package releases are recorded in the [CHANGELOG](CHANGELOG.md) file.
  153. ## Reporting issues
  154. Use the [issue tracker](https://github.com/trikoder/oauth2-bundle/issues) to report any issues you might have.
  155. ## License
  156. See the [LICENSE](LICENSE.md) file for license rights and limitations (MIT).