This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
/
CounterBasedOtpSharedParametersGenerator.php
91 lines (81 loc) · 2.74 KB
/
CounterBasedOtpSharedParametersGenerator.php
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
<?php
/*
* This file is part of the Otis package.
*
* Copyright © 2013 Erin Millard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eloquent\Otis\Parameters\Generator;
use Eloquent\Otis\Configuration\MfaConfigurationInterface;
use Eloquent\Otis\Exception\UnsupportedArgumentsException;
use Eloquent\Otis\Parameters\MfaSharedParametersInterface;
use Icecave\Isolator\Isolator;
/**
* Generates counter-based one-time password shared parameters.
*/
class CounterBasedOtpSharedParametersGenerator implements
MfaSharedParametersGeneratorInterface,
CounterBasedOtpSharedParametersGeneratorInterface
{
/**
* Construct a new counter-based one-time password shared parameter generator.
*
* @param Isolator|null $isolator The isolator to use.
*/
public function __construct(Isolator $isolator = null)
{
$this->isolator = Isolator::get($isolator);
}
/**
* Returns true if this generator supports the supplied configuration.
*
* @param MfaConfigurationInterface $configuration The configuration to generate shared parameters for.
*
* @return boolean True if the configuration is supported.
*/
public function supports(MfaConfigurationInterface $configuration)
{
return $configuration instanceof CounterBasedOtpConfigurationInterface;
}
/**
* Generate a set of shared parameters.
*
* @param MfaConfigurationInterface $configuration The configuration to generate shared parameters for.
*
* @return MfaSharedParametersInterface The generated shared parameters.
* @throws UnsupportedArgumentsException If the configuration type is not supported.
*/
public function generate(MfaConfigurationInterface $configuration)
{
if (!$this->supports($configuration)) {
throw new UnsupportedArgumentsException;
}
return $this->generateCounterBased($configuration);
}
/**
* Generate a set of counter-based one-time password shared parameters.
*
* @param CounterBasedOtpConfigurationInterface $configuration The configuration to generate shared parameters for.
*
* @return CounterBasedOtpSharedParametersInterface The generated shared parameters.
*/
public function generateCounterBased(
CounterBasedOtpConfigurationInterface $configuration
) {
return new CounterBasedOtpSharedParameters(
$this->isolator()->mcrypt_create_iv($configuration->secretLength())
);
}
/**
* Get the isolator.
*
* @return Isolator The isolator.
*/
public function isolator()
{
return $this->isolator;
}
private $isolator;
}