|
| 1 | +# Configuration |
| 2 | + |
| 3 | +In order to use this authentication adapter, you will need to provide |
| 4 | +configuration for the OAuth2 providers you plan to use. |
| 5 | + |
| 6 | +Examples are provided in the `config/` directory of this component, and repeated |
| 7 | +here for purposes of documentation: |
| 8 | + |
| 9 | +## Global configuration |
| 10 | + |
| 11 | +This is configuration that should be present no matter what environment you are |
| 12 | +in, and it covers the base path for the OAuth2 client callbacks and debug |
| 13 | +provider URIs. |
| 14 | + |
| 15 | +```php |
| 16 | +// e.g. config/autoload/oauth2-client.global.php: |
| 17 | + |
| 18 | +return [ |
| 19 | + 'oauth2clientauthentication' => [ |
| 20 | + // Configure the base path for all OAuth2 client callbacks. By default, |
| 21 | + // this is "/auth". |
| 22 | + // 'auth_path' => '/auth', |
| 23 | + |
| 24 | + // Configure the production and debug routes for OAuth2 client callbacks |
| 25 | + // if desired. These strings will be relative to the 'auth_path' config |
| 26 | + // as specified above. |
| 27 | + 'routes' => [ |
| 28 | + // Production path. |
| 29 | + // 'production' => '/{provider:facebook|github|google|instagram}|linkedin[/oauth2callback]', |
| 30 | + |
| 31 | + // Debug path. |
| 32 | + // 'debug' => '/{provider:debug|facebook|github|google|instagram|linkedin}[/oauth2callback]', |
| 33 | + ], |
| 34 | + ], |
| 35 | +]; |
| 36 | +``` |
| 37 | + |
| 38 | +## Local/Environment-specific configuration |
| 39 | + |
| 40 | +This is configuration for the providers you wish to enable. You will need to |
| 41 | +review the [league/oauth2-client providers documentation](http://oauth2-client.thephpleague.com/providers/league/ |
| 42 | +for links to both full configuration documentation, as well as resources on how |
| 43 | +to obtain the various client identifiers and secrets you will need to use. |
| 44 | + |
| 45 | +This information should _not_ be shipped directly in your repository, but rather |
| 46 | +included as part of your application environment. |
| 47 | + |
| 48 | +```php |
| 49 | +// e.g. config/autoload/oauth2-client.local.php: |
| 50 | + |
| 51 | +return [ |
| 52 | + 'oauth2clientauthentication' => [ |
| 53 | + // Debug |
| 54 | + // This is the debug provider shipped within this component for purposes |
| 55 | + // of testing the OAuth2 client workflow within your applications. |
| 56 | + 'debug' => [ |
| 57 | + // Provide this if you have provided an alternate route path via |
| 58 | + // the oauth2clientauthentication.routes.debug key: |
| 59 | + // 'callback_uri_template' => '/alternate/debug/callback?code=%s&state=%s', |
| 60 | + |
| 61 | + // Provide this if you want to use an alternate path for the OAuth2 |
| 62 | + // "server" authorization: |
| 63 | + // 'authorization_url' => '/alternate/debug/authorization', |
| 64 | + ], |
| 65 | + |
| 66 | + // Facebook |
| 67 | + // 'facebook' => [ |
| 68 | + // 'clientId' => '{facebook-app-id}', |
| 69 | + // 'clientSecret' => '{facebook-app-secret}', |
| 70 | + // 'redirectUri' => '', // based on the auth_path + production route; must be fully qualifed |
| 71 | + // 'graphApiVersion' => 'v2.10', |
| 72 | + // ], |
| 73 | + |
| 74 | + // GitHub |
| 75 | + // 'github' => [ |
| 76 | + // 'clientId' => '{github-client-id}', |
| 77 | + // 'clientSecret' => '{github-client-secret}', |
| 78 | + // 'redirectUri' => '', // based on the auth_path + production route; must be fully qualifed |
| 79 | + // ], |
| 80 | + |
| 81 | + // Google |
| 82 | + // 'google' => [ |
| 83 | + // 'clientId' => '{google-client-id}', |
| 84 | + // 'clientSecret' => '{google-client-secret}', |
| 85 | + // 'redirectUri' => '', // based on the auth_path + production route; must be fully qualifed |
| 86 | + // 'hostedDomain' => '', // scheme + domain of your app |
| 87 | + // ], |
| 88 | + |
| 89 | + // Instagram |
| 90 | + // 'instagram' => [ |
| 91 | + // 'clientId' => '{instagram-client-id}', |
| 92 | + // 'clientSecret' => '{instagram-client-secret}', |
| 93 | + // 'redirectUri' => '', // based on the auth_path + production route; must be fully qualifed |
| 94 | + // 'host' => 'https://api.instagram.com', // Optional; this is the default |
| 95 | + // ], |
| 96 | + |
| 97 | + // LinkedIn |
| 98 | + // 'linkedin' => [ |
| 99 | + // 'clientId' => '{linkedin-client-id}', |
| 100 | + // 'clientSecret' => '{linkedin-client-secret}', |
| 101 | + // 'redirectUri' => '', // based on the auth_path + production route; must be fully qualifed |
| 102 | + // ], |
| 103 | + ], |
| 104 | + 'dependencies' => [ |
| 105 | + 'factories' => [ |
| 106 | + // Enable this when in debug mode: |
| 107 | + // Debug\DebugProviderMiddleware::class => Debug\DebugProviderMiddlewareFactory::class, |
| 108 | + ], |
| 109 | + ], |
| 110 | +]; |
| 111 | +``` |
| 112 | + |
| 113 | +## Pipeline configuration |
| 114 | + |
| 115 | +The various callbacks operate under a base path as specified by the |
| 116 | +`oauth2clientauthentication.auth_path` configuration, which defaults to `/auth`. |
| 117 | +You will need to pipe the `Phly\Expressive\OAuth2ClientAuthentication\OAuth2CallbackMiddleware` |
| 118 | +service to that path: |
| 119 | + |
| 120 | +```php |
| 121 | +// In config/pipeline.php: |
| 122 | + |
| 123 | +use Phly\Expressive\OAuth2ClientAuthentication\OAuth2CallbackMiddleware; |
| 124 | + |
| 125 | +$app->pipe('/auth', OAuth2CallbackMiddleware::class); |
| 126 | +``` |
| 127 | + |
| 128 | +@todo Detail how to pipe the callback middleware when using other middleware frameworks. |
| 129 | + |
| 130 | +## Route configuration |
| 131 | + |
| 132 | +Once the above is complete, you can add |
| 133 | +`Zend\Expressive\Authentication\AuthenticationMiddleware` to your route-specific |
| 134 | +pipelines. You will also need to pipe |
| 135 | +`Zend\Expressive\Session\SessionMiddleware` in these pipelines as this adapter |
| 136 | +persists user information within the session. |
| 137 | + |
| 138 | +As an example: |
| 139 | + |
| 140 | +```php |
| 141 | +// In config/routes.php |
| 142 | + |
| 143 | +use Zend\Expressive\Authentication\AuthenticationMiddleware; |
| 144 | +use Zend\Expressive\Session\SessionMiddleware; |
| 145 | + |
| 146 | +$app->post('/api/books', [ |
| 147 | + SessionMiddleware::class, |
| 148 | + AuthenticationMiddleware::class, |
| 149 | + CreateBookHandler::class, |
| 150 | +]); |
| 151 | +``` |
| 152 | + |
| 153 | +> ### Create a delegator factory |
| 154 | +> |
| 155 | +> You may want to consider creating a delegator factory for registering these |
| 156 | +> two middleware in a pipeline with any handler. As an example: |
| 157 | +> |
| 158 | +> ```php |
| 159 | +> function (ContainerInterface $container, $serviceName, callable $callback) |
| 160 | +> { |
| 161 | +> $pipeline = new MiddlewarePipe(); |
| 162 | +> $pipeline->pipe($container->get(SessionMiddleware::class)); |
| 163 | +> $pipeline->pipe($container->get(AuthenticationMiddleware::class)); |
| 164 | +> $pipeline->pipe($callback()); |
| 165 | +> return $pipeline; |
| 166 | +> } |
| 167 | +> ``` |
0 commit comments