66 * @author smiley <smiley@chillerlan.net>
77 * @copyright 2019 smiley
88 * @license MIT
9+ *
10+ * @see https://github.com/phan/phan/issues/5491
11+ * @phan-file-suppress PhanUnreferencedUseNormal, PhanUnreferencedUseFunction, PhanPropertyHookWithDefaultValue
912 */
1013declare (strict_types=1 );
1114
1720use function strtolower ;
1821use function strtoupper ;
1922
20- /**
21- * @property int $digits
22- * @property int $period
23- * @property int $secret_length
24- * @property string $algorithm
25- * @property string $mode
26- * @property int $adjacent
27- * @property int $time_offset
28- * @property bool $useLocalTime
29- * @property bool $forceTimeRefresh
30- * @property bool $omitUriSettings
31- */
3223trait AuthenticatorOptionsTrait{
3324
3425 /**
3526 * Code length: either 6 or 8
3627 */
37- protected int $ digits = 6 ;
28+ public int $ digits = 6 {
29+ set{
30+
31+ if (!in_array ($ value , [6 , 8 ], true )){
32+ throw new InvalidArgumentException ('Invalid code length: ' .$ value );
33+ }
34+
35+ $ this ->digits = $ value ;
36+ }
37+ }
3838
3939 /**
4040 * Validation period (seconds): 15 - 60
4141 */
42- protected int $ period = 30 ;
42+ public int $ period = 30 {
43+ set{
44+
45+ if ($ value < 15 || $ value > 60 ){
46+ throw new InvalidArgumentException ('Invalid period: ' .$ value );
47+ }
48+
49+ $ this ->period = $ value ;
50+ }
51+ }
4352
4453 /**
4554 * Length of the secret phrase (bytes, unencoded binary)
4655 *
4756 * @see \random_bytes()
4857 */
49- protected int $ secret_length = 20 ;
58+ public int $ secret_length = 20 {
59+ set{
60+
61+ if ($ value < 16 || $ value > 1024 ){
62+ throw new InvalidArgumentException ('Invalid secret length: ' .$ value );
63+ }
64+
65+ $ this ->secret_length = $ value ;
66+ }
67+ }
5068
5169 /**
5270 * Hash algorithm:
@@ -55,7 +73,17 @@ trait AuthenticatorOptionsTrait{
5573 * - `AuthenticatorInterface::ALGO_SHA256`
5674 * - `AuthenticatorInterface::ALGO_SHA512`
5775 */
58- protected string $ algorithm = AuthenticatorInterface::ALGO_SHA1 ;
76+ public string $ algorithm = AuthenticatorInterface::ALGO_SHA1 {
77+ set{
78+ $ value = strtoupper ($ value );
79+
80+ if (!in_array ($ value , AuthenticatorInterface::HASH_ALGOS , true )){
81+ throw new InvalidArgumentException ('Invalid algorithm: ' .$ value );
82+ }
83+
84+ $ this ->algorithm = $ value ;
85+ }
86+ }
5987
6088 /**
6189 * Authenticator mode:
@@ -64,19 +92,38 @@ trait AuthenticatorOptionsTrait{
6492 * - `AuthenticatorInterface::TOTP` = time based
6593 * - `AuthenticatorInterface::STEAM` = time based (Steam Guard)
6694 */
67- protected string $ mode = AuthenticatorInterface::TOTP ;
95+ public string $ mode = AuthenticatorInterface::TOTP {
96+ set{
97+ $ value = strtolower ($ value );
98+
99+ if (!isset (AuthenticatorInterface::MODES [$ value ])){
100+ throw new InvalidArgumentException ('Invalid mode: ' .$ value );
101+ }
102+
103+ $ this ->mode = $ value ;
104+ }
105+ }
68106
69107 /**
70108 * Number of allowed adjacent codes
71109 */
72- protected int $ adjacent = 1 ;
110+ public int $ adjacent = 1 {
111+ set{
112+ // limit to a sane amount
113+ if ($ value < 0 || $ value > 20 ){
114+ throw new InvalidArgumentException ('Invalid number of adjacent codes: ' .$ value );
115+ }
116+
117+ $ this ->adjacent = $ value ;
118+ }
119+ }
73120
74121 /**
75122 * A fixed time offset that will be added to the current time value
76123 *
77124 * @see \chillerlan\Authenticator\Authenticators\AuthenticatorInterface::getCounter()
78125 */
79- protected int $ time_offset = 0 ;
126+ public int $ time_offset = 0 ;
80127
81128 /**
82129 * Whether to use local time or request server time from the API
@@ -85,102 +132,18 @@ trait AuthenticatorOptionsTrait{
85132 *
86133 * note: API requests needs ext-curl installed
87134 */
88- protected bool $ useLocalTime = true ;
135+ public bool $ useLocalTime = true ;
89136
90137 /**
91138 * Whether to force refreshing server time on each call or use the time returned from the last request
92139 */
93- protected bool $ forceTimeRefresh = false ;
140+ public bool $ forceTimeRefresh = false ;
94141
95142 /**
96143 * Whether to omit the additional settings in the URI for an authenticator app (algo, digits, period)
97144 *
98145 * @link https://github.com/google/google-authenticator/wiki/Key-Uri-Format#parameters
99146 */
100- protected bool $ omitUriSettings = true ;
101-
102- /**
103- * Sets the code length to either 6 or 8
104- *
105- * @throws \InvalidArgumentException
106- */
107- protected function set_digits (int $ digits ):void {
108-
109- if (!in_array ($ digits , [6 , 8 ], true )){
110- throw new InvalidArgumentException ('Invalid code length: ' .$ digits );
111- }
112-
113- $ this ->digits = $ digits ;
114- }
115-
116- /**
117- * Sets the period to a value between 15 and 60 seconds
118- *
119- * @throws \InvalidArgumentException
120- */
121- protected function set_period (int $ period ):void {
122-
123- if ($ period < 15 || $ period > 60 ){
124- throw new InvalidArgumentException ('Invalid period: ' .$ period );
125- }
126-
127- $ this ->period = $ period ;
128- }
129-
130- /**
131- * Sets the hash algorithm
132- *
133- * @throws \InvalidArgumentException
134- */
135- protected function set_algorithm (string $ algorithm ):void {
136- $ algorithm = strtoupper ($ algorithm );
137-
138- if (!in_array ($ algorithm , AuthenticatorInterface::HASH_ALGOS , true )){
139- throw new InvalidArgumentException ('Invalid algorithm: ' .$ algorithm );
140- }
141-
142- $ this ->algorithm = $ algorithm ;
143- }
144-
145- /**
146- * Sets the authenticator mode
147- *
148- * @throws \InvalidArgumentException
149- */
150- protected function set_mode (string $ mode ):void {
151- $ mode = strtolower ($ mode );
152-
153- if (!isset (AuthenticatorInterface::MODES [$ mode ])){
154- throw new InvalidArgumentException ('Invalid mode: ' .$ mode );
155- }
156-
157- $ this ->mode = $ mode ;
158- }
159-
160- /**
161- * Sets the adjacent amount
162- *
163- * @throws \InvalidArgumentException
164- */
165- protected function set_adjacent (int $ adjacent ):void {
166- // limit to a sane amount
167- if ($ adjacent < 0 || $ adjacent > 20 ){
168- throw new InvalidArgumentException ('Invalid number of adjacent codes: ' .$ adjacent );
169- }
170-
171- $ this ->adjacent = $ adjacent ;
172- }
173-
174- /**
175- * @throws \InvalidArgumentException
176- */
177- protected function set_secret_length (int $ secret_length ):void {
178-
179- if ($ secret_length < 16 || $ secret_length > 1024 ){
180- throw new InvalidArgumentException ('Invalid secret length: ' .$ secret_length );
181- }
182-
183- $ this ->secret_length = $ secret_length ;
184- }
147+ public bool $ omitUriSettings = true ;
185148
186149}
0 commit comments