|
| 1 | +# Authorization code grant |
| 2 | + |
| 3 | +Authorization code grant has two steps |
| 4 | + |
| 5 | +1. Acquiring authorization code |
| 6 | +2. Getting token from authorization code |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +To use authorization code grant `enable_auth_code_grant` parameter inside `authorization_server` must be set to `true` (it is set to `true` by default). |
| 11 | + |
| 12 | +### Example: config.yml |
| 13 | + |
| 14 | +```yaml |
| 15 | +trikoder_oauth2: |
| 16 | + authorization_server: |
| 17 | + enable_auth_code_grant: true |
| 18 | +``` |
| 19 | +
|
| 20 | +After authorization code grant is enabled, token and authorization endpoints must be set. |
| 21 | +It can be done by including `Resources/config/routes.xml` which will provide `/authorize` or `/token` endpoints or manually by setting |
| 22 | + |
| 23 | +1. Controller `Trikoder\Bundle\OAuth2Bundle\Controller\AuthorizationController::indexAction` with `GET` method for authorization endpoint |
| 24 | +2. Controller `Trikoder\Bundle\OAuth2Bundle\Controller\TokenController::indexAction` with `POST` method for token endpoint |
| 25 | + |
| 26 | +### Example: custom setup |
| 27 | + |
| 28 | +```yaml |
| 29 | +oauth2_authorization_code: |
| 30 | + controller: Trikoder\Bundle\OAuth2Bundle\Controller\AuthorizationController::indexAction |
| 31 | + path: /oauth2-authorization-code |
| 32 | +
|
| 33 | +oauth2_token: |
| 34 | + controller: Trikoder\Bundle\OAuth2Bundle\Controller\TokenController::indexAction |
| 35 | + path: /api/token |
| 36 | +``` |
| 37 | + |
| 38 | +After assigning routes, listener for `trikoder.oauth2.authorization_request_resolve` must be configured. |
| 39 | + |
| 40 | +`\Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent` (whose name is `trikoder.oauth2.authorization_request_resolve`) consist of three important methods which have to be used |
| 41 | + |
| 42 | +1. `setUser(?UserInterface $user)` and `resolveAuthorization(bool $authorizationResolution)` when user is already logged in when accessing authorization endpoint |
| 43 | +2. `setResponse(ResponseInterface $response)` when user needs to log in before authorization server can issue authorization code |
| 44 | + |
| 45 | +### Example: (services.yml and php class) |
| 46 | + |
| 47 | +```yaml |
| 48 | + BestNamespace\OAuthLogin\Listener\AuthorizationCodeListener: |
| 49 | + tags: |
| 50 | + - { name: kernel.event_listener, event: 'trikoder.oauth2.authorization_request_resolve', method: onAuthorizationRequestResolve } |
| 51 | +``` |
| 52 | + |
| 53 | +```php |
| 54 | +<?php |
| 55 | +
|
| 56 | +declare(strict_types=1); |
| 57 | +
|
| 58 | +namespace BestNamespace\OAuthLogin\Listener; |
| 59 | +
|
| 60 | +use Nyholm\Psr7\Response; |
| 61 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 62 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 63 | +use Symfony\Component\Security\Core\Security; |
| 64 | +use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent; |
| 65 | +
|
| 66 | +final class AuthorizationCodeListener |
| 67 | +{ |
| 68 | + private $security; |
| 69 | + private $urlGenerator; |
| 70 | + private $requestStack; |
| 71 | +
|
| 72 | + public function __construct( |
| 73 | + Security $security, |
| 74 | + UrlGeneratorInterface $urlGenerator, |
| 75 | + RequestStack $requestStack |
| 76 | + ) { |
| 77 | + $this->security = $security; |
| 78 | + $this->urlGenerator = $urlGenerator; |
| 79 | + $this->requestStack = $requestStack; |
| 80 | + } |
| 81 | +
|
| 82 | + public function onAuthorizationRequestResolve(AuthorizationRequestResolveEvent $event) |
| 83 | + { |
| 84 | + if (null !== ($user = $this->security->getUser())) { |
| 85 | + $event->setUser($user); |
| 86 | + $event->resolveAuthorization(true); |
| 87 | + } else { |
| 88 | + $event->setResponse( |
| 89 | + new Response( |
| 90 | + 302, |
| 91 | + [ |
| 92 | + 'Location' => $this->urlGenerator->generate( |
| 93 | + 'login', |
| 94 | + [ |
| 95 | + 'returnUrl' => $this->requestStack->getMasterRequest()->getUri(), |
| 96 | + ] |
| 97 | + ), |
| 98 | + ] |
| 99 | + ) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +After listener is configured new client can be registered. |
| 107 | + |
| 108 | +### Example: cli |
| 109 | + |
| 110 | +``` |
| 111 | +bin/console trikoder:oauth2:create-client best_client not_so_secret --redirect-uri "https://www.bestclient.com/" --grant-type "authorization_code" --scope "user.view" |
| 112 | +``` |
| 113 | +
|
| 114 | +This example assumes scope `user.view` is already registered scope inside `trikoder_oauth2` configuration |
| 115 | +
|
| 116 | +### Example: config.yml |
| 117 | +
|
| 118 | +```yaml |
| 119 | +trikoder_oauth2: |
| 120 | + scopes: |
| 121 | + - 'user.view' |
| 122 | +``` |
| 123 | + |
| 124 | +After client is registered he can communicate with your server using authorization code grant. |
0 commit comments