|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use Gaufrette\Filesystem; |
|
14
|
|
|
use Gaufrette\Exception\FileNotFound; |
|
15
|
|
|
use OAuth2\Storage\PublicKeyInterface; |
|
16
|
|
|
|
|
17
|
|
|
class PublicKey implements PublicKeyInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Filesystem */ |
|
20
|
|
|
private $filesystem; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $fileName; |
|
24
|
|
|
|
|
25
|
1 |
|
public function setFilesystem(Filesystem $filesystem, $fileName = 'private.pem') |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->filesystem = $filesystem; |
|
28
|
1 |
|
$this->fileName = $fileName; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getEncryptionAlgorithm($client_id = null) |
|
32
|
|
|
{ |
|
33
|
1 |
|
return 'RS256'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getPrivateKey($client_id = null) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$key = $this->getPrivateKeyResource(); |
|
39
|
1 |
|
openssl_pkey_export($key, $priv); |
|
40
|
|
|
|
|
41
|
1 |
|
return $priv; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function getPublicKey($client_id = null) |
|
45
|
|
|
{ |
|
46
|
1 |
|
$details = openssl_pkey_get_details($this->getPrivateKeyResource()); |
|
47
|
|
|
|
|
48
|
1 |
|
return $details['key']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \Gaufrette\File |
|
53
|
|
|
*/ |
|
54
|
1 |
|
private function createKeys() |
|
55
|
|
|
{ |
|
56
|
1 |
|
$priv = null; |
|
57
|
1 |
|
$key = openssl_pkey_new(); |
|
58
|
1 |
|
openssl_pkey_export($key, $priv); |
|
59
|
1 |
|
$file = $this->filesystem->get($this->fileName, true); |
|
60
|
1 |
|
$file->setContent($priv); |
|
61
|
|
|
|
|
62
|
1 |
|
return $file; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return resource |
|
67
|
|
|
*/ |
|
68
|
1 |
|
private function getPrivateKeyResource() |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
1 |
|
$file = $this->filesystem->get($this->fileName); |
|
72
|
1 |
|
} catch (FileNotFound $e) { |
|
73
|
1 |
|
$file = $this->createKeys(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$key = openssl_pkey_get_private($file->getContent()); |
|
77
|
|
|
|
|
78
|
1 |
|
return $key; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.