28
28
*/
29
29
30
30
use Psr \Log \LoggerInterface ;
31
+ use Lcobucci \JWT \Signer \Key ;
32
+ use Lcobucci \JWT \Signer \Ecdsa \Sha256 ;
33
+ use Lcobucci \JWT \Configuration ;
31
34
32
35
/**
33
36
* Abstract class: this is the superclass for all Apple Push Notification Service
@@ -64,6 +67,9 @@ abstract class ApnsPHP_Abstract
64
67
65
68
protected $ _sProviderCertificateFile ; /**< @type string Provider certificate file with key (Bundled PEM). */
66
69
protected $ _sProviderCertificatePassphrase ; /**< @type string Provider certificate passphrase. */
70
+ protected $ _sProviderToken ; /**< @type string|null Provider Authentication token. */
71
+ protected $ _sProviderTeamId ; /**< @type string|null Apple Team Identifier. */
72
+ protected $ _sProviderKeyId ; /**< @type string|null Apple Key Identifier. */
67
73
protected $ _sRootCertificationAuthorityFile ; /**< @type string Root certification authority file. */
68
74
69
75
protected $ _nWriteInterval ; /**< @type integer Write interval in micro seconds. */
@@ -106,7 +112,7 @@ public function __construct($nEnvironment, $sProviderCertificateFile, $nProtocol
106
112
);
107
113
}
108
114
$ this ->_nProtocol = $ nProtocol ;
109
-
115
+
110
116
$ this ->_nConnectTimeout = ini_get ("default_socket_timeout " );
111
117
$ this ->_nWriteInterval = self ::WRITE_INTERVAL ;
112
118
$ this ->_nConnectRetryInterval = self ::CONNECT_RETRY_INTERVAL ;
@@ -167,6 +173,26 @@ public function setProviderCertificatePassphrase($sProviderCertificatePassphrase
167
173
$ this ->_sProviderCertificatePassphrase = $ sProviderCertificatePassphrase ;
168
174
}
169
175
176
+ /**
177
+ * Set the Team Identifier.
178
+ *
179
+ * @param string $sTeamId Apple Team Identifier.
180
+ */
181
+ public function setTeamId ($ sTeamId )
182
+ {
183
+ $ this ->_sProviderTeamId = $ sTeamId ;
184
+ }
185
+
186
+ /**
187
+ * Set the Key Identifier.
188
+ *
189
+ * @param string $sKeyId Apple Key Identifier.
190
+ */
191
+ public function setKeyId ($ sKeyId )
192
+ {
193
+ $ this ->_sProviderKeyId = $ sKeyId ;
194
+ }
195
+
170
196
/**
171
197
* Set the Root Certification Authority file.
172
198
*
@@ -401,7 +427,7 @@ protected function _connect()
401
427
*/
402
428
protected function _httpInit ()
403
429
{
404
- $ this ->_log ( " INFO: Trying to initialize HTTP/2 backend... " );
430
+ $ this ->_logger ()-> info ( " Trying to initialize HTTP/2 backend... " );
405
431
406
432
$ this ->_hSocket = curl_init ();
407
433
if (!$ this ->_hSocket ) {
@@ -413,24 +439,41 @@ protected function _httpInit()
413
439
if (!defined ('CURL_HTTP_VERSION_2_0 ' )) {
414
440
define ('CURL_HTTP_VERSION_2_0 ' , 3 );
415
441
}
416
-
417
- if (!curl_setopt_array ($ this ->_hSocket , array (
418
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0 ,
419
- CURLOPT_SSLCERT => $ this ->_sProviderCertificateFile ,
420
- CURLOPT_SSLCERTPASSWD => empty ($ this ->_sProviderCertificatePassphrase ) ? null : $ this ->_sProviderCertificatePassphrase ,
421
- CURLOPT_RETURNTRANSFER => true ,
422
- CURLOPT_USERAGENT => 'ApnsPHP ' ,
423
- CURLOPT_CONNECTTIMEOUT => 10 ,
424
- CURLOPT_TIMEOUT => 30 ,
425
- CURLOPT_SSL_VERIFYPEER => true ,
426
- CURLOPT_VERBOSE => false
427
- ))) {
442
+ $ aCurlOpts = array (
443
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0 ,
444
+ CURLOPT_RETURNTRANSFER => true ,
445
+ CURLOPT_USERAGENT => 'ApnsPHP ' ,
446
+ CURLOPT_CONNECTTIMEOUT => 10 ,
447
+ CURLOPT_TIMEOUT => 30 ,
448
+ CURLOPT_SSL_VERIFYPEER => true ,
449
+ CURLOPT_VERBOSE => false
450
+ );
451
+
452
+ if (strpos ($ this ->_sProviderCertificateFile , '.pem ' ) !== false ) {
453
+ $ this ->_logger ()->info ("Initializing HTTP/2 backend with certificate. " );
454
+ $ aCurlOpts [CURLOPT_SSLCERT ] = $ this ->_sProviderCertificateFile ;
455
+ $ aCurlOpts [CURLOPT_SSLCERTPASSWD ] = empty ($ this ->_sProviderCertificatePassphrase ) ? null : $ this ->_sProviderCertificatePassphrase ;
456
+ }
457
+
458
+ if (strpos ($ this ->_sProviderCertificateFile , '.p8 ' ) !== false ) {
459
+ $ this ->_logger ()->info ("Initializing HTTP/2 backend with key. " );
460
+ $ cKey = new Key \LocalFileReference ('file:// ' . $ this ->_sProviderCertificateFile );
461
+ $ cToken = Configuration::forUnsecuredSigner ()->builder ()
462
+ ->issuedBy ($ this ->_sProviderTeamId )
463
+ ->issuedAt (new DateTimeImmutable ())
464
+ ->withHeader ('kid ' , $ this ->_sProviderKeyId )
465
+ ->getToken (new Sha256 (), $ cKey );
466
+
467
+ $ this ->_sProviderToken = (string ) $ cToken ;
468
+ }
469
+
470
+ if (!curl_setopt_array ($ this ->_hSocket , $ aCurlOpts )) {
428
471
throw new ApnsPHP_Exception (
429
472
"Unable to initialize HTTP/2 backend. "
430
473
);
431
474
}
432
475
433
- $ this ->_log ( " INFO: Initialized HTTP/2 backend. " );
476
+ $ this ->_logger ()-> info ( " Initialized HTTP/2 backend. " );
434
477
435
478
return true ;
436
479
}
@@ -443,7 +486,7 @@ protected function _httpInit()
443
486
*/
444
487
protected function _binaryConnect ($ sURL )
445
488
{
446
- $ this ->_log ("Trying {$ sURL }... " );
489
+ $ this ->_logger ()-> info ("Trying {$ sURL }... " );
447
490
$ sURL = $ this ->_aServiceURLs [$ this ->_nEnvironment ];
448
491
449
492
$ this ->_logger ()->info ("Trying {$ sURL }... " );
@@ -478,7 +521,7 @@ protected function _binaryConnect($sURL)
478
521
479
522
return true ;
480
523
}
481
-
524
+
482
525
/**
483
526
* Return the Logger (with lazy loading)
484
527
*/
0 commit comments