Skip to content

Commit ac8f076

Browse files
committed
Added two factor auth verification
1 parent 5f8f655 commit ac8f076

File tree

5 files changed

+91
-31
lines changed

5 files changed

+91
-31
lines changed

format.json renamed to format/login.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"displayName": "John Smith",
66
"userType": 1,
77
"token": "0037SCRgGuX0D5CLlAIk5fwg7raV3xF3Sw97sYLr",
8+
"sessionId": 123,
89
"refresh": "0XWV6Xdw4EdNAgwZ8srJuLo7lo7Lz4l5qhsM1r7P",
910
"expiry": "1444897254",
1011
"authIp": "127.0.0.1",

format/twofactor.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"token": "2FaverificationToken",
3+
"verify": "md5: userFid + secret + token + sessionId"
4+
}

src/AbstractCookieReader.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Fortifi\Login;
3+
4+
abstract class AbstractCookieReader
5+
{
6+
protected $_cookie;
7+
8+
/**
9+
* @return string
10+
*
11+
* @throws \Exception
12+
*/
13+
public static function cookieName()
14+
{
15+
throw new \Exception("Cookie name must be used");
16+
}
17+
18+
public function __construct($cookie = null)
19+
{
20+
if(!empty($cookie))
21+
{
22+
$this->_cookie = json_decode(base64_decode(rawurldecode($cookie)));
23+
}
24+
}
25+
26+
public static function fromGlobals()
27+
{
28+
$cookie = null;
29+
if(isset($_COOKIE[static::cookieName()]))
30+
{
31+
$cookie = $_COOKIE[static::cookieName()];
32+
}
33+
return new static($cookie);
34+
}
35+
36+
protected function _property($property, $default = null)
37+
{
38+
return isset($this->_cookie[$property]) ?
39+
$this->_cookie[$property] : $default;
40+
}
41+
42+
public function isPresent()
43+
{
44+
return $this->_cookie !== null && array_key_exists('token', $this->_cookie);
45+
}
46+
}

src/LoginCookie.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
11
<?php
22
namespace Fortifi\Login;
33

4-
class LoginCookie
4+
class LoginCookie extends AbstractCookieReader
55
{
6-
const COOKIE_NAME = 'FRTLGN';
7-
8-
protected $_cookie;
9-
10-
public function __construct($cookie = null)
11-
{
12-
if(!empty($cookie))
13-
{
14-
$this->_cookie = json_decode(base64_decode(rawurldecode($cookie)));
15-
}
16-
}
17-
18-
public static function fromGlobals()
19-
{
20-
$cookie = null;
21-
if(isset($_COOKIE[static::COOKIE_NAME]))
22-
{
23-
$cookie = $_COOKIE[static::COOKIE_NAME];
24-
}
25-
return new static($cookie);
26-
}
27-
28-
public function isPresent()
6+
/**
7+
* @return string
8+
*/
9+
public static function cookieName()
2910
{
30-
return $this->_cookie !== null && array_key_exists('token', $this->_cookie);
31-
}
32-
33-
protected function _property($property, $default = null)
34-
{
35-
return isset($this->_cookie[$property]) ?
36-
$this->_cookie[$property] : $default;
11+
return 'FRTLGN';
3712
}
3813

3914
public function getCustomerFid($default = null)
@@ -81,6 +56,11 @@ public function isExpired()
8156
return $this->getTokenExpiry() < time();
8257
}
8358

59+
public function getSessionId()
60+
{
61+
return $this->_property('sessionId');
62+
}
63+
8464
public function getAuthedIp()
8565
{
8666
return $this->_property('authIp');

src/TwoFactorCookie.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Fortifi\Login;
3+
4+
class TwoFactorCookie extends AbstractCookieReader
5+
{
6+
/**
7+
* @return string
8+
*/
9+
public static function cookieName()
10+
{
11+
return 'FRT2FA';
12+
}
13+
14+
public function get2FaToken()
15+
{
16+
return $this->_property('token');
17+
}
18+
19+
public function verifyCookie($secret, LoginCookie $login)
20+
{
21+
$check = md5(
22+
$login->getUserFid() .
23+
$secret .
24+
$this->_property('token') .
25+
$login->getSessionId()
26+
);
27+
return $check === $this->_property('verify');
28+
}
29+
}

0 commit comments

Comments
 (0)