Skip to content

Commit 1162025

Browse files
committed
v1.0
0 parents  commit 1162025

17 files changed

+730
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Komar Stanislau
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "micro/plugin-oauth2-client",
3+
"description": "Micro Framework: OAuth2 client based on \"league/oauth2-client\"",
4+
"type": "library",
5+
"version": "1.0",
6+
"require": {
7+
"micro/plugin-locator": "^1",
8+
"league/oauth2-client": "^2"
9+
},
10+
"license": "MIT",
11+
"autoload": {
12+
"psr-4": {
13+
"Micro\\Plugin\\OAuth2\\Client\\": "src/"
14+
}
15+
},
16+
"authors": [
17+
{
18+
"name": "Stanislau Komar",
19+
"email": "[email protected]"
20+
}
21+
]
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\OAuth2\Client\Configuration;
15+
16+
use Micro\Plugin\OAuth2\Client\Configuration\Provider\OAuth2ClientProviderConfigurationInterface;
17+
18+
/**
19+
* @author Stanislau Komar <[email protected]>
20+
*/
21+
interface OAuth2ClientPluginConfigurationInterface
22+
{
23+
/**
24+
* @param string $providerName
25+
*
26+
* @return OAuth2ClientProviderConfigurationInterface
27+
*/
28+
public function getProviderConfiguration(string $providerName): OAuth2ClientProviderConfigurationInterface;
29+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\OAuth2\Client\Configuration\Provider;
15+
16+
use Micro\Framework\Kernel\Configuration\PluginRoutingKeyConfiguration;
17+
18+
/**
19+
* @author Stanislau Komar <[email protected]>
20+
*/
21+
class OAuth2ClientProviderConfiguration extends PluginRoutingKeyConfiguration implements OAuth2ClientProviderConfigurationInterface
22+
{
23+
const CFG_TYPE = 'MICRO_OAUTH2_%s_TYPE';
24+
const CFG_CLIENT_ID = 'MICRO_OAUTH2_%s_CLIENT_ID';
25+
const CFG_CLIENT_SECRET = 'MICRO_OAUTH2_%s_CLIENT_SECRET';
26+
const CFG_URL_AUTHORIZATION = 'MICRO_OAUTH2_%s_CLIENT_URL_AUTHORIZATION';
27+
const CFG_URL_REDIRECT = 'MICRO_OAUTH2_%s_CLIENT_URL_REDIRECT';
28+
const CFG_URL_RESOURCE_OWNER = 'MICRO_OAUTH2_%s_CLIENT_URL_RESOURCE_OWNER';
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
final public function getType(): string
34+
{
35+
return $this->get(self::CFG_TYPE);
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
final public function getClientId(): string
42+
{
43+
return $this->get(self::CFG_CLIENT_ID);
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
final public function getClientSecret(): string
50+
{
51+
return $this->get(self::CFG_CLIENT_SECRET);
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function getUrlRedirect(): string
58+
{
59+
return rtrim($this->get(self::CFG_URL_REDIRECT), '/');
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function getUrlAuthorization(): string
66+
{
67+
return rtrim($this->get(self::CFG_URL_AUTHORIZATION), '/');
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
public function getUrlResourceOwnerDetails(): string
74+
{
75+
return rtrim($this->get(self::CFG_URL_RESOURCE_OWNER), '/');
76+
}
77+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\OAuth2\Client\Configuration\Provider;
15+
16+
/**
17+
* @author Stanislau Komar <[email protected]>
18+
*/
19+
interface OAuth2ClientProviderConfigurationInterface
20+
{
21+
/**
22+
* @return string
23+
*/
24+
public function getType(): string;
25+
26+
/**
27+
* @return string
28+
*/
29+
public function getClientId(): string;
30+
31+
/**
32+
* @return string
33+
*/
34+
public function getClientSecret(): string;
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getUrlRedirect(): string;
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getUrlAuthorization(): string;
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getUrlResourceOwnerDetails(): string;
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\OAuth2\Client\Exception;
15+
16+
/**
17+
* @author Stanislau Komar <[email protected]>
18+
*/
19+
class ProviderAdapterNotRegisteredException extends \RuntimeException
20+
{
21+
/**
22+
* @param string $providerType
23+
* @param int|null $code
24+
* @param \Throwable|null $throwable
25+
*/
26+
public function __construct(string $providerType, \Throwable $throwable = null)
27+
{
28+
parent::__construct(
29+
sprintf('OAuth2 provider "%s" is not registered.', $providerType),
30+
0,
31+
$throwable,
32+
);
33+
}
34+
}

src/Facade/Oauth2ClientFacade.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\OAuth2\Client\Facade;
15+
16+
use League\OAuth2\Client\Provider\AbstractProvider;
17+
use Micro\Plugin\OAuth2\Client\Configuration\OAuth2ClientPluginConfigurationInterface;
18+
use Micro\Plugin\OAuth2\Client\Provider\Locator\ProviderPluginLocatorFactoryInterface;
19+
20+
/**
21+
* @author Stanislau Komar <[email protected]>
22+
*/
23+
class Oauth2ClientFacade implements Oauth2ClientFacadeInterface
24+
{
25+
/**
26+
* @var array<string, AbstractProvider>
27+
*/
28+
private array $providers = [];
29+
30+
/**
31+
* @param ProviderPluginLocatorFactoryInterface $providerPluginLocatorFactory
32+
* @param OAuth2ClientPluginConfigurationInterface $pluginConfiguration
33+
*/
34+
public function __construct(
35+
private readonly ProviderPluginLocatorFactoryInterface $providerPluginLocatorFactory,
36+
private readonly OAuth2ClientPluginConfigurationInterface $pluginConfiguration
37+
)
38+
{
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function getProvider(string $providerName): AbstractProvider
45+
{
46+
if(array_key_exists($providerName, $this->providers)) {
47+
return $this->providers[$providerName];
48+
}
49+
50+
$providerConfig = $this->pluginConfiguration->getProviderConfiguration($providerName);
51+
$provider = $this->providerPluginLocatorFactory
52+
->create()
53+
->lookup($providerConfig->getType())
54+
->createProvider($providerName);
55+
56+
$this->providers[$providerName] = $provider;
57+
58+
return $provider;
59+
}
60+
}

0 commit comments

Comments
 (0)