Skip to content

Commit f404cbc

Browse files
authored
Merge pull request #184 from razvanphp/master
Add support for DER and P12 certificates
2 parents 52dae5b + c3b4b36 commit f404cbc

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ foreach ($responses as $response) {
110110
}
111111
```
112112

113-
Using Certificate (.pem). Only the initilization differs from JWT code (above). Remember to include the rest of the code by yourself.
113+
Using Certificate (.pem or .p12). Only the initilization differs from JWT code (above). Remember to include the rest of the code by yourself.
114114

115115
``` php
116116
<?php

src/AuthProvider/Certificate.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public static function create(array $options): Certificate
7676
*/
7777
public function authenticateClient(Request $request)
7878
{
79+
# OpenSSL (versions 0.9.3 and later) also support "P12" for PKCS#12-encoded files.
80+
# see https://curl.se/libcurl/c/CURLOPT_SSLCERTTYPE.html
81+
$ext = pathinfo($this->certificatePath, \PATHINFO_EXTENSION);
82+
if (preg_match('#^(der|p12)$#i', $ext)) {
83+
$request->addOptions(
84+
[
85+
CURLOPT_SSLCERTTYPE => strtoupper($ext)
86+
]
87+
);
88+
}
7989
$request->addOptions(
8090
[
8191
CURLOPT_SSLCERT => $this->certificatePath,

tests/AuthProvider/CertificateTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ public function testAuthenticatingClient()
3939
$this->assertSame($request->getOptions()[CURLOPT_SSLCERTPASSWD], $options['certificate_secret']);
4040
}
4141

42+
public function testAuthenticatingClientP12()
43+
{
44+
$certFile = tempnam(sys_get_temp_dir(), "mock_test_cert");
45+
rename($certFile, $certFile .= '.p12');
46+
try {
47+
$options = [
48+
'certificate_path' => $certFile,
49+
'certificate_secret' => 'secret',
50+
'app_bundle_id' => 'com.apple.test',
51+
];
52+
$authProvider = Certificate::create($options);
53+
54+
$request = $this->createRequest();
55+
$authProvider->authenticateClient($request);
56+
57+
$this->assertSame($request->getOptions()[CURLOPT_SSLCERTTYPE], 'P12');
58+
} finally {
59+
@\unlink($certFile);
60+
}
61+
}
62+
4263
public function testVoipApnsTopic()
4364
{
4465
$options = $this->getOptions();

0 commit comments

Comments
 (0)