diff --git a/core/oauth2.md b/core/oauth2.md new file mode 100644 index 00000000000..02929eb1df7 --- /dev/null +++ b/core/oauth2.md @@ -0,0 +1,329 @@ +# Adding a OAuth2 Authentication using `FOSOAuthServerBundle` + +> [OAuth](https://oauth.net/2/) is an open standard for authorization, commonly used as a way for Internet users to authorize websites or applications to access their information on other websites but without giving them the passwords. This mechanism is used by companies such as Google, Facebook, Microsoft and Twitter to permit the users to share information about their accounts with third party applications or websites. + +[Wikipedia](https://en.wikipedia.org/wiki/OAuth) + +API Platform allows to easily add a OAuth2-based authentication to your API using [FOSOAuthServerBundle](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle). + +API Platform is fully working with [FOSOAuthServerBundle](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle). + +This tutorial is based on [Getting Started With FOSOAuthServerBundle](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md) and [Basic RESTful API with Symfony 2 + FOSRestBundle (JSON format only) + FOSUserBundle + FOSOauthServerBundle](https://gist.github.com/tjamps/11d617a4b318d65ca583) + +## Install FOSOauthServerBundle + +Install the bundle with composer: + +```bash +composer require friendsofsymfony/oauth-server-bundle +``` + +Enable the bundle in the kernel: + +```php +setName('oauth:client:create') + ->setDescription('Create OAuth Client') + ->addArgument( + 'grantType', + InputArgument::REQUIRED, + 'Grant Type?' + ) + ->addArgument( + 'redirectUri', + InputArgument::OPTIONAL, + 'Redirect URI?' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $container = $this->getContainer(); + $redirectUri = $input->getArgument('redirectUri'); + $grantType = $input->getArgument('grantType'); + + $clientManager = $container->get('fos_oauth_server.client_manager.default'); + /** @var Client $client */ + $client = $clientManager->createClient(); + $client->setRedirectUris($redirectUri ? [$redirectUri] : []); + $client->setAllowedGrantTypes([$grantType]); + $clientManager->updateClient($client); + + $output->writeln(sprintf('The client %s was created with %s as public id and %s as secret', + $client->getId(), + $client->getPublicId(), + $client->getSecret() + )); + } +} +``` + +Now you can generate two clients. One for our swagger api documentation and one for our application that wants to get data from our api. + +```bash +# Application client +php bin/console oauth:client:create password + +# Swagger api documentation client +php bin/console oauth:client:create client_credentials +``` + +## OAuth2 Configuration + +Add the following code to your `app/config/config.yml` and replace the `clientId` and `clientSecret` with the data from the generated application client with the `client_credentials` grant type. + +```yaml +# ... +fos_oauth_server: + db_driver: orm # Drivers available: orm, mongodb, or propel + client_class: 'AppBundle\Entity\Client' + access_token_class: 'AppBundle\Entity\AccessToken' + refresh_token_class: 'AppBundle\Entity\RefreshToken' + auth_code_class: 'AppBundle\Entity\AuthCode' + service: + user_provider: fos_user.user_provider.username + options: + access_token_lifetime: 10800 + supported_scopes: user + +api_platform: + # ... + oauth2: + enabled: true + clientId: "enter-swagger-api-documentation-client-id" + clientSecret: "enter-swagger-api-documentation-client-secret" +``` + +That's all, now your OAuth2 authentication should work. diff --git a/outline.yaml b/outline.yaml index 0f0a5aab0bb..8ccbbd604a6 100644 --- a/outline.yaml +++ b/outline.yaml @@ -45,6 +45,7 @@ chapters: - events - file-upload - jwt + - oAuth2 - form-data - angularjs-integration - fosuser-bundle