Passed
Pull Request — develop (#295)
by Peter
04:30
created

Configuration::__construct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
nc 4
nop 4
dl 0
loc 34
rs 9.2888
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2022 SURFnet bv
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject;
20
21
use Exception;
22
use ParagonIE\ConstantTime\Binary;
23
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidCookieTypeException;
24
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidEncryptionKeyException;
25
26
class Configuration
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Configuration
Loading history...
27
{
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var string
30
     */
31
    private $name;
0 ignored issues
show
Coding Style introduced by
Private member variable "name" must be prefixed with an underscore
Loading history...
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @var CookieType
35
     */
36
    private $type;
0 ignored issues
show
Coding Style introduced by
Private member variable "type" must be prefixed with an underscore
Loading history...
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @var int
40
     */
41
    private $lifetime;
0 ignored issues
show
Coding Style introduced by
Private member variable "lifetime" must be prefixed with an underscore
Loading history...
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var string
45
     */
46
    private $encryptionKey;
0 ignored issues
show
Coding Style introduced by
Private member variable "encryptionKey" must be prefixed with an underscore
Loading history...
47
48
    public function __construct(string $name, string $type, int $lifetime, string $encryptionKey)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
49
    {
50
        $this->name = $name;
51
        $this->type = CookieType::fromConfiguration($type);
52
        if ($lifetime === 0 && $this->type->isPersistent()) {
53
            throw new InvalidCookieTypeException(
54
                'When using a persistent cookie, you must configure a non zero cookie lifetime'
55
            );
56
        }
57
        $this->lifetime = $lifetime;
58
59
        // Convert the key from the configuration from hex to binary. sodium_hex2bin
60
        try {
61
            $binaryKey = sodium_hex2bin($encryptionKey);
62
        } catch (Exception $e) {
63
            // The key contains non-hexadecimal values. Show a custom error message in logs.
64
            throw new InvalidEncryptionKeyException(
65
                'The configured SSO on 2FA encryption key contains illegal characters. It should be a 64 digits long ' .
66
                'hexadecimal value. Example value: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
67
                0,
68
                $e
69
            );
70
        }
71
        // The key length, converted back to binary must be 32 bytes long
72
        if (Binary::safeStrlen($binaryKey) < SODIUM_CRYPTO_STREAM_KEYBYTES) {
73
            throw new InvalidEncryptionKeyException(
74
                sprintf(
75
                    'The configured SSO on 2FA encryption key must be exactly %d bytes. ' .
76
                    'This comes down to 64 hex digits value, configured in the sso_encryption_key configuration option',
77
                    SODIUM_CRYPTO_STREAM_KEYBYTES
78
                )
79
            );
80
        }
81
        $this->encryptionKey = $binaryKey;
82
    }
83
84
    public function getName(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getName()
Loading history...
85
    {
86
        return $this->name;
87
    }
88
89
    public function isPersistent(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isPersistent()
Loading history...
90
    {
91
        return $this->type->isPersistent();
92
    }
93
94
    public function getLifetime(): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getLifetime()
Loading history...
95
    {
96
        return $this->lifetime;
97
    }
98
99
    public function getEncryptionKey(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getEncryptionKey()
Loading history...
100
    {
101
        return $this->encryptionKey;
102
    }
103
}
104