Completed
Push — master ( 0e7cb7...85d6ec )
by Neomerx
02:27
created

PassportSettings::get()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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