1
|
|
|
<?php namespace Limoncello\Passport\Package; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Doctrine\DBAL\Connection; |
20
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface as CCI; |
21
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
22
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
23
|
|
|
use Limoncello\Passport\Adaptors\MySql\MySqlPassportServerIntegration; |
24
|
|
|
use Limoncello\Passport\Adaptors\MySql\TokenRepository; |
25
|
|
|
use Limoncello\Passport\Contracts\Entities\DatabaseSchemeInterface; |
26
|
|
|
use Limoncello\Passport\Contracts\PassportServerIntegrationInterface; |
27
|
|
|
use Limoncello\Passport\Contracts\Repositories\TokenRepositoryInterface; |
28
|
|
|
use Limoncello\Passport\Package\PassportSettings as C; |
29
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Passport |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
class MySqlPassportContainerConfigurator extends BasePassportContainerConfigurator implements CCI |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
* |
39
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
40
|
|
|
*/ |
41
|
|
|
public static function configureContainer(LimoncelloContainerInterface $container) |
42
|
|
|
{ |
43
|
|
|
static::baseConfigureContainer($container); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$container[PassportServerIntegrationInterface::class] = function (PsrContainerInterface $container) { |
46
|
|
|
assert($container !== null); |
47
|
|
|
|
48
|
|
|
return new class ($container) extends MySqlPassportServerIntegration |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var PsrContainerInterface |
52
|
|
|
*/ |
53
|
|
|
private $container; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $settings; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param PsrContainerInterface $container |
62
|
|
|
*/ |
63
|
|
|
public function __construct(PsrContainerInterface $container) |
64
|
|
|
{ |
65
|
|
|
$this->container = $container; |
66
|
|
|
$this->settings = $container->get(SettingsProviderInterface::class)->get(C::class); |
67
|
|
|
|
68
|
|
|
/** @var Connection $connection */ |
69
|
|
|
$connection = $container->get(Connection::class); |
70
|
|
|
|
71
|
|
|
parent::__construct( |
72
|
|
|
$connection, |
73
|
|
|
$this->settings[C::KEY_DEFAULT_CLIENT_ID], |
74
|
|
|
$this->settings[C::KEY_APPROVAL_URI_STRING], |
75
|
|
|
$this->settings[C::KEY_ERROR_URI_STRING], |
76
|
|
|
$this->settings[C::KEY_CODE_EXPIRATION_TIME_IN_SECONDS], |
77
|
|
|
$this->settings[C::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS], |
78
|
|
|
$this->settings[C::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function validateUserId(string $userName, string $password) |
86
|
|
|
{ |
87
|
|
|
$validator = $this->settings[C::KEY_USER_CREDENTIALS_VALIDATOR]; |
88
|
|
|
$nullOrUserId = call_user_func($validator, $this->container, $userName, $password); |
89
|
|
|
|
90
|
|
|
return $nullOrUserId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function verifyAllowedUserScope(int $userIdentity, array $scope = null) |
97
|
|
|
{ |
98
|
|
|
$validator = $this->settings[C::KEY_USER_SCOPE_VALIDATOR]; |
99
|
|
|
$nullOrScope = call_user_func($validator, $this->container, $userIdentity, $scope); |
100
|
|
|
|
101
|
|
|
return $nullOrScope; |
102
|
|
|
} |
103
|
|
|
}; |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
$container[TokenRepositoryInterface::class] = function (PsrContainerInterface $container) { |
107
|
|
|
$connection = $container->get(Connection::class); |
108
|
|
|
$scheme = $container->get(DatabaseSchemeInterface::class); |
109
|
|
|
|
110
|
|
|
return new TokenRepository($connection, $scheme); |
111
|
|
|
}; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.