1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the BEAR.JwtAuthModule package. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
namespace BEAR\JwtAuth; |
9
|
|
|
|
10
|
|
|
use BEAR\JwtAuth\Annotation\Algo; |
11
|
|
|
use BEAR\JwtAuth\Annotation\Secret; |
12
|
|
|
use BEAR\JwtAuth\Annotation\Ttl; |
13
|
|
|
use BEAR\JwtAuth\Auth\Auth; |
14
|
|
|
use BEAR\JwtAuth\Auth\AuthProvider; |
15
|
|
|
use BEAR\JwtAuth\Encoder\JwtEncoderInterface; |
16
|
|
|
use BEAR\JwtAuth\Encoder\NamshiSymmetric; |
17
|
|
|
use BEAR\JwtAuth\Exception\NoConfigException; |
18
|
|
|
use BEAR\JwtAuth\Generator\JwtGenerator; |
19
|
|
|
use BEAR\JwtAuth\Generator\JwtGeneratorInterface; |
20
|
|
|
use Ray\Di\AbstractModule; |
21
|
|
|
use Ray\Di\Scope; |
22
|
|
|
|
23
|
|
|
class SymmetricJwtAuthModule extends AbstractModule |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $algo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $ttl; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $secret; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $algo Hashing algorithm |
42
|
|
|
* @param int $ttl Time to live |
43
|
|
|
* @param string $secret Used for symmetric algorithms |
44
|
|
|
*/ |
45
|
7 |
|
public function __construct(string $algo, int $ttl, string $secret) |
46
|
|
|
{ |
47
|
7 |
|
if ($algo == '' |
48
|
6 |
|
|| $ttl < 0 |
49
|
7 |
|
|| $secret == '') { |
50
|
1 |
|
throw new NoConfigException; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
$this->algo = $algo; |
54
|
6 |
|
$this->ttl = $ttl; |
55
|
6 |
|
$this->secret = $secret; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
6 |
|
protected function configure() |
59
|
|
|
{ |
60
|
6 |
|
$this->install(new TokenExtractorModule()); |
61
|
|
|
|
62
|
6 |
|
$this->bind()->annotatedWith(Algo::class)->toInstance($this->algo); |
63
|
6 |
|
$this->bind()->annotatedWith(Ttl::class)->toInstance($this->ttl); |
64
|
6 |
|
$this->bind()->annotatedWith(Secret::class)->toInstance($this->secret); |
65
|
|
|
|
66
|
6 |
|
$this->bind(JwtGeneratorInterface::class)->to(JwtGenerator::class)->in(Scope::SINGLETON); |
67
|
6 |
|
$this->bind(JwtEncoderInterface::class)->to(NamshiSymmetric::class)->in(Scope::SINGLETON); |
68
|
6 |
|
$this->bind(Auth::class)->toProvider(AuthProvider::class)->in(Scope::SINGLETON); |
69
|
6 |
|
} |
70
|
|
|
} |
71
|
|
|
|