|
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
|
|
|
*/ |
|
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 |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $name; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CookieType |
|
35
|
|
|
*/ |
|
36
|
|
|
private $type; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $lifetime; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $encryptionKey; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(string $name, string $type, int $lifetime, string $encryptionKey) |
|
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 |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function isPersistent(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->type->isPersistent(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getLifetime(): int |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->lifetime; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getEncryptionKey(): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->encryptionKey; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|