summaryrefslogtreecommitdiff
path: root/vendor/spomky-labs/otphp/src/ParameterTrait.php
blob: 69fa774db7c3c22d25875b07841819966a79f6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

declare(strict_types=1);

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2019 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

namespace OTPHP;

use Assert\Assertion;
use InvalidArgumentException;
use ParagonIE\ConstantTime\Base32;
use function Safe\sprintf;

trait ParameterTrait
{
    /**
     * @var array<string, mixed>
     */
    private $parameters = [];

    /**
     * @var string|null
     */
    private $issuer;

    /**
     * @var string|null
     */
    private $label;

    /**
     * @var bool
     */
    private $issuer_included_as_parameter = true;

    /**
     * @return array<string, mixed>
     */
    public function getParameters(): array
    {
        $parameters = $this->parameters;

        if (null !== $this->getIssuer() && true === $this->isIssuerIncludedAsParameter()) {
            $parameters['issuer'] = $this->getIssuer();
        }

        return $parameters;
    }

    public function getSecret(): string
    {
        return $this->getParameter('secret');
    }

    private function setSecret(?string $secret): void
    {
        $this->setParameter('secret', $secret);
    }

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(string $label): void
    {
        $this->setParameter('label', $label);
    }

    public function getIssuer(): ?string
    {
        return $this->issuer;
    }

    public function setIssuer(string $issuer): void
    {
        $this->setParameter('issuer', $issuer);
    }

    public function isIssuerIncludedAsParameter(): bool
    {
        return $this->issuer_included_as_parameter;
    }

    public function setIssuerIncludedAsParameter(bool $issuer_included_as_parameter): void
    {
        $this->issuer_included_as_parameter = $issuer_included_as_parameter;
    }

    public function getDigits(): int
    {
        return $this->getParameter('digits');
    }

    private function setDigits(int $digits): void
    {
        $this->setParameter('digits', $digits);
    }

    public function getDigest(): string
    {
        return $this->getParameter('algorithm');
    }

    private function setDigest(string $digest): void
    {
        $this->setParameter('algorithm', $digest);
    }

    public function hasParameter(string $parameter): bool
    {
        return \array_key_exists($parameter, $this->parameters);
    }

    public function getParameter(string $parameter)
    {
        if ($this->hasParameter($parameter)) {
            return $this->getParameters()[$parameter];
        }

        throw new InvalidArgumentException(sprintf('Parameter "%s" does not exist', $parameter));
    }

    public function setParameter(string $parameter, $value): void
    {
        $map = $this->getParameterMap();

        if (true === \array_key_exists($parameter, $map)) {
            $callback = $map[$parameter];
            $value = $callback($value);
        }

        if (property_exists($this, $parameter)) {
            $this->$parameter = $value;
        } else {
            $this->parameters[$parameter] = $value;
        }
    }

    /**
     * @return array<string, mixed>
     */
    protected function getParameterMap(): array
    {
        return [
            'label' => function ($value) {
                Assertion::false($this->hasColon($value), 'Label must not contain a colon.');

                return $value;
            },
            'secret' => function ($value): string {
                if (null === $value) {
                    $value = Base32::encodeUpper(random_bytes(64));
                }
                $value = trim(mb_strtoupper($value), '=');

                return $value;
            },
            'algorithm' => function ($value): string {
                $value = mb_strtolower($value);
                Assertion::inArray($value, hash_algos(), sprintf('The "%s" digest is not supported.', $value));

                return $value;
            },
            'digits' => function ($value): int {
                Assertion::greaterThan($value, 0, 'Digits must be at least 1.');

                return (int) $value;
            },
            'issuer' => function ($value) {
                Assertion::false($this->hasColon($value), 'Issuer must not contain a colon.');

                return $value;
            },
        ];
    }

    private function hasColon(string $value): bool
    {
        $colons = [':', '%3A', '%3a'];
        foreach ($colons as $colon) {
            if (false !== mb_strpos($value, $colon)) {
                return true;
            }
        }

        return false;
    }
}