Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
Línea 2... Línea 2...
2
 
2
 
Línea 3... Línea -...
3
declare(strict_types=1);
-
 
4
 
-
 
5
/*
-
 
6
 * The MIT License (MIT)
-
 
7
 *
-
 
8
 * Copyright (c) 2014-2018 Spomky-Labs
-
 
9
 *
-
 
10
 * This software may be modified and distributed under the terms
-
 
11
 * of the MIT license.  See the LICENSE file for details.
-
 
12
 */
3
declare(strict_types=1);
Línea -... Línea 4...
-
 
4
 
-
 
5
namespace OTPHP;
13
 
6
 
14
namespace OTPHP;
7
use InvalidArgumentException;
-
 
8
use function array_key_exists;
-
 
9
use function assert;
Línea 15... Línea 10...
15
 
10
use function in_array;
16
use Assert\Assertion;
11
use function is_int;
17
use ParagonIE\ConstantTime\Base32;
12
use function is_string;
18
 
13
 
19
trait ParameterTrait
14
trait ParameterTrait
20
{
15
{
Línea 21... Línea 16...
21
    /**
16
    /**
22
     * @var array
17
     * @var array<non-empty-string, mixed>
23
     */
18
     */
24
    private $parameters = [];
19
    private array $parameters = [];
Línea 25... Línea 20...
25
 
20
 
26
    /**
21
    /**
27
     * @var string|null
22
     * @var non-empty-string|null
28
     */
23
     */
Línea 29... Línea -...
29
    private $issuer = null;
-
 
30
 
-
 
31
    /**
-
 
32
     * @var string|null
24
    private null|string $issuer = null;
Línea 33... Línea 25...
33
     */
25
 
34
    private $label = null;
26
    /**
35
 
27
     * @var non-empty-string|null
36
    /**
28
     */
37
     * @var bool
29
    private null|string $label = null;
38
     */
30
 
Línea 39... Línea 31...
39
    private $issuer_included_as_parameter = true;
31
    private bool $issuer_included_as_parameter = true;
40
 
32
 
41
    /**
33
    /**
Línea 42... Línea 34...
42
     * @return array
34
     * @return array<non-empty-string, mixed>
43
     */
35
     */
Línea 44... Línea -...
44
    public function getParameters(): array
-
 
45
    {
-
 
46
        $parameters = $this->parameters;
-
 
47
 
36
    public function getParameters(): array
48
        if (null !== $this->getIssuer() && $this->isIssuerIncludedAsParameter() === true) {
37
    {
49
            $parameters['issuer'] = $this->getIssuer();
38
        $parameters = $this->parameters;
50
        }
-
 
-
 
39
 
Línea 51... Línea -...
51
 
-
 
52
        return $parameters;
40
        if ($this->getIssuer() !== null && $this->isIssuerIncludedAsParameter() === true) {
53
    }
-
 
54
 
-
 
55
    /**
-
 
56
     * @return string
-
 
57
     */
41
            $parameters['issuer'] = $this->getIssuer();
Línea 58... Línea -...
58
    public function getSecret(): string
-
 
59
    {
-
 
60
        return $this->getParameter('secret');
-
 
61
    }
42
        }
62
 
43
 
63
    /**
44
        return $parameters;
64
     * @param string|null $secret
45
    }
Línea 65... Línea -...
65
     */
-
 
66
    private function setSecret($secret)
-
 
67
    {
-
 
68
        $this->setParameter('secret', $secret);
46
 
69
    }
47
    public function getSecret(): string
70
 
48
    {
71
    /**
49
        $value = $this->getParameter('secret');
Línea 72... Línea -...
72
     * @return string|null
-
 
73
     */
-
 
74
    public function getLabel()
-
 
75
    {
50
        (is_string($value) && $value !== '') || throw new InvalidArgumentException('Invalid "secret" parameter.');
76
        return $this->label;
51
 
77
    }
52
        return $value;
78
 
53
    }
Línea 79... Línea -...
79
    /**
-
 
80
     * @param string $label
-
 
81
     */
-
 
82
    public function setLabel(string $label)
54
 
83
    {
55
    public function getLabel(): null|string
84
        $this->setParameter('label', $label);
56
    {
85
    }
57
        return $this->label;
Línea 86... Línea -...
86
 
-
 
87
    /**
-
 
88
     * @return string|null
-
 
89
     */
58
    }
90
    public function getIssuer()
59
 
91
    {
60
    public function setLabel(string $label): void
92
        return $this->issuer;
61
    {
Línea 93... Línea -...
93
    }
-
 
94
 
-
 
95
    /**
-
 
96
     * @param string $issuer
62
        $this->setParameter('label', $label);
97
     */
63
    }
98
    public function setIssuer(string $issuer)
64
 
99
    {
65
    public function getIssuer(): null|string
Línea 100... Línea -...
100
        $this->setParameter('issuer', $issuer);
-
 
101
    }
-
 
102
 
-
 
103
    /**
66
    {
104
     * @return bool
67
        return $this->issuer;
105
     */
68
    }
106
    public function isIssuerIncludedAsParameter(): bool
-
 
-
 
69
 
Línea 107... Línea -...
107
    {
-
 
108
        return $this->issuer_included_as_parameter;
70
    public function setIssuer(string $issuer): void
109
    }
-
 
110
 
-
 
111
    /**
-
 
112
     * @param bool $issuer_included_as_parameter
-
 
113
     */
71
    {
Línea 114... Línea -...
114
    public function setIssuerIncludedAsParameter(bool $issuer_included_as_parameter)
-
 
115
    {
-
 
116
        $this->issuer_included_as_parameter = $issuer_included_as_parameter;
-
 
117
    }
72
        $this->setParameter('issuer', $issuer);
118
 
73
    }
119
    /**
74
 
120
     * @return int
-
 
-
 
75
    public function isIssuerIncludedAsParameter(): bool
Línea 121... Línea -...
121
     */
-
 
122
    public function getDigits(): int
76
    {
123
    {
-
 
124
        return $this->getParameter('digits');
-
 
125
    }
-
 
126
 
-
 
127
    /**
77
        return $this->issuer_included_as_parameter;
Línea 128... Línea -...
128
     * @param int $digits
-
 
129
     */
-
 
130
    private function setDigits(int $digits)
-
 
131
    {
-
 
132
        $this->setParameter('digits', $digits);
-
 
133
    }
78
    }
134
 
79
 
135
    /**
80
    public function setIssuerIncludedAsParameter(bool $issuer_included_as_parameter): void
136
     * @return string
81
    {
Línea 137... Línea -...
137
     */
-
 
138
    public function getDigest(): string
-
 
139
    {
-
 
140
        return $this->getParameter('algorithm');
-
 
141
    }
-
 
142
 
82
        $this->issuer_included_as_parameter = $issuer_included_as_parameter;
143
    /**
83
    }
144
     * @param string $digest
84
 
145
     */
85
    public function getDigits(): int
146
    private function setDigest(string $digest)
86
    {
Línea 147... Línea 87...
147
    {
87
        $value = $this->getParameter('digits');
148
        $this->setParameter('algorithm', $digest);
88
        (is_int($value) && $value > 0) || throw new InvalidArgumentException('Invalid "digits" parameter.');
Línea 149... Línea -...
149
    }
-
 
150
 
-
 
151
    /**
-
 
152
     * @param string $parameter
-
 
153
     *
89
 
154
     * @return bool
90
        return $value;
155
     */
91
    }
Línea 156... Línea 92...
156
    public function hasParameter(string $parameter): bool
92
 
157
    {
93
    public function getDigest(): string
158
        return array_key_exists($parameter, $this->parameters);
94
    {
159
    }
95
        $value = $this->getParameter('algorithm');
Línea 160... Línea 96...
160
 
96
        (is_string($value) && $value !== '') || throw new InvalidArgumentException('Invalid "algorithm" parameter.');
161
    /**
97
 
162
     * @param string $parameter
98
        return $value;
163
     *
99
    }
164
     * @return mixed
100
 
165
     */
101
    public function hasParameter(string $parameter): bool
Línea -... Línea 102...
-
 
102
    {
-
 
103
        return array_key_exists($parameter, $this->parameters);
-
 
104
    }
-
 
105
 
-
 
106
    public function getParameter(string $parameter): mixed
-
 
107
    {
-
 
108
        if ($this->hasParameter($parameter)) {
-
 
109
            return $this->getParameters()[$parameter];
-
 
110
        }
-
 
111
 
-
 
112
        throw new InvalidArgumentException(sprintf('Parameter "%s" does not exist', $parameter));
-
 
113
    }
-
 
114
 
-
 
115
    public function setParameter(string $parameter, mixed $value): void
-
 
116
    {
166
    public function getParameter(string $parameter)
117
        $map = $this->getParameterMap();
167
    {
118
 
168
        if ($this->hasParameter($parameter)) {
119
        if (array_key_exists($parameter, $map) === true) {
169
            return $this->getParameters()[$parameter];
120
            $callback = $map[$parameter];
170
        }
121
            $value = $callback($value);
171
 
122
        }
172
        throw new \InvalidArgumentException(sprintf('Parameter "%s" does not exist', $parameter));
123
 
173
    }
-
 
174
 
-
 
175
    /**
124
        if (property_exists($this, $parameter)) {
176
     * @param string $parameter
-
 
177
     * @param mixed  $value
125
            $this->{$parameter} = $value;
178
     */
-
 
179
    public function setParameter(string $parameter, $value)
126
        } else {
180
    {
127
            $this->parameters[$parameter] = $value;
181
        $map = $this->getParameterMap();
-
 
Línea 182... Línea 128...
182
 
128
        }
183
        if (true === array_key_exists($parameter, $map)) {
129
    }
-
 
130
 
184
            $callback = $map[$parameter];
131
    public function setSecret(string $secret): void
185
            $value = $callback($value);
132
    {
186
        }
133
        $this->setParameter('secret', $secret);
-
 
134
    }
-
 
135
 
-
 
136
    public function setDigits(int $digits): void
Línea 187... Línea 137...
187
 
137
    {
188
        if (property_exists($this, $parameter)) {
138
        $this->setParameter('digits', $digits);
189
            $this->$parameter = $value;
139
    }
190
        } else {
140
 
Línea 191... Línea 141...
191
            $this->parameters[$parameter] = $value;
141
    public function setDigest(string $digest): void
192
        }
142
    {
193
    }
143
        $this->setParameter('algorithm', $digest);
-
 
144
    }
-
 
145
 
194
 
146
    /**
-
 
147
     * @return array<non-empty-string, callable>
Línea 195... Línea 148...
195
    /**
148
     */
196
     * @return array
149
    protected function getParameterMap(): array
197
     */
150
    {
198
    protected function getParameterMap(): array
151
        return [
Línea 199... Línea 152...
199
    {
152
            'label' => function (string $value): string {
200
        return [
153
                assert($value !== '');
201
            'label'     => function ($value) {
-
 
202
                Assertion::false($this->hasColon($value), 'Label must not contain a colon.');
-
 
203
 
154
                $this->hasColon($value) === false || throw new InvalidArgumentException(
204
                return $value;
155
                    'Label must not contain a colon.'
205
            },
156
                );
206
            'secret'    => function ($value) {
157
 
207
                if (null === $value) {
158
                return $value;
208
                    $value = Base32::encodeUpper(random_bytes(64));
159
            },
209
                }
160
            'secret' => static fn (string $value): string => mb_strtoupper(trim($value, '=')),
210
                $value = trim(strtoupper($value), '=');
161
            'algorithm' => static function (string $value): string {
211
 
162
                $value = mb_strtolower($value);
Línea 212... Línea 163...
212
                return $value;
163
                in_array($value, hash_algos(), true) || throw new InvalidArgumentException(sprintf(