Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
fusiondirectory
otphp
Commits
26d19c0b
Unverified
Commit
26d19c0b
authored
7 years ago
by
Spomky
Committed by
GitHub
7 years ago
Browse files
Options
Download
Patches
Plain Diff
Catch exception during Base32 decoding process (#96)
* Catch exception during Base32 decoding process * Tests added
parent
84d0ac40
11.2.x
10.0.x
11.0.x
11.1.x
9.0.x
dependabot/composer/phpunit/phpunit-tw-9.5.26or-tw-10.0.0
11.1.0
v11.0.2
v11.0.1
v11.0.0
v10.0.3
v10.0.2
v10.0.1
v10.0.0
v10.0
v9.1.4
v9.1.3
v9.1.2
v9.1.1
v9.1.0
v9.0.3
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/HOTP.php
+2
-2
src/HOTP.php
src/OTP.php
+5
-1
src/OTP.php
src/TOTP.php
+2
-2
src/TOTP.php
tests/HOTPTest.php
+12
-0
tests/HOTPTest.php
tests/TOTPTest.php
+12
-0
tests/TOTPTest.php
with
33 additions
and
5 deletions
+33
-5
src/HOTP.php
+
2
−
2
View file @
26d19c0b
...
...
@@ -39,7 +39,7 @@ final class HOTP extends OTP implements HOTPInterface
*
* @return self
*/
public
static
function
create
(
?string
$secret
=
null
,
int
$counter
=
0
,
string
$digest
=
'sha1'
,
int
$digits
=
6
):
HOTP
public
static
function
create
(
?string
$secret
=
null
,
int
$counter
=
0
,
string
$digest
=
'sha1'
,
int
$digits
=
6
):
self
{
return
new
self
(
$secret
,
$counter
,
$digest
,
$digits
);
}
...
...
@@ -119,7 +119,7 @@ final class HOTP extends OTP implements HOTPInterface
{
$window
=
$this
->
getWindow
(
$window
);
for
(
$i
=
$counter
;
$i
<=
$counter
+
$window
;
++
$i
)
{
for
(
$i
=
$counter
;
$i
<=
$counter
+
$window
;
$i
++
)
{
if
(
$this
->
compareOTP
(
$this
->
at
(
$i
),
$otp
))
{
$this
->
updateCounter
(
$i
+
1
);
...
...
This diff is collapsed.
Click to expand it.
src/OTP.php
+
5
−
1
View file @
26d19c0b
...
...
@@ -108,7 +108,11 @@ abstract class OTP implements OTPInterface
*/
private
function
getDecodedSecret
():
string
{
$secret
=
Base32
::
decodeUpper
(
$this
->
getSecret
());
try
{
$secret
=
Base32
::
decodeUpper
(
$this
->
getSecret
());
}
catch
(
\
Exception
$e
)
{
throw
new
\
RuntimeException
(
'Unable to decode the secret. Is it correctly base32 encoded?'
);
}
return
$secret
;
}
...
...
This diff is collapsed.
Click to expand it.
src/TOTP.php
+
2
−
2
View file @
26d19c0b
...
...
@@ -41,7 +41,7 @@ final class TOTP extends OTP implements TOTPInterface
*
* @return self
*/
public
static
function
create
(
?string
$secret
=
null
,
int
$period
=
30
,
string
$digest
=
'sha1'
,
int
$digits
=
6
):
TOTP
public
static
function
create
(
?string
$secret
=
null
,
int
$period
=
30
,
string
$digest
=
'sha1'
,
int
$digits
=
6
):
self
{
return
new
self
(
$secret
,
$period
,
$digest
,
$digits
);
}
...
...
@@ -104,7 +104,7 @@ final class TOTP extends OTP implements TOTPInterface
{
$window
=
abs
(
$window
);
for
(
$i
=
-
$window
;
$i
<=
$window
;
++
$i
)
{
for
(
$i
=
-
$window
;
$i
<=
$window
;
$i
++
)
{
$at
=
(
int
)
$i
*
$this
->
getPeriod
()
+
$timestamp
;
if
(
$this
->
compareOTP
(
$this
->
at
(
$at
),
$otp
))
{
return
true
;
...
...
This diff is collapsed.
Click to expand it.
tests/HOTPTest.php
+
12
−
0
View file @
26d19c0b
...
...
@@ -100,6 +100,18 @@ final class HOTPTest extends TestCase
HOTP
::
create
(
'JDDK4U6G3BJLEZ7Y'
,
0
,
'foo'
);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to decode the secret. Is it correctly base32 encoded?
*/
public
function
testSecretShouldBeBase32Encoded
()
{
$secret
=
random_bytes
(
32
);
$otp
=
HOTP
::
create
(
$secret
);
$otp
->
at
(
0
);
}
public
function
testObjectCreationValid
()
{
$otp
=
HOTP
::
create
();
...
...
This diff is collapsed.
Click to expand it.
tests/TOTPTest.php
+
12
−
0
View file @
26d19c0b
...
...
@@ -56,6 +56,18 @@ final class TOTPTest extends TestCase
TOTP
::
create
(
'JDDK4U6G3BJLEZ7Y'
,
-
20
,
'sha512'
,
8
);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to decode the secret. Is it correctly base32 encoded?
*/
public
function
testSecretShouldBeBase32Encoded
()
{
$secret
=
random_bytes
(
32
);
$otp
=
TOTP
::
create
(
$secret
);
$otp
->
now
();
}
public
function
testGetProvisioningUri
()
{
$otp
=
$this
->
createTOTP
(
6
,
'sha1'
,
30
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets