|
| 1 | + |
| 2 | +# Micro Framework Oauth2 client plugin |
| 3 | + |
| 4 | +Micro Framework: OAuth2 client based on "league/oauth2-client" |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install plugin with composer |
| 8 | + |
| 9 | +```bash |
| 10 | +$ composer require micro/plugin-oauth2-client |
| 11 | +``` |
| 12 | + |
| 13 | +And install a specific provider(s). |
| 14 | + |
| 15 | +Available providers: |
| 16 | +[micro/plugin-oauth2-client-keycloak]() |
| 17 | + |
| 18 | +Аnd then add plugin to the list of plugins (etc/plugins.php) |
| 19 | + |
| 20 | +```php |
| 21 | + |
| 22 | +$pluginsCommon = [ |
| 23 | + //....OTHER PLUGINS .... |
| 24 | + Micro\Plugin\OAuth2\Client\OAuth2ClientPlugin::class, |
| 25 | + Micro\Plugin\OAuth2\Client\Keycloak\OAuth2KeycloakProviderPlugin::class, |
| 26 | +]; |
| 27 | + |
| 28 | +``` |
| 29 | +Configure your oauth2 providers |
| 30 | + |
| 31 | +The adapter configuration template usually looks like this `MICRO_OAUTH2_<PROVIDER_NAME>_<PROVIDER_SETTING>` |
| 32 | + |
| 33 | +Default adapter name "default" |
| 34 | + |
| 35 | + |
| 36 | +```dotenv |
| 37 | +MICRO_OAUTH2_DEFAULT_TYPE=keycloak |
| 38 | +MICRO_OAUTH2_DEFAULT_CLIENT_ID=<my-client-id> |
| 39 | +MICRO_OAUTH2_DEFAULT_CLIENT_SECRET=<client-secret-key> |
| 40 | +MICRO_OAUTH2_DEFAULT_CLIENT_URL_AUTHORIZATION=<auth url> |
| 41 | +MICRO_OAUTH2_DEFAULT_CLIENT_URL_REDIRECT=<redirect url> |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## Usage/Examples |
| 46 | + |
| 47 | +Index document |
| 48 | + |
| 49 | +```php |
| 50 | + |
| 51 | +use use Micro\Plugin\OAuth2\Client\Facade\Oauth2ClientFacadeInterface; |
| 52 | + |
| 53 | +/** Unauthorized user */ |
| 54 | +$client = $container->get(Oauth2ClientFacadeInterface::class); |
| 55 | +$provider = $client->getProvider('default'); |
| 56 | + |
| 57 | +$provider->getAuthorizationUrl(); This method will return the full path to the server for user authorization. |
| 58 | + |
| 59 | + |
| 60 | +/*** Response from the authorization server with a code */ |
| 61 | +$accessToken = $provider->getAccessToken('authorization_code', [ |
| 62 | + 'code' => $_GET['code'], |
| 63 | +]); |
| 64 | + |
| 65 | +$owner = $provider->getResourceOwner($accessToken); |
| 66 | + |
| 67 | +$id = $owner->getId(); |
| 68 | +$ownerData = $owner->toArray(); //Associated data with the user. |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +## Support |
| 74 | + |
| 75 | +For support, email [email protected]. |
| 76 | + |
| 77 | + |
| 78 | +## Authors |
| 79 | + |
| 80 | +- [@stanislau_komar](https://www.github.com/asisyas) |
| 81 | + |
| 82 | + |
| 83 | +## License |
| 84 | + |
| 85 | +[MIT](LICENSE) |
| 86 | + |
0 commit comments