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\PassPhrase; |
12
|
|
|
use BEAR\JwtAuth\Annotation\PrivateKey; |
13
|
|
|
use BEAR\JwtAuth\Annotation\PublicKey; |
14
|
|
|
use BEAR\JwtAuth\Annotation\Ttl; |
15
|
|
|
use BEAR\JwtAuth\Auth\Auth; |
16
|
|
|
use BEAR\JwtAuth\Auth\AuthProvider; |
17
|
|
|
use BEAR\JwtAuth\Encoder\JwtEncoderInterface; |
18
|
|
|
use BEAR\JwtAuth\Encoder\NamshiAsymmetric; |
19
|
|
|
use BEAR\JwtAuth\Exception\NoConfigException; |
20
|
|
|
use BEAR\JwtAuth\Generator\JwtGenerator; |
21
|
|
|
use BEAR\JwtAuth\Generator\JwtGeneratorInterface; |
22
|
|
|
use Ray\Di\AbstractModule; |
23
|
|
|
use Ray\Di\Scope; |
24
|
|
|
|
25
|
|
|
class AsymmetricJwtAuthModule extends AbstractModule |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $algo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $ttl; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $privateKey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $publicKey; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $passPhrase; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $algo Hashing algorithm |
54
|
|
|
* @param int $ttl Time to live for JWT |
55
|
|
|
* @param string $privateKey Private key |
56
|
|
|
* @param string $publicKey Public key |
57
|
|
|
* @param string $passPhrase Pass phrase |
58
|
|
|
*/ |
59
|
5 |
|
public function __construct(string $algo, int $ttl, string $privateKey, string $publicKey, string $passPhrase) |
60
|
|
|
{ |
61
|
5 |
|
if ($algo == '' |
62
|
4 |
|
|| $ttl < 0 |
63
|
4 |
|
|| $privateKey == '' |
64
|
4 |
|
|| $privateKey == '' |
65
|
5 |
|
|| $passPhrase == '') { |
66
|
1 |
|
throw new NoConfigException; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$this->algo = $algo; |
70
|
4 |
|
$this->ttl = $ttl; |
71
|
4 |
|
$this->privateKey = $privateKey; |
72
|
4 |
|
$this->publicKey = $publicKey; |
73
|
4 |
|
$this->passPhrase = $passPhrase; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
4 |
|
protected function configure() |
77
|
|
|
{ |
78
|
4 |
|
$this->install(new TokenExtractorModule()); |
79
|
|
|
|
80
|
4 |
|
$this->bind()->annotatedWith(Algo::class)->toInstance($this->algo); |
81
|
4 |
|
$this->bind()->annotatedWith(Ttl::class)->toInstance($this->ttl); |
82
|
4 |
|
$this->bind()->annotatedWith(PrivateKey::class)->toInstance($this->privateKey); |
83
|
4 |
|
$this->bind()->annotatedWith(PublicKey::class)->toInstance($this->publicKey); |
84
|
4 |
|
$this->bind()->annotatedWith(PassPhrase::class)->toInstance($this->passPhrase); |
85
|
|
|
|
86
|
4 |
|
$this->bind(JwtGeneratorInterface::class)->to(JwtGenerator::class)->in(Scope::SINGLETON); |
87
|
4 |
|
$this->bind(JwtEncoderInterface::class)->to(NamshiAsymmetric::class)->in(Scope::SINGLETON); |
88
|
4 |
|
$this->bind(Auth::class)->toProvider(AuthProvider::class)->in(Scope::SINGLETON); |
89
|
4 |
|
} |
90
|
|
|
} |
91
|
|
|
|