[Web] Fix log line handling
[Web] Add mailcow UI logs [Web] Changes to _SESSION['return'] logic and logger (more to come) [Web] Show last login [Web, Postfix] Allow to disable sender check completely [Web] Many minor fixes [Web] Update some libs
This commit is contained in:
22
data/web/inc/lib/composer.lock
generated
22
data/web/inc/lib/composer.lock
generated
@@ -1,23 +1,23 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "3edeec2e3fa875d4f9d5e7f22a8179be",
|
||||
"packages": [
|
||||
{
|
||||
"name": "php-mime-mail-parser/php-mime-mail-parser",
|
||||
"version": "2.9.5",
|
||||
"version": "2.11.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-mime-mail-parser/php-mime-mail-parser.git",
|
||||
"reference": "fbb424e77de2837dc6d8a110656d3c10f2fc9353"
|
||||
"reference": "4769e942ed0dbbdd7882fc390b119d625463c8af"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/fbb424e77de2837dc6d8a110656d3c10f2fc9353",
|
||||
"reference": "fbb424e77de2837dc6d8a110656d3c10f2fc9353",
|
||||
"url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/4769e942ed0dbbdd7882fc390b119d625463c8af",
|
||||
"reference": "4769e942ed0dbbdd7882fc390b119d625463c8af",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -84,7 +84,7 @@
|
||||
"mailparse",
|
||||
"mime"
|
||||
],
|
||||
"time": "2018-01-16T18:38:20+00:00"
|
||||
"time": "2018-04-30T05:55:59+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpmailer/phpmailer",
|
||||
@@ -165,16 +165,16 @@
|
||||
},
|
||||
{
|
||||
"name": "robthree/twofactorauth",
|
||||
"version": "1.6.1",
|
||||
"version": "1.6.5",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobThree/TwoFactorAuth.git",
|
||||
"reference": "a77e7d822343bb88112baef808839cfae7bc5abb"
|
||||
"reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/a77e7d822343bb88112baef808839cfae7bc5abb",
|
||||
"reference": "a77e7d822343bb88112baef808839cfae7bc5abb",
|
||||
"url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/f5f58a4c62d0336a0e6175856894a51f3565dad2",
|
||||
"reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -212,7 +212,7 @@
|
||||
"php",
|
||||
"tfa"
|
||||
],
|
||||
"time": "2017-11-06T17:55:56+00:00"
|
||||
"time": "2018-06-09T10:09:59+00:00"
|
||||
},
|
||||
{
|
||||
"name": "soundasleep/html2text",
|
||||
|
24
data/web/inc/lib/vendor/composer/installed.json
vendored
24
data/web/inc/lib/vendor/composer/installed.json
vendored
@@ -1,17 +1,17 @@
|
||||
[
|
||||
{
|
||||
"name": "php-mime-mail-parser/php-mime-mail-parser",
|
||||
"version": "2.9.5",
|
||||
"version_normalized": "2.9.5.0",
|
||||
"version": "2.11.1",
|
||||
"version_normalized": "2.11.1.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-mime-mail-parser/php-mime-mail-parser.git",
|
||||
"reference": "fbb424e77de2837dc6d8a110656d3c10f2fc9353"
|
||||
"reference": "4769e942ed0dbbdd7882fc390b119d625463c8af"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/fbb424e77de2837dc6d8a110656d3c10f2fc9353",
|
||||
"reference": "fbb424e77de2837dc6d8a110656d3c10f2fc9353",
|
||||
"url": "https://api.github.com/repos/php-mime-mail-parser/php-mime-mail-parser/zipball/4769e942ed0dbbdd7882fc390b119d625463c8af",
|
||||
"reference": "4769e942ed0dbbdd7882fc390b119d625463c8af",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -28,7 +28,7 @@
|
||||
"satooshi/php-coveralls": "0.*",
|
||||
"squizlabs/php_codesniffer": "2.*"
|
||||
},
|
||||
"time": "2018-01-16T18:38:20+00:00",
|
||||
"time": "2018-04-30T05:55:59+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
@@ -162,17 +162,17 @@
|
||||
},
|
||||
{
|
||||
"name": "robthree/twofactorauth",
|
||||
"version": "1.6.1",
|
||||
"version_normalized": "1.6.1.0",
|
||||
"version": "1.6.5",
|
||||
"version_normalized": "1.6.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobThree/TwoFactorAuth.git",
|
||||
"reference": "a77e7d822343bb88112baef808839cfae7bc5abb"
|
||||
"reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/a77e7d822343bb88112baef808839cfae7bc5abb",
|
||||
"reference": "a77e7d822343bb88112baef808839cfae7bc5abb",
|
||||
"url": "https://api.github.com/repos/RobThree/TwoFactorAuth/zipball/f5f58a4c62d0336a0e6175856894a51f3565dad2",
|
||||
"reference": "f5f58a4c62d0336a0e6175856894a51f3565dad2",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -181,7 +181,7 @@
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "@stable"
|
||||
},
|
||||
"time": "2017-11-06T17:55:56+00:00",
|
||||
"time": "2018-06-09T10:09:59+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
|
@@ -98,10 +98,10 @@ $Parser->setStream(fopen("php://stdin", "r"));
|
||||
|
||||
// Once we've indicated where to find the mail, we can parse out the data
|
||||
$to = $Parser->getHeader('to'); // "test" <test@example.com>, "test2" <test2@example.com>
|
||||
$addressesTo = $Parser->getAddresses('to'); //Return an array : [[test, test@example.com, false],[test2, test2@example.com, false]]
|
||||
$addressesTo = $Parser->getAddresses('to'); //Return an array : [["display"=>"test", "address"=>"test@example.com", false],["display"=>"test2", "address"=>"test2@example.com", false]]
|
||||
|
||||
$from = $Parser->getHeader('from'); // "test" <test@example.com>
|
||||
$addressesFrom = $Parser->getAddresses('from'); //Return an array : test, test@example.com, false
|
||||
$addressesFrom = $Parser->getAddresses('from'); //Return an array : [["display"=>"test", "address"=>"test@example.com", "is_group"=>false]]
|
||||
|
||||
$subject = $Parser->getHeader('subject');
|
||||
|
||||
|
23
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/Contracts/Middleware.php
vendored
Normal file
23
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/Contracts/Middleware.php
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace PhpMimeMailParser\Contracts;
|
||||
|
||||
use PhpMimeMailParser\MimePart;
|
||||
use PhpMimeMailParser\MiddlewareStack;
|
||||
|
||||
/**
|
||||
* Process Mime parts by either:
|
||||
* processing the part or calling the $next MiddlewareStack
|
||||
*/
|
||||
interface Middleware
|
||||
{
|
||||
/**
|
||||
* Process a mime part, optionally delegating parsing to the $next MiddlewareStack
|
||||
*
|
||||
* @param MimePart $part
|
||||
* @param MiddlewareStack $next
|
||||
*
|
||||
* @return MimePart
|
||||
*/
|
||||
public function parse(MimePart $part, MiddlewareStack $next);
|
||||
}
|
27
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/Middleware.php
vendored
Normal file
27
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/Middleware.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace PhpMimeMailParser;
|
||||
|
||||
/**
|
||||
* Wraps a callable as a Middleware
|
||||
*/
|
||||
class Middleware implements Contracts\Middleware
|
||||
{
|
||||
/**
|
||||
* Create a middleware using a callable $fn
|
||||
*
|
||||
* @param callable $fn
|
||||
*/
|
||||
public function __construct(callable $fn)
|
||||
{
|
||||
$this->parser = $fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a mime part, optionally delegating parsing to the $next MiddlewareStack
|
||||
*/
|
||||
public function parse(MimePart $part, MiddlewareStack $next)
|
||||
{
|
||||
return call_user_func($this->parser, $part, $next);
|
||||
}
|
||||
}
|
89
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/MiddlewareStack.php
vendored
Normal file
89
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/MiddlewareStack.php
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace PhpMimeMailParser;
|
||||
|
||||
use PhpMimeMailParser\Contracts\MiddleWare;
|
||||
|
||||
/**
|
||||
* A stack of middleware chained together by (MiddlewareStack $next)
|
||||
*/
|
||||
class MiddlewareStack
|
||||
{
|
||||
/**
|
||||
* Next MiddlewareStack in chain
|
||||
*
|
||||
* @var MiddlewareStack
|
||||
*/
|
||||
protected $next;
|
||||
|
||||
/**
|
||||
* Middleware in this MiddlewareStack
|
||||
*
|
||||
* @var Middleware
|
||||
*/
|
||||
protected $middleware;
|
||||
|
||||
/**
|
||||
* Construct the first middleware in this MiddlewareStack
|
||||
* The next middleware is chained through $MiddlewareStack->add($Middleware)
|
||||
*
|
||||
* @param Middleware $middleware
|
||||
*/
|
||||
public function __construct(Middleware $middleware = null)
|
||||
{
|
||||
$this->middleware = $middleware;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a chained middleware in MiddlewareStack
|
||||
*
|
||||
* @param Middleware $middleware
|
||||
* @return MiddlewareStack Immutable MiddlewareStack
|
||||
*/
|
||||
public function add(Middleware $middleware)
|
||||
{
|
||||
$stack = new static($middleware);
|
||||
$stack->next = $this;
|
||||
return $stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the MimePart by passing it through the Middleware
|
||||
* @param MimePart $part
|
||||
* @return MimePart
|
||||
*/
|
||||
public function parse(MimePart $part)
|
||||
{
|
||||
if (!$this->middleware) {
|
||||
return $part;
|
||||
}
|
||||
$part = call_user_func(array($this->middleware, 'parse'), $part, $this->next);
|
||||
return $part;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MiddlewareStack based on an array of middleware
|
||||
*
|
||||
* @param Middleware[] $middlewares
|
||||
* @return MiddlewareStack
|
||||
*/
|
||||
public static function factory(array $middlewares = array())
|
||||
{
|
||||
$stack = new static;
|
||||
foreach ($middlewares as $middleware) {
|
||||
$stack = $stack->add($middleware);
|
||||
}
|
||||
return $stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow calling MiddlewareStack instance directly to invoke parse()
|
||||
*
|
||||
* @param MimePart $part
|
||||
* @return MimePart
|
||||
*/
|
||||
public function __invoke(MimePart $part)
|
||||
{
|
||||
return $this->parse($part);
|
||||
}
|
||||
}
|
115
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/MimePart.php
vendored
Normal file
115
data/web/inc/lib/vendor/php-mime-mail-parser/php-mime-mail-parser/src/MimePart.php
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace PhpMimeMailParser;
|
||||
|
||||
/**
|
||||
* Mime Part
|
||||
* Represents the results of mailparse_msg_get_part_data()
|
||||
*
|
||||
* Note ArrayAccess::offsetSet() cannot modify deeply nestated arrays.
|
||||
* When modifying use getPart() and setPart() for deep nested data modification
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* $MimePart['headers']['from'] = 'modified@example.com' // fails
|
||||
*
|
||||
* // correct
|
||||
* $part = $MimePart->getPart();
|
||||
* $part['headers']['from'] = 'modified@example.com';
|
||||
* $MimePart->setPart($part);
|
||||
*/
|
||||
class MimePart implements \ArrayAccess
|
||||
{
|
||||
/**
|
||||
* Internal mime part
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $part = array();
|
||||
|
||||
/**
|
||||
* Immutable Part Id
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* Create a mime part
|
||||
*
|
||||
* @param array $part
|
||||
* @param string $id
|
||||
*/
|
||||
public function __construct($id, array $part)
|
||||
{
|
||||
$this->part = $part;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the part Id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the part data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPart()
|
||||
{
|
||||
return $this->part;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the mime part data
|
||||
*
|
||||
* @param array $part
|
||||
* @return void
|
||||
*/
|
||||
public function setPart(array $part)
|
||||
{
|
||||
$this->part = $part;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (is_null($offset)) {
|
||||
$this->part[] = $value;
|
||||
} else {
|
||||
$this->part[$offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->part[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->part[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->part[$offset]) ? $this->part[$offset] : null;
|
||||
}
|
||||
}
|
@@ -62,6 +62,13 @@ class Parser
|
||||
'x+b', 'c+b', 'rt', 'r+t', 'w+t', 'a+t', 'x+t', 'c+t'
|
||||
];
|
||||
|
||||
/**
|
||||
* Stack of middleware registered to process data
|
||||
*
|
||||
* @var MiddlewareStack
|
||||
*/
|
||||
protected $middlewareStack;
|
||||
|
||||
/**
|
||||
* Parser constructor.
|
||||
*
|
||||
@@ -74,6 +81,7 @@ class Parser
|
||||
}
|
||||
|
||||
$this->charset = $charset;
|
||||
$this->middlewareStack = new MiddlewareStack();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +193,10 @@ class Parser
|
||||
$this->parts = [];
|
||||
foreach ($structure as $part_id) {
|
||||
$part = mailparse_msg_get_part($this->resource, $part_id);
|
||||
$this->parts[$part_id] = mailparse_msg_get_part_data($part);
|
||||
$part_data = mailparse_msg_get_part_data($part);
|
||||
$mimePart = new MimePart($part_id, $part_data);
|
||||
// let each middleware parse the part before saving
|
||||
$this->parts[$part_id] = $this->middlewareStack->parse($mimePart)->getPart();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +313,7 @@ class Parser
|
||||
$start = $part['starting-pos'];
|
||||
$end = $part['starting-pos-body'];
|
||||
fseek($this->stream, $start, SEEK_SET);
|
||||
$header = fread($this->stream, $end-$start);
|
||||
$header = fread($this->stream, $end - $start);
|
||||
return $header;
|
||||
}
|
||||
|
||||
@@ -316,7 +327,7 @@ class Parser
|
||||
{
|
||||
$start = $part['starting-pos'];
|
||||
$end = $part['starting-pos-body'];
|
||||
$header = substr($this->data, $start, $end-$start);
|
||||
$header = substr($this->data, $start, $end - $start);
|
||||
return $header;
|
||||
}
|
||||
|
||||
@@ -357,12 +368,11 @@ class Parser
|
||||
*
|
||||
* @param string $type text, html or htmlEmbedded
|
||||
*
|
||||
* @return false|string Body or False if not found
|
||||
* @return string Body
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getMessageBody($type = 'text')
|
||||
{
|
||||
$body = false;
|
||||
$mime_types = [
|
||||
'text' => 'text/plain',
|
||||
'html' => 'text/html',
|
||||
@@ -370,7 +380,7 @@ class Parser
|
||||
];
|
||||
|
||||
if (in_array($type, array_keys($mime_types))) {
|
||||
$part_type = $type === 'htmlEmbedded' ? 'html' : $type;
|
||||
$part_type = $type === 'htmlEmbedded' ? 'html' : $type;
|
||||
$inline_parts = $this->getInlineParts($part_type);
|
||||
$body = empty($inline_parts) ? '' : $inline_parts[0];
|
||||
} else {
|
||||
@@ -425,9 +435,13 @@ class Parser
|
||||
*/
|
||||
public function getAddresses($name)
|
||||
{
|
||||
$value = $this->getHeader($name);
|
||||
|
||||
return mailparse_rfc822_parse_addresses($value);
|
||||
$value = $this->getRawHeader($name);
|
||||
$value = (is_array($value)) ? $value[0] : $value;
|
||||
$addresses = mailparse_rfc822_parse_addresses($value);
|
||||
foreach ($addresses as $i => $item) {
|
||||
$addresses[$i]['display'] = $this->decodeHeader($item['display']);
|
||||
}
|
||||
return $addresses;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -438,7 +452,6 @@ class Parser
|
||||
public function getInlineParts($type = 'text')
|
||||
{
|
||||
$inline_parts = [];
|
||||
$dispositions = ['inline'];
|
||||
$mime_types = [
|
||||
'text' => 'text/plain',
|
||||
'html' => 'text/html',
|
||||
@@ -456,9 +469,6 @@ class Parser
|
||||
$headers = $this->getPart('headers', $part);
|
||||
$encodingType = array_key_exists('content-transfer-encoding', $headers) ?
|
||||
$headers['content-transfer-encoding'] : '';
|
||||
if (is_array($encodingType)) {
|
||||
$encodingType = $encodingType[0];
|
||||
}
|
||||
$undecoded_body = $this->decodeContentTransfer($this->getPartBody($part), $encodingType);
|
||||
$inline_parts[] = $this->charset->decodeCharset($undecoded_body, $this->getPartCharset($part));
|
||||
}
|
||||
@@ -475,9 +485,7 @@ class Parser
|
||||
public function getAttachments($include_inline = true)
|
||||
{
|
||||
$attachments = [];
|
||||
$dispositions = $include_inline ?
|
||||
['attachment', 'inline'] :
|
||||
['attachment'];
|
||||
$dispositions = $include_inline ? ['attachment', 'inline'] : ['attachment'];
|
||||
$non_attachment_types = ['text/plain', 'text/html'];
|
||||
$nonameIter = 0;
|
||||
|
||||
@@ -504,6 +512,9 @@ class Parser
|
||||
// if we cannot get it by getMessageBody(), we assume it is an attachment
|
||||
$disposition = 'attachment';
|
||||
}
|
||||
if (in_array($disposition, ['attachment', 'inline']) === false && !empty($disposition)) {
|
||||
$disposition = 'attachment';
|
||||
}
|
||||
|
||||
if (in_array($disposition, $dispositions) === true) {
|
||||
if ($filename == 'noname') {
|
||||
@@ -514,12 +525,13 @@ class Parser
|
||||
$headersAttachments = $this->getPart('headers', $part);
|
||||
$contentidAttachments = $this->getPart('content-id', $part);
|
||||
|
||||
$attachmentStream = $this->getAttachmentStream($part);
|
||||
$mimePartStr = $this->getPartComplete($part);
|
||||
|
||||
$attachments[] = new Attachment(
|
||||
$filename,
|
||||
$this->getPart('content-type', $part),
|
||||
$this->getAttachmentStream($part),
|
||||
$attachmentStream,
|
||||
$disposition,
|
||||
$contentidAttachments,
|
||||
$headersAttachments,
|
||||
@@ -564,7 +576,7 @@ class Parser
|
||||
break;
|
||||
case self::ATTACHMENT_DUPLICATE_THROW:
|
||||
case self::ATTACHMENT_DUPLICATE_SUFFIX:
|
||||
$attachment_path = $attach_dir . $attachment->getFilename();
|
||||
$attachment_path = $attach_dir.$attachment->getFilename();
|
||||
break;
|
||||
default:
|
||||
throw new Exception('Invalid filename strategy argument provided.');
|
||||
@@ -622,8 +634,8 @@ class Parser
|
||||
$written = 0;
|
||||
while ($written < $len) {
|
||||
$write = $len;
|
||||
$part = fread($this->stream, $write);
|
||||
fwrite($temp_fp, $this->decodeContentTransfer($part, $encodingType));
|
||||
$data = fread($this->stream, $write);
|
||||
fwrite($temp_fp, $this->decodeContentTransfer($data, $encodingType));
|
||||
$written += $write;
|
||||
}
|
||||
} elseif ($this->data) {
|
||||
@@ -650,13 +662,17 @@ class Parser
|
||||
*/
|
||||
protected function decodeContentTransfer($encodedString, $encodingType)
|
||||
{
|
||||
if (is_array($encodingType)) {
|
||||
$encodingType = $encodingType[0];
|
||||
}
|
||||
|
||||
$encodingType = strtolower($encodingType);
|
||||
if ($encodingType == 'base64') {
|
||||
return base64_decode($encodedString);
|
||||
} elseif ($encodingType == 'quoted-printable') {
|
||||
return quoted_printable_decode($encodedString);
|
||||
} else {
|
||||
return $encodedString; //8bit, 7bit, binary
|
||||
return $encodedString;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -709,7 +725,7 @@ class Parser
|
||||
}
|
||||
|
||||
$text = $this->charset->decodeCharset($text, $this->charset->getCharsetAlias($charset));
|
||||
$input = str_replace($encoded . $space, $text, $input);
|
||||
$input = str_replace($encoded.$space, $text, $input);
|
||||
}
|
||||
|
||||
return $input;
|
||||
@@ -720,14 +736,14 @@ class Parser
|
||||
*
|
||||
* @param array $part
|
||||
*
|
||||
* @return string|false
|
||||
* @return string
|
||||
*/
|
||||
protected function getPartCharset($part)
|
||||
{
|
||||
if (isset($part['charset'])) {
|
||||
return $this->charset->getCharsetAlias($part['charset']);
|
||||
} else {
|
||||
return false;
|
||||
return 'us-ascii';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -901,4 +917,28 @@ class Parser
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a middleware to the parser MiddlewareStack
|
||||
* Each middleware is invoked when:
|
||||
* a MimePart is retrieved by mailparse_msg_get_part_data() during $this->parse()
|
||||
* The middleware will receive MimePart $part and the next MiddlewareStack $next
|
||||
*
|
||||
* Eg:
|
||||
*
|
||||
* $Parser->addMiddleware(function(MimePart $part, MiddlewareStack $next) {
|
||||
* // do something with the $part
|
||||
* return $next($part);
|
||||
* });
|
||||
*
|
||||
* @param callable $middleware Plain Function or Middleware Instance to execute
|
||||
* @return void
|
||||
*/
|
||||
public function addMiddleware(callable $middleware)
|
||||
{
|
||||
if (!$middleware instanceof Middleware) {
|
||||
$middleware = new Middleware($middleware);
|
||||
}
|
||||
$this->middlewareStack = $this->middlewareStack->add($middleware);
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,15 @@
|
||||
language: php
|
||||
|
||||
dist: trusty
|
||||
matrix:
|
||||
include:
|
||||
- php: 5.3
|
||||
dist: precise
|
||||
|
||||
php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- hhvm
|
||||
- 7.2
|
||||
|
||||
before_script:
|
||||
- composer install
|
||||
|
||||
script:
|
||||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then phpunit --coverage-text tests ; fi
|
||||
- vendor/bin/phpunit --coverage-text tests
|
@@ -1,6 +1,6 @@
|
||||
#  PHP library for Two Factor Authentication
|
||||
|
||||
[](https://travis-ci.org/RobThree/TwoFactorAuth/) [](https://packagist.org/packages/robthree/twofactorauth) [](LICENSE) [](https://packagist.org/packages/robthree/twofactorauth) [](http://hhvm.h4cc.de/package/robthree/twofactorauth) [](https://codeclimate.com/github/RobThree/TwoFactorAuth) [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6MB5M2SQLP636 "Keep me off the streets")
|
||||
[](https://travis-ci.org/RobThree/TwoFactorAuth/) [](https://packagist.org/packages/robthree/twofactorauth) [](LICENSE) [](https://packagist.org/packages/robthree/twofactorauth) [](https://codeclimate.com/github/RobThree/TwoFactorAuth) [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6MB5M2SQLP636 "Keep me off the streets")
|
||||
|
||||
PHP library for [two-factor (or multi-factor) authentication](http://en.wikipedia.org/wiki/Multi-factor_authentication) using [TOTP](http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm) and [QR-codes](http://en.wikipedia.org/wiki/QR_code). Inspired by, based on but most importantly an *improvement* on '[PHPGangsta/GoogleAuthenticator](https://github.com/PHPGangsta/GoogleAuthenticator)'. There's a [.Net implementation](https://github.com/RobThree/TwoFactorAuth.Net) of this library as well.
|
||||
|
||||
@@ -10,7 +10,7 @@ PHP library for [two-factor (or multi-factor) authentication](http://en.wikipedi
|
||||
|
||||
## Requirements
|
||||
|
||||
* Tested on PHP 5.3, 5.4, 5.5 and 5.6, 7.0, 7.1 and HHVM
|
||||
* Tested on PHP 5.4 up to 7.2
|
||||
* [cURL](http://php.net/manual/en/book.curl.php) when using the provided `GoogleQRCodeProvider` (default), `QRServerProvider` or `QRicketProvider` but you can also provide your own QR-code provider.
|
||||
* [random_bytes()](http://php.net/manual/en/function.random-bytes.php), [MCrypt](http://php.net/manual/en/book.mcrypt.php), [OpenSSL](http://php.net/manual/en/book.openssl.php) or [Hash](http://php.net/manual/en/book.hash.php) depending on which built-in RNG you use (TwoFactorAuth will try to 'autodetect' and use the best available); however: feel free to provide your own (CS)RNG.
|
||||
|
||||
@@ -33,9 +33,9 @@ Here are some code snippets that should help you get started...
|
||||
$tfa = new RobThree\Auth\TwoFactorAuth('My Company');
|
||||
````
|
||||
|
||||
The TwoFactorAuth class constructor accepts 7 parameters (all optional):
|
||||
The TwoFactorAuth class constructor accepts 7 arguments (all optional):
|
||||
|
||||
Parameter | Default value | Use
|
||||
Argument | Default value | Use
|
||||
------------------|---------------|--------------------------------------------------
|
||||
`$issuer` | `null` | Will be displayed in the app as issuer name
|
||||
`$digits` | `6` | The number of digits the resulting codes will be
|
||||
@@ -45,7 +45,7 @@ Parameter | Default value | Use
|
||||
`$rngprovider` | `null` | Random Number Generator provider (more on this later)
|
||||
`$timeprovider` | `null` | Time provider (more on this later)
|
||||
|
||||
These parameters are all '`write once`'; the class will, for it's lifetime, use these values when generating / calculating codes. The number of digits, the period and algorithm are all set to values Google's Authticator app uses (and supports). You may specify `8` digits, a period of `45` seconds and the `sha256` algorithm but the authenticator app (be it Google's implementation, Authy or any other app) may or may not support these values. Your mileage may vary; keep it on the safe side if you don't control which app your audience uses.
|
||||
These arguments are all '`write once`'; the class will, for it's lifetime, use these values when generating / calculating codes. The number of digits, the period and algorithm are all set to values Google's Authticator app uses (and supports). You may specify `8` digits, a period of `45` seconds and the `sha256` algorithm but the authenticator app (be it Google's implementation, Authy or any other app) may or may not support these values. Your mileage may vary; keep it on the safe side if you don't control which app your audience uses.
|
||||
|
||||
### Step 1: Set up secret shared key
|
||||
|
||||
@@ -89,9 +89,11 @@ When the shared secret is added to the app, the app will be ready to start gener
|
||||
$result = $tfa->verifyCode($_SESSION['secret'], $_POST['verification']);
|
||||
````
|
||||
|
||||
`verifyCode()` will return either `true` (the code was valid) or `false` (the code was invalid; no points for you!). You may need to store `$secret` in a `$_SESSION` or other persistent storage between requests. The `verifyCode()` accepts, aside from `$secret` and `$code`, two more parameters. The first being `$discrepancy`. Since TOTP codes are based on time("slices") it is very important that the server (but also client) have a correct date/time. But because the two *may* differ a bit we usually allow a certain amount of leeway. Because generated codes are valid for a specific period (remember the `$period` parameter in the `TwoFactorAuth`'s constructor?) we usually check the period directly before and the period directly after the current time when validating codes. So when the current time is `14:34:21`, which results in a 'current timeslice' of `14:34:00` to `14:34:30` we also calculate/verify the codes for `14:33:30` to `14:34:00` and for `14:34:30` to `14:35:00`. This gives us a 'window' of `14:33:30` to `14:35:00`. The `$discrepancy` parameter specifies how many periods (or: timeslices) we check in either direction of the current time. The default `$discrepancy` of `1` results in (max.) 3 period checks: -1, current and +1 period. A `$discrepancy` of `4` would result in a larger window (or: bigger time difference between client and server) of -4, -3, -2, -1, current, +1, +2, +3 and +4 periods.
|
||||
`verifyCode()` will return either `true` (the code was valid) or `false` (the code was invalid; no points for you!). You may need to store `$secret` in a `$_SESSION` or other persistent storage between requests. The `verifyCode()` accepts, aside from `$secret` and `$code`, three more arguments. The first being `$discrepancy`. Since TOTP codes are based on time("slices") it is very important that the server (but also client) have a correct date/time. But because the two *may* differ a bit we usually allow a certain amount of leeway. Because generated codes are valid for a specific period (remember the `$period` argument in the `TwoFactorAuth`'s constructor?) we usually check the period directly before and the period directly after the current time when validating codes. So when the current time is `14:34:21`, which results in a 'current timeslice' of `14:34:00` to `14:34:30` we also calculate/verify the codes for `14:33:30` to `14:34:00` and for `14:34:30` to `14:35:00`. This gives us a 'window' of `14:33:30` to `14:35:00`. The `$discrepancy` argument specifies how many periods (or: timeslices) we check in either direction of the current time. The default `$discrepancy` of `1` results in (max.) 3 period checks: -1, current and +1 period. A `$discrepancy` of `4` would result in a larger window (or: bigger time difference between client and server) of -4, -3, -2, -1, current, +1, +2, +3 and +4 periods.
|
||||
|
||||
The second parameter `$time` allows you to check a code for a specific point in time. This parameter has no real practical use but can be handy for unittesting etc. The default value, `null`, means: use the current time.
|
||||
The second, `$time`, allows you to check a code for a specific point in time. This argument has no real practical use but can be handy for unittesting etc. The default value, `null`, means: use the current time.
|
||||
|
||||
The third, `$timeslice`, is an out-argument; the value returned in `$timeslice` is the value of the timeslice that matched the code (if any). This value will be 0 when the code doesn't match and non-zero when the code matches. This value can be stored with the user and can be used to prevent replay-attacks. All you need to do is, on successful login, make sure `$timeslice` is greater than the previously stored timeslice.
|
||||
|
||||
### Step 3: Store `$secret` with user and we're done!
|
||||
|
||||
@@ -117,7 +119,7 @@ public function verifyCode($secret, $code, $discrepancy = 1, $time = null): bool
|
||||
|
||||
### QR-code providers
|
||||
|
||||
As mentioned before, this library comes with three 'built-in' QR-code providers. This chapter will touch the subject a bit but most of it should be self-explanatory. The `TwoFactorAuth`-class accepts a `$qrcodeprovider` parameter which lets you specify a built-in or custom QR-code provider. All three built-in providers do a simple HTTP request to retrieve an image using cURL and implement the [`IQRCodeProvider`](lib/Providers/Qr/IQRCodeProvider.php) interface which is all you need to implement to write your own QR-code provider.
|
||||
As mentioned before, this library comes with three 'built-in' QR-code providers. This chapter will touch the subject a bit but most of it should be self-explanatory. The `TwoFactorAuth`-class accepts a `$qrcodeprovider` argument which lets you specify a built-in or custom QR-code provider. All three built-in providers do a simple HTTP request to retrieve an image using cURL and implement the [`IQRCodeProvider`](lib/Providers/Qr/IQRCodeProvider.php) interface which is all you need to implement to write your own QR-code provider.
|
||||
|
||||
The default provider is the [`GoogleQRCodeProvider`](lib/Providers/Qr/GoogleQRCodeProvider.php) which uses the [Google Chart Tools](https://developers.google.com/chart/infographics/docs/qr_codes) to render QR-codes. Then we have the [`QRServerProvider`](lib/Providers/Qr/QRServerProvider.php) which uses the [goqr.me API](http://goqr.me/api/doc/create-qr-code/) and finally we have the [`QRicketProvider`](lib/Providers/Qr/QRicketProvider.php) which uses the [QRickit API](http://qrickit.com/qrickit_apps/qrickit_api.php). All three inherit from a common (abstract) baseclass named [`BaseHTTPQRCodeProvider`](lib/Providers/Qr/BaseHTTPQRCodeProvider.php) because all three share the same functionality: retrieve an image from a 3rd party over HTTP. All three classes have constructors that allow you to tweak some settings and most, if not all, arguments should speak for themselves. If you're not sure which values are supported, click the links in this paragraph for documentation on the API's that are utilized by these classes.
|
||||
|
||||
@@ -132,7 +134,7 @@ The `getMimeType()` method should return the [MIME type](http://en.wikipedia.org
|
||||
|
||||
`otpauth://totp/LABEL:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=ISSUER`
|
||||
|
||||
All you need to do is return the QR-code as binary image data and you're done. All parts of the `$qrtext` have been escaped for you (but note: you *may* need to escape the entire `$qrtext` just once more when passing the data to another server as GET-parameter).
|
||||
All you need to do is return the QR-code as binary image data and you're done. All parts of the `$qrtext` have been escaped for you (but note: you *may* need to escape the entire `$qrtext` just once more when passing the data to another server as GET-argument).
|
||||
|
||||
Let's see if we can use [PHP QR Code](http://phpqrcode.sourceforge.net/) to implement our own, custom, no-3rd-parties-allowed-here, provider. We start with downloading the [required (single) file](https://github.com/t0k4rt/phpqrcode/blob/master/phpqrcode.php) and putting it in the directory where `TwoFactorAuth.php` is located as well. Now let's implement the provider: create another file named `myprovider.php` in the `Providers\Qr` directory and paste in this content:
|
||||
|
||||
@@ -176,15 +178,15 @@ Voilà. Couldn't make it any simpler.
|
||||
|
||||
This library also comes with three 'built-in' RNG providers ([Random Number Generator](https://en.wikipedia.org/wiki/Random_number_generation)). The RNG provider generates a number of random bytes and returns these bytes as a string. These values are then used to create the secret. By default (no RNG provider specified) TwoFactorAuth will try to determine the best available RNG provider to use. It will, by default, try to use the [`CSRNGProvider`](lib/Providers/Rng/CSRNGProvider.php) for PHP7+ or the [`MCryptRNGProvider`](lib/Providers/Rng/MCryptRNGProvider.php); if this is not available/supported for any reason it will try to use the [`OpenSSLRNGProvider`](lib/Providers/Rng/OpenSSLRNGProvider.php) and if that is also not available/supported it will try to use the final RNG provider: [`HashRNGProvider`](lib/Providers/Rng/HashRNGProvider.php). Each of these providers use their own method of generating a random sequence of bytes. The first three (`CSRNGProvider`, `OpenSSLRNGProvider` and `MCryptRNGProvider`) return a [cryptographically secure](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) sequence of random bytes whereas the `HashRNGProvider` returns a **non-cryptographically secure** sequence.
|
||||
|
||||
You can easily implement your own `RNGProvider` by simply implementing the `IRNGProvider` interface. Each of the 'built-in' RNG providers have some constructor parameters that allow you to 'tweak' some of the settings to use when creating the random bytes such as which source to use (`MCryptRNGProvider`) or which hashing algorithm (`HashRNGProvider`). I encourage you to have a look at some of the ['built-in' RNG providers](lib/Providers/Rng) for details and the [`IRNGProvider` interface](lib/Providers/Rng/IRNGProvider.php).
|
||||
You can easily implement your own `RNGProvider` by simply implementing the `IRNGProvider` interface. Each of the 'built-in' RNG providers have some constructor arguments that allow you to 'tweak' some of the settings to use when creating the random bytes such as which source to use (`MCryptRNGProvider`) or which hashing algorithm (`HashRNGProvider`). I encourage you to have a look at some of the ['built-in' RNG providers](lib/Providers/Rng) for details and the [`IRNGProvider` interface](lib/Providers/Rng/IRNGProvider.php).
|
||||
|
||||
### Time providers
|
||||
|
||||
Another set of providers in this library are the Time Providers; this library provides three 'built-in' ones. The default Time Provider used is the [`LocalMachineTimeProvider`](lib/Providers/Time/LocalMachineTimeProvider.php); this provider simply returns the output of `Time()` and is *highly recommended* as default provider. The [`HttpTimeProvider`](lib/Providers/Time/HttpTimeProvider.php) executes a `HEAD` request against a given webserver (default: google.com) and tries to extract the `Date:`-HTTP header and returns it's date. Other url's/domains can be used by specifying the url in the constructor. The final Time Provider is the [`ConvertUnixTimeDotComTimeProvider`](lib/Providers/Time/ConvertUnixTimeDotComTimeProvider.php) which does a HTTP request to `convert-unix-time.com/api` and decodes the `JSON` result to retrieve the time.
|
||||
Another set of providers in this library are the Time Providers; this library provides three 'built-in' ones. The default Time Provider used is the [`LocalMachineTimeProvider`](lib/Providers/Time/LocalMachineTimeProvider.php); this provider simply returns the output of `Time()` and is *highly recommended* as default provider. The [`HttpTimeProvider`](lib/Providers/Time/HttpTimeProvider.php) executes a `HEAD` request against a given webserver (default: google.com) and tries to extract the `Date:`-HTTP header and returns it's date. Other url's/domains can be used by specifying the url in the constructor. The final Time Provider is the [`NTPTimeProvider`](lib/Providers/Time/NTPTimeProvider.php) which does an NTP request to a specified NTP server.
|
||||
|
||||
You can easily implement your own `TimeProvider` by simply implementing the `ITimeProvider` interface.
|
||||
|
||||
As to *why* these Time Providers are implemented: it allows the TwoFactorAuth library to ensure the hosts time is correct (or rather: within a margin). You can use the `ensureCorrectTime()` method to ensure the hosts time is correct. By default this method will compare the hosts time (returned by calling `time()` on the `LocalMachineTimeProvider`) to Google's and convert-unix-time.com's current time. You can pass an array of `ITimeProvider`s and specify the `leniency` (second argument) allowed (default: 5 seconds). The method will throw when the TwoFactorAuth's timeprovider (which can be any `ITimeProvider`, see constructor) differs more than the given amount of seconds from any of the given `ITimeProviders`. We advise to call this method sparingly when relying on 3rd parties (which both the `HttpTimeProvider` and `ConvertUnixTimeDotComTimeProvider` do) or, if you need to ensure time is correct on a (very) regular basis to implement an `ITimeProvider` that is more efficient than the 'built-in' ones (like use a GPS signal). The `ensureCorrectTime()` method is mostly to be used to make sure the server is configured correctly.
|
||||
As to *why* these Time Providers are implemented: it allows the TwoFactorAuth library to ensure the hosts time is correct (or rather: within a margin). You can use the `ensureCorrectTime()` method to ensure the hosts time is correct. By default this method will compare the hosts time (returned by calling `time()` on the `LocalMachineTimeProvider`) to Google's and convert-unix-time.com's current time. You can pass an array of `ITimeProvider`s and specify the `leniency` (second argument) allowed (default: 5 seconds). The method will throw when the TwoFactorAuth's timeprovider (which can be any `ITimeProvider`, see constructor) differs more than the given amount of seconds from any of the given `ITimeProviders`. We advise to call this method sparingly when relying on 3rd parties (which both the `HttpTimeProvider` and `NTPTimeProvider` do) or, if you need to ensure time is correct on a (very) regular basis to implement an `ITimeProvider` that is more efficient than the 'built-in' ones (like use a GPS signal). The `ensureCorrectTime()` method is mostly to be used to make sure the server is configured correctly.
|
||||
|
||||
## Integrations
|
||||
|
||||
|
@@ -38,10 +38,10 @@
|
||||
<Compile Include="lib\Providers\Rng\OpenSSLRNGProvider.php" />
|
||||
<Compile Include="lib\Providers\Rng\HashRNGProvider.php" />
|
||||
<Compile Include="lib\Providers\Rng\RNGException.php" />
|
||||
<Compile Include="lib\Providers\Time\ConvertUnixTimeDotComTimeProvider.php" />
|
||||
<Compile Include="lib\Providers\Time\HttpTimeProvider.php" />
|
||||
<Compile Include="lib\Providers\Time\ITimeProvider.php" />
|
||||
<Compile Include="lib\Providers\Time\LocalMachineTimeProvider.php" />
|
||||
<Compile Include="lib\Providers\Time\NTPTimeProvider.php" />
|
||||
<Compile Include="lib\Providers\Time\TimeException.php" />
|
||||
<Compile Include="lib\TwoFactorAuth.php" />
|
||||
<Compile Include=".gitignore" />
|
||||
@@ -65,5 +65,6 @@
|
||||
<Content Include="logo.png" />
|
||||
<Content Include="multifactorauthforeveryone.png" />
|
||||
<Content Include="LICENSE" />
|
||||
<Content Include="phpunit.xml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "robthree/twofactorauth",
|
||||
"description": "Two Factor Authentication",
|
||||
"version": "1.6.1",
|
||||
"version": "1.6.5",
|
||||
"type": "library",
|
||||
"keywords": [ "Authentication", "Two Factor Authentication", "Multi Factor Authentication", "TFA", "MFA", "PHP", "Authenticator", "Authy" ],
|
||||
"homepage": "https://github.com/RobThree/TwoFactorAuth",
|
||||
|
@@ -17,7 +17,7 @@ class HashRNGProvider implements IRNGProvider
|
||||
$hash = mt_rand();
|
||||
for ($i = 0; $i < $bytecount; $i++) {
|
||||
$hash = hash($this->algorithm, $hash.mt_rand(), true);
|
||||
$result .= $hash[mt_rand(0, sizeof($hash))];
|
||||
$result .= $hash[mt_rand(0, strlen($hash)-1)];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ class MCryptRNGProvider implements IRNGProvider
|
||||
}
|
||||
|
||||
public function getRandomBytes($bytecount) {
|
||||
$result = mcrypt_create_iv($bytecount, $this->source);
|
||||
$result = @mcrypt_create_iv($bytecount, $this->source);
|
||||
if ($result === false)
|
||||
throw new \RNGException('mcrypt_create_iv returned an invalid value');
|
||||
return $result;
|
||||
|
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RobThree\Auth\Providers\Time;
|
||||
|
||||
class ConvertUnixTimeDotComTimeProvider implements ITimeProvider
|
||||
{
|
||||
public function getTime() {
|
||||
$json = @json_decode(
|
||||
@file_get_contents('http://www.convert-unix-time.com/api?timestamp=now&r=' . uniqid(null, true))
|
||||
);
|
||||
if ($json === null || !is_int($json->timestamp))
|
||||
throw new \TimeException('Unable to retrieve time from convert-unix-time.com');
|
||||
return $json->timestamp;
|
||||
}
|
||||
}
|
52
data/web/inc/lib/vendor/robthree/twofactorauth/lib/Providers/Time/NTPTimeProvider.php
vendored
Normal file
52
data/web/inc/lib/vendor/robthree/twofactorauth/lib/Providers/Time/NTPTimeProvider.php
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace RobThree\Auth\Providers\Time;
|
||||
|
||||
/**
|
||||
* Takes the time from any NTP server
|
||||
*/
|
||||
class NTPTimeProvider implements ITimeProvider
|
||||
{
|
||||
public $host;
|
||||
public $port;
|
||||
public $timeout;
|
||||
|
||||
function __construct($host = 'pool.ntp.org', $port = 123, $timeout = 1)
|
||||
{
|
||||
$this->host = $host;
|
||||
|
||||
if (!is_int($port) || $port <= 0 || $port > 65535)
|
||||
throw new \TimeException('Port must be 0 < port < 65535');
|
||||
$this->port = $port;
|
||||
|
||||
if (!is_int($timeout) || $timeout < 0)
|
||||
throw new \TimeException('Timeout must be >= 0');
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
|
||||
public function getTime() {
|
||||
try {
|
||||
/* Create a socket and connect to NTP server */
|
||||
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
||||
socket_connect($sock, $this->host, $this->port);
|
||||
|
||||
/* Send request */
|
||||
$msg = "\010" . str_repeat("\0", 47);
|
||||
socket_send($sock, $msg, strlen($msg), 0);
|
||||
|
||||
/* Receive response and close socket */
|
||||
socket_recv($sock, $recv, 48, MSG_WAITALL);
|
||||
socket_close($sock);
|
||||
|
||||
/* Interpret response */
|
||||
$data = unpack('N12', $recv);
|
||||
$timestamp = sprintf('%u', $data[9]);
|
||||
|
||||
/* NTP is number of seconds since 0000 UT on 1 January 1900 Unix time is seconds since 0000 UT on 1 January 1970 */
|
||||
return $timestamp - 2208988800;
|
||||
}
|
||||
catch (Exception $ex) {
|
||||
throw new \TimeException(sprintf('Unable to retrieve time from %s (%s)', $this->host, $ex->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
@@ -79,16 +79,23 @@ class TwoFactorAuth
|
||||
/**
|
||||
* Check if the code is correct. This will accept codes starting from ($discrepancy * $period) sec ago to ($discrepancy * period) sec from now
|
||||
*/
|
||||
public function verifyCode($secret, $code, $discrepancy = 1, $time = null)
|
||||
public function verifyCode($secret, $code, $discrepancy = 1, $time = null, &$timeslice = 0)
|
||||
{
|
||||
$result = false;
|
||||
$timetamp = $this->getTime($time);
|
||||
|
||||
// To keep safe from timing-attachs we iterate *all* possible codes even though we already may have verified a code is correct
|
||||
for ($i = -$discrepancy; $i <= $discrepancy; $i++)
|
||||
$result |= $this->codeEquals($this->getCode($secret, $timetamp + ($i * $this->period)), $code);
|
||||
$timeslice = 0;
|
||||
|
||||
return (bool)$result;
|
||||
// To keep safe from timing-attacks we iterate *all* possible codes even though we already may have
|
||||
// verified a code is correct. We use the timeslice variable to hold either 0 (no match) or the timeslice
|
||||
// of the match. Each iteration we either set the timeslice variable to the timeslice of the match
|
||||
// or set the value to itself. This is an effort to maintain constant execution time for the code.
|
||||
for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
|
||||
$ts = $timetamp + ($i * $this->period);
|
||||
$slice = $this->getTimeSlice($ts);
|
||||
$timeslice = $this->codeEquals($this->getCode($secret, $ts), $code) ? $slice : $timeslice;
|
||||
}
|
||||
|
||||
return $timeslice > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,7 +141,7 @@ class TwoFactorAuth
|
||||
|
||||
if ($timeproviders == null)
|
||||
$timeproviders = array(
|
||||
new Providers\Time\ConvertUnixTimeDotComTimeProvider(),
|
||||
new Providers\Time\NTPTimeProvider(),
|
||||
new Providers\Time\HttpTimeProvider()
|
||||
);
|
||||
|
||||
|
20
data/web/inc/lib/vendor/robthree/twofactorauth/phpunit.xml
vendored
Normal file
20
data/web/inc/lib/vendor/robthree/twofactorauth/phpunit.xml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false">
|
||||
<testsuites>
|
||||
<testsuite name="Unit">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">./lib</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
0
data/web/inc/lib/vendor/robthree/twofactorauth/phpunit.xml.tmppica
vendored
Normal file
0
data/web/inc/lib/vendor/robthree/twofactorauth/phpunit.xml.tmppica
vendored
Normal file
@@ -18,7 +18,7 @@ require_once 'lib/Providers/Rng/RNGException.php';
|
||||
require_once 'lib/Providers/Time/ITimeProvider.php';
|
||||
require_once 'lib/Providers/Time/LocalMachineTimeProvider.php';
|
||||
require_once 'lib/Providers/Time/HttpTimeProvider.php';
|
||||
require_once 'lib/Providers/Time/ConvertUnixTimeDotComTimeProvider.php';
|
||||
require_once 'lib/Providers/Time/NTPTimeProvider.php';
|
||||
require_once 'lib/Providers/Time/TimeException.php';
|
||||
|
||||
use RobThree\Auth\TwoFactorAuth;
|
||||
@@ -124,7 +124,8 @@ class TwoFactorAuthTest extends PHPUnit_Framework_TestCase
|
||||
public function testEnsureAllTimeProvidersReturnCorrectTime() {
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30, 'sha1');
|
||||
$tfa->ensureCorrectTime(array(
|
||||
new RobThree\Auth\Providers\Time\ConvertUnixTimeDotComTimeProvider(),
|
||||
new RobThree\Auth\Providers\Time\NTPTimeProvider(), // Uses pool.ntp.org by default
|
||||
//new RobThree\Auth\Providers\Time\NTPTimeProvider('time.google.com'), // Somehow time.google.com and time.windows.com make travis timeout??
|
||||
new RobThree\Auth\Providers\Time\HttpTimeProvider(), // Uses google.com by default
|
||||
new RobThree\Auth\Providers\Time\HttpTimeProvider('https://github.com'),
|
||||
new RobThree\Auth\Providers\Time\HttpTimeProvider('https://yahoo.com'),
|
||||
@@ -150,6 +151,31 @@ class TwoFactorAuthTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(true , $tfa->verifyCode('VMR466AB62ZBOKHE', '543160', 2, 1426847205 - 65)); //Test discrepancy
|
||||
}
|
||||
|
||||
public function testVerifyCorrectTimeSliceIsReturned() {
|
||||
$tfa = new TwoFactorAuth('Test', 6, 30);
|
||||
|
||||
// We test with discrepancy 3 (so total of 7 codes: c-3, c-2, c-1, c, c+1, c+2, c+3
|
||||
// Ensure each corresponding timeslice is returned correctly
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '534113', 3, 1426847190, $timeslice1));
|
||||
$this->assertEquals(47561570, $timeslice1);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '819652', 3, 1426847190, $timeslice2));
|
||||
$this->assertEquals(47561571, $timeslice2);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '915954', 3, 1426847190, $timeslice3));
|
||||
$this->assertEquals(47561572, $timeslice3);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '543160', 3, 1426847190, $timeslice4));
|
||||
$this->assertEquals(47561573, $timeslice4);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '348401', 3, 1426847190, $timeslice5));
|
||||
$this->assertEquals(47561574, $timeslice5);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '648525', 3, 1426847190, $timeslice6));
|
||||
$this->assertEquals(47561575, $timeslice6);
|
||||
$this->assertEquals(true, $tfa->verifyCode('VMR466AB62ZBOKHE', '170645', 3, 1426847190, $timeslice7));
|
||||
$this->assertEquals(47561576, $timeslice7);
|
||||
|
||||
// Incorrect code should return false and a 0 timeslice
|
||||
$this->assertEquals(false, $tfa->verifyCode('VMR466AB62ZBOKHE', '111111', 3, 1426847190, $timeslice8));
|
||||
$this->assertEquals(0, $timeslice8);
|
||||
}
|
||||
|
||||
public function testTotpUriIsCorrect() {
|
||||
$qr = new TestQrProvider();
|
||||
|
||||
@@ -295,10 +321,12 @@ class TwoFactorAuthTest extends PHPUnit_Framework_TestCase
|
||||
* @requires function mcrypt_create_iv
|
||||
*/
|
||||
public function testMCryptRNGProvidersReturnExpectedNumberOfBytes() {
|
||||
$rng = new \RobThree\Auth\Providers\Rng\MCryptRNGProvider();
|
||||
foreach ($this->getRngTestLengths() as $l)
|
||||
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
|
||||
$this->assertEquals(true, $rng->isCryptographicallySecure());
|
||||
if (function_exists('mcrypt_create_iv')) {
|
||||
$rng = new \RobThree\Auth\Providers\Rng\MCryptRNGProvider();
|
||||
foreach ($this->getRngTestLengths() as $l)
|
||||
$this->assertEquals($l, strlen($rng->getRandomBytes($l)));
|
||||
$this->assertEquals(true, $rng->isCryptographicallySecure());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user