Completed
Push — master ( 7c3a00...4fd5ad )
by Neomerx
04:42
created

getTokenCustomPropertiesProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Limoncello\Contracts\Settings\SettingsInterface;
20
use Limoncello\Core\Reflection\CheckCallableTrait;
21
use Limoncello\Passport\Contracts\Entities\TokenInterface;
22
use Psr\Container\ContainerInterface;
23
use ReflectionParameter;
24
25
/**
26
 * @package Limoncello\Passport
27
 */
28
abstract class PassportSettings implements SettingsInterface
29
{
30
    use CheckCallableTrait;
31
32
    /** Config key */
33
    const KEY_ENABLE_LOGS = 0;
34
35
    /** Config key */
36
    const KEY_APPROVAL_URI_STRING = self::KEY_ENABLE_LOGS + 1;
37
38
    /** Config key */
39
    const KEY_ERROR_URI_STRING = self::KEY_APPROVAL_URI_STRING + 1;
40
41
    /** Config key */
42
    const KEY_DEFAULT_CLIENT_ID = self::KEY_ERROR_URI_STRING + 1;
43
44
    /** Config key */
45
    const KEY_CODE_EXPIRATION_TIME_IN_SECONDS = self::KEY_DEFAULT_CLIENT_ID + 1;
46
47
    /** Config key */
48
    const KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS = self::KEY_CODE_EXPIRATION_TIME_IN_SECONDS + 1;
49
50
    /** Config key */
51
    const KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH = self::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS + 1;
52
53
    /** Config key */
54
    const KEY_USER_TABLE_NAME = self::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH + 1;
55
56
    /** Config key */
57
    const KEY_USER_PRIMARY_KEY_NAME = self::KEY_USER_TABLE_NAME + 1;
58
59
    /** Config key */
60
    const KEY_USER_CREDENTIALS_VALIDATOR = self::KEY_USER_PRIMARY_KEY_NAME + 1;
61
62
    /** Config key */
63
    const KEY_FAILED_AUTHENTICATION_FACTORY = self::KEY_USER_CREDENTIALS_VALIDATOR + 1;
64
65
    /** Config key */
66
    const KEY_USER_SCOPE_VALIDATOR = self::KEY_FAILED_AUTHENTICATION_FACTORY + 1;
67
68
    /** Config key */
69
    const KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER = self::KEY_USER_SCOPE_VALIDATOR + 1;
70
71
    /** Config key */
72
    const KEY_LAST = self::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER + 1;
73
74
    /**
75
     * @return string
76
     */
77
    abstract protected function getApprovalUri(): string;
78
79
    /**
80
     * @return string
81
     */
82
    abstract protected function getErrorUri(): string;
83
84
    /**
85
     * @return string
86
     */
87
    abstract protected function getDefaultClientId(): string;
88
89
    /**
90
     * @return string
91
     */
92
    abstract protected function getUserTableName(): string;
93
94
    /**
95
     * @return string
96
     */
97
    abstract protected function getUserPrimaryKeyName(): string;
98
99
    /**
100
     * Should return static callable for user credentials validator (login and password).
101
     *
102
     * Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName'
103
     *
104
     * Method signature
105
     *
106
     * public static function validateUser(ContainerInterface $container, string $userName, string $password): ?int
107
     *
108
     * which returns either user ID (int) or null if user not found/invalid credentials.
109
     *
110
     * @return callable
111
     */
112
    abstract protected function getUserCredentialsValidator(): callable;
113
114
    /**
115
     * Should return static callable for user scope validator (allowed scope identities).
116
     *
117
     * Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName'
118
     *
119
     * Method signature
120
     *
121
     * public static function validateScope(ContainerInterface $container, int $userId, array $scopeIds = null): ?array
122
     *
123
     * which returns either changed allowed scope IDs or null if scope was not changed or throws auth exception.
124
     *
125
     * @return callable
126
     */
127
    abstract protected function getUserScopeValidator(): callable;
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function get(): array
133
    {
134
        $credentialsValidator = $this->getUserCredentialsValidator();
135
        $scopeValidator       = $this->getUserScopeValidator();
136
        $customPropsProvider  = $this->getTokenCustomPropertiesProvider();
137
138
        // check that validators are valid callable (static with proper in/out signature).
139
        assert($this->checkPublicStaticCallable(
140
            $credentialsValidator,
141
            [ContainerInterface::class, 'string', 'string']
142
        ));
143
        assert($this->checkPublicStaticCallable(
144
            $scopeValidator,
145
            [
146
                ContainerInterface::class,
147
                'int',
148
                function (ReflectionParameter $parameter) {
149
                    return $parameter->allowsNull() === true && $parameter->isArray() === true;
150
                }
151
            ]
152
        ));
153
        assert($customPropsProvider === null || $this->checkPublicStaticCallable(
154
            $customPropsProvider,
155
            [ContainerInterface::class, TokenInterface::class],
156
            'array'
157
        ));
158
159
        return [
160
            static::KEY_ENABLE_LOGS                          => false,
161
            static::KEY_APPROVAL_URI_STRING                  => $this->getApprovalUri(),
162
            static::KEY_ERROR_URI_STRING                     => $this->getErrorUri(),
163
            static::KEY_DEFAULT_CLIENT_ID                    => $this->getDefaultClientId(),
164
            static::KEY_CODE_EXPIRATION_TIME_IN_SECONDS      => 10 * 60,
165
            static::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS     => 60 * 60,
166
            static::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH => true,
167
            static::KEY_USER_TABLE_NAME                      => $this->getUserTableName(),
168
            static::KEY_USER_PRIMARY_KEY_NAME                => $this->getUserPrimaryKeyName(),
169
            static::KEY_USER_CREDENTIALS_VALIDATOR           => $credentialsValidator,
170
            static::KEY_USER_SCOPE_VALIDATOR                 => $scopeValidator,
171
            static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER     => $customPropsProvider,
172
        ];
173
    }
174
175
    /**
176
     * @return null|callable (static)
177
     */
178
    protected function getTokenCustomPropertiesProvider()
179
    {
180
        return null;
181
    }
182
}
183