-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAuthenticationSecret.php
More file actions
155 lines (135 loc) · 5 KB
/
AuthenticationSecret.php
File metadata and controls
155 lines (135 loc) · 5 KB
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
<?php declare(strict_types=1);
/**
* Class AuthenticationSecret
*
* @author Robin 'codeFareith' von den Bergen <robinvonberg@gmx.de>
* @copyright (c) 2018-2022 by Robin von den Bergen
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0.0
*
* @link https://github.com/codeFareith/cf_google_authenticator
* @see https://www.fareith.de
* @see https://typo3.org
*/
namespace CodeFareith\CfGoogleAuthenticator\Domain\Immutable;
use InvalidArgumentException;
use function http_build_query;
use function rawurldecode;
use function rawurlencode;
use function strpos;
use function vsprintf;
/**
* Immutable authentication secret
*
* This class represents an immutable authentication secret,
* which means that an instance of this is class is not
* meant to change its properties.
* It is used by the authentication services and QR image generators
* and consists of an issuer, an account name and a secret key.
*
* @see \CodeFareith\CfGoogleAuthenticator\Service\GoogleAuthenticatorService
* @see \CodeFareith\CfGoogleAuthenticator\Service\QrCodeGeneratorInterface
*
* @package CodeFareith\CfGoogleAuthenticator\Domain\Immutable
* @since 1.0.0
*/
class AuthenticationSecret
implements ImmutableInterface
{
/*─────────────────────────────────────────────────────────────────────────────*\
Constants
\*─────────────────────────────────────────────────────────────────────────────*/
public const BASE_URL = 'otpauth://totp/';
/*─────────────────────────────────────────────────────────────────────────────*\
Properties
\*─────────────────────────────────────────────────────────────────────────────*/
/**
* @var string
*/
protected $issuer;
/**
* @var string
*/
protected $accountName;
/**
* @var string
*/
protected $secretKey;
/**
* @var string
*/
protected $uri;
/**
* @var string
*/
protected $label;
/*─────────────────────────────────────────────────────────────────────────────*\
Methods
\*─────────────────────────────────────────────────────────────────────────────*/
/**
* @throws InvalidArgumentException
*/
public function __construct(string $issuer, string $accountName, string $secretKey)
{
if (strpos($issuer . $accountName, ':') !== false) {
throw new InvalidArgumentException(
'Neither the \'issuer\' parameter nor the \'accountName\' parameter may contain a colon.'
);
}
$this->issuer = $issuer;
$this->accountName = $accountName;
$this->secretKey = $secretKey;
}
/**
* @throws InvalidArgumentException
*/
public static function create(string $issuer, string $accountName, string $secretKey): self
{
return new self($issuer, $accountName, $secretKey);
}
public function getUri(): string
{
if ($this->uri === null) {
$params = [
'secret' => $this->getSecretKey(),
'issuer' => rawurlencode($this->getIssuer()),
];
$query = http_build_query($params);
$queryDecoded = rawurldecode($query);
$this->uri = vsprintf(
'%s%s?%s',
[
self::BASE_URL,
rawurlencode($this->getLabel()),
$queryDecoded,
]
);
}
return $this->uri;
}
public function getLabel(): string
{
if ($this->label === null) {
$this->label = vsprintf(
'%s:%s',
[
$this->getIssuer(),
$this->getAccountName(),
]
);
}
return $this->label;
}
public function getIssuer(): string
{
return $this->issuer;
}
public function getAccountName(): string
{
return $this->accountName;
}
public function getSecretKey(): string
{
return $this->secretKey;
}
}