|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Pushok package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arthur Edamov <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Pushok\AuthProvider; |
|
13
|
|
|
|
|
14
|
|
|
use Pushok\AuthProviderInterface; |
|
15
|
|
|
use Pushok\Request; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Certificate |
|
19
|
|
|
* @package Pushok\AuthProvider |
|
20
|
|
|
* |
|
21
|
|
|
* @see http://bit.ly/communicating-with-apns |
|
22
|
|
|
*/ |
|
23
|
|
|
class Certificate implements AuthProviderInterface |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Path to certificate. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $certificatePath; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Certificate secret. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $certificateSecret; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The bundle ID for app obtained from Apple developer account. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $appBundleId; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* This provider accepts the following options: |
|
48
|
|
|
* |
|
49
|
|
|
* - certificate_path |
|
50
|
|
|
* - certificate_secret |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
*/ |
|
54
|
|
|
private function __construct(array $options) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->certificatePath = $options['certificate_path'] ; |
|
57
|
|
|
$this->certificateSecret = $options['certificate_secret']; |
|
58
|
|
|
$this->appBundleId = $options['app_bundle_id'] ?? null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create Certificate Auth provider. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $options |
|
65
|
|
|
* @return Certificate |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function create(array $options): Certificate |
|
68
|
|
|
{ |
|
69
|
|
|
return new self($options); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Authenticate client. |
|
74
|
|
|
* |
|
75
|
|
|
* @param Request $request |
|
76
|
|
|
*/ |
|
77
|
|
|
public function authenticateClient(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$request->addOptions( |
|
80
|
|
|
[ |
|
81
|
|
|
CURLOPT_SSLCERT => $this->certificatePath, |
|
82
|
|
|
CURLOPT_SSLCERTPASSWD => $this->certificateSecret, |
|
83
|
|
|
CURLOPT_SSL_VERIFYPEER => true |
|
84
|
|
|
] |
|
85
|
|
|
); |
|
86
|
|
|
$request->addHeaders([ |
|
87
|
|
|
'apns-topic' => $this->generateApnsTopic($request->getHeaders()['apns-push-type']), |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Generate a correct apns-topic string |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $pushType |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
public function generateApnsTopic(string $pushType) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
switch ($pushType) { |
|
100
|
|
|
case 'voip': |
|
101
|
|
|
return $this->appBundleId . '.voip'; |
|
102
|
|
|
|
|
103
|
|
|
case 'complication': |
|
104
|
|
|
return $this->appBundleId . '.complication'; |
|
105
|
|
|
|
|
106
|
|
|
case 'fileprovider': |
|
107
|
|
|
return $this->appBundleId . '.pushkit.fileprovider'; |
|
108
|
|
|
|
|
109
|
|
|
default: |
|
110
|
|
|
return $this->appBundleId; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|