1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: GCC-MED |
5
|
|
|
* Date: 29/01/2018 |
6
|
|
|
* Time: 16:58 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Test |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function indexAction() { |
12
|
|
|
$certsRepository = $this->sqlOauth->getRepository(Cert::class); |
13
|
|
|
|
14
|
|
|
$actualCerts = $certsRepository->findBy([], ['createdAt' => 'DESC'], 2); |
15
|
|
|
|
16
|
|
|
$data = []; |
17
|
|
|
foreach ($actualCerts as $cert) { |
18
|
|
|
$data[] = [ |
19
|
|
|
'kty' => 'RSA', |
20
|
|
|
'alg' => 'RSA256', |
21
|
|
|
'use' => 'sig', |
22
|
|
|
'kid' => $cert->getKid(), |
23
|
|
|
'n' => $cert->getN(), |
24
|
|
|
'e' => $cert->getE(), |
25
|
|
|
'x5c' => $this->pemToInline($cert->getPublicKey()) |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return new JsonResponse(json_encode($data, JSON_PRETTY_PRINT), 200, [ |
30
|
|
|
'Access-Control-Allow-Origin' => '*' |
31
|
|
|
], true); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function pemToInline($pem) { |
35
|
|
|
$pem = str_replace('-----BEGIN PUBLIC KEY-----', '', $pem); |
36
|
|
|
$pem = str_replace('-----END PUBLIC KEY-----', '', $pem); |
37
|
|
|
$pem = str_replace("\n", '', $pem); |
38
|
|
|
return $pem; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @Route("/oauth/certs/rotate", name="certs_rotate") |
43
|
|
|
* @return JsonResponse |
44
|
|
|
*/ |
45
|
|
|
public function rotateAction() { |
46
|
|
|
$certsRepository = $this->sqlOauth->getRepository(Cert::class); |
47
|
|
|
|
48
|
|
|
$actualCerts = $certsRepository->findBy([], ['createdAt' => 'DESC'], 2); |
49
|
|
|
if(!isset($actualCerts[0])) { |
50
|
|
|
list('privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $rsa) = $this->generateRSAKeys(); |
51
|
|
|
|
52
|
|
|
$oldCert = new Cert(); |
53
|
|
|
$oldCert->setPrivateKey($privKey); |
54
|
|
|
$oldCert->setPublicKey($pubKey); |
55
|
|
|
$oldCert->setN(isset($rsa['n']) ? base64_encode($rsa['n']) : null); |
56
|
|
|
$oldCert->setE(isset($rsa['e']) ? base64_encode($rsa['e']) : null); |
57
|
|
|
$this->sqlOauth->persist($oldCert); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
list('privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $rsa) = $this->generateRSAKeys(); |
61
|
|
|
$newCert = new Cert(); |
62
|
|
|
$newCert->setPrivateKey($privKey); |
63
|
|
|
$newCert->setPublicKey($pubKey); |
64
|
|
|
$newCert->setN(isset($rsa['n']) ? base64_encode($rsa['n']) : null); |
65
|
|
|
$newCert->setE(isset($rsa['e']) ? base64_encode($rsa['e']) : null); |
66
|
|
|
$this->sqlOauth->persist($newCert); |
67
|
|
|
|
68
|
|
|
// VarDumper::dump($oldCert);die; |
|
|
|
|
69
|
|
|
$this->sqlOauth->flush(); |
70
|
|
|
return new JsonResponse(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function generateRSAKeys() { |
74
|
|
|
$config = array( |
75
|
|
|
"digest_alg" => "sha512", |
76
|
|
|
"private_key_bits" => 4096, |
77
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA, |
78
|
|
|
); |
79
|
|
|
// Create the private and public key |
80
|
|
|
$res = openssl_pkey_new($config); |
81
|
|
|
|
82
|
|
|
// Extract the private key from $res to $privKey |
83
|
|
|
openssl_pkey_export($res, $privKey); |
84
|
|
|
|
85
|
|
|
// Extract the public key from $res to $pubKey |
86
|
|
|
$details = openssl_pkey_get_details($res); |
87
|
|
|
|
88
|
|
|
$pubKey = $details["key"]; |
89
|
|
|
return ['privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $details['rsa']]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.