|
4 | 4 | use bunq\Exception\BunqException; |
5 | 5 | use bunq\Model\Core\DeviceServerInternal; |
6 | 6 | use bunq\Model\Core\Installation; |
| 7 | +use bunq\Model\Core\PaymentServiceProviderCredentialInternal; |
7 | 8 | use bunq\Model\Core\SandboxUserInternal; |
8 | 9 | use bunq\Model\Core\SessionServer; |
9 | 10 | use bunq\Model\Core\Token; |
10 | 11 | use bunq\Model\Generated\Endpoint\Session; |
| 12 | +use bunq\Model\Generated\Object\Certificate; |
11 | 13 | use bunq\Security\KeyPair; |
| 14 | +use bunq\Security\PrivateKey; |
12 | 15 | use bunq\Util\BunqEnumApiEnvironmentType; |
13 | 16 | use bunq\Util\FileUtil; |
14 | 17 | use bunq\Util\InstallationUtil; |
| 18 | +use bunq\Util\SecurityUtil; |
15 | 19 | use GuzzleHttp\Psr7\Uri; |
16 | 20 |
|
17 | 21 | /** |
@@ -117,6 +121,46 @@ public static function create( |
117 | 121 | return $apiContext; |
118 | 122 | } |
119 | 123 |
|
| 124 | + /** |
| 125 | + * @param BunqEnumApiEnvironmentType $environmentType |
| 126 | + * @param Certificate $publicCertificate |
| 127 | + * @param PrivateKey $privateKey |
| 128 | + * @param Certificate[] $allChainCertificate |
| 129 | + * @param string $description |
| 130 | + * @param string[] $allPermittedIp |
| 131 | + * @param string|null $proxyUrl |
| 132 | + * |
| 133 | + * @return ApiContext |
| 134 | + */ |
| 135 | + public static function createForPsd2( |
| 136 | + BunqEnumApiEnvironmentType $environmentType, |
| 137 | + Certificate $publicCertificate, |
| 138 | + PrivateKey $privateKey, |
| 139 | + array $allChainCertificate, |
| 140 | + string $description, |
| 141 | + array $allPermittedIp = [], |
| 142 | + string $proxyUrl = null |
| 143 | + ): ApiContext { |
| 144 | + InstallationUtil::assertDeviceDescriptionIsValid($description); |
| 145 | + InstallationUtil::assertAllIpIsValid($allPermittedIp); |
| 146 | + |
| 147 | + $apiContext = new static(); |
| 148 | + $apiContext->environmentType = $environmentType; |
| 149 | + $apiContext->proxyUrl = $proxyUrl; |
| 150 | + |
| 151 | + $apiContext->initializeInstallationContext(); |
| 152 | + $apiContext->initializePsd2Credential( |
| 153 | + $publicCertificate, |
| 154 | + $privateKey, |
| 155 | + $allChainCertificate |
| 156 | + ); |
| 157 | + |
| 158 | + $apiContext->registerDevice($description, $allPermittedIp); |
| 159 | + $apiContext->initializeSessionContext(); |
| 160 | + |
| 161 | + return $apiContext; |
| 162 | + } |
| 163 | + |
120 | 164 | /** |
121 | 165 | */ |
122 | 166 | private function createSandboxUser() |
@@ -152,6 +196,34 @@ private function initializeInstallationContext() |
152 | 196 | ); |
153 | 197 | } |
154 | 198 |
|
| 199 | + /** |
| 200 | + * @param Certificate $publicCertificate |
| 201 | + * @param PrivateKey $privateKey |
| 202 | + * @param Certificate[] $allChainCertificate |
| 203 | + */ |
| 204 | + private function initializePsd2Credential( |
| 205 | + Certificate $publicCertificate, |
| 206 | + PrivateKey $privateKey, |
| 207 | + array $allChainCertificate |
| 208 | + ) { |
| 209 | + $sessionToken = $this->installationContext->getInstallationToken(); |
| 210 | + $clientKeyPair = $this->installationContext->getKeyPairClient(); |
| 211 | + |
| 212 | + $stringToSign = SecurityUtil::getPublicKeyFormattedString( |
| 213 | + $clientKeyPair->getPublicKey() |
| 214 | + ) . $sessionToken->getToken(); |
| 215 | + |
| 216 | + $keySignature = $privateKey->sign($stringToSign); |
| 217 | + $paymentProviderResponse = PaymentServiceProviderCredentialInternal::createWithApiContext( |
| 218 | + $publicCertificate->getCertificate(), |
| 219 | + SecurityUtil::getCertificateChainString($allChainCertificate), |
| 220 | + $keySignature, |
| 221 | + $this |
| 222 | + ); |
| 223 | + |
| 224 | + $this->apiKey = $paymentProviderResponse->getValue()->getTokenValue(); |
| 225 | + } |
| 226 | + |
155 | 227 | /** |
156 | 228 | * @param string $description |
157 | 229 | * @param string[] $permittedIps |
@@ -183,7 +255,7 @@ private function initializeSessionContext() |
183 | 255 | */ |
184 | 256 | public static function restore(string $fileName = self::FILENAME_CONFIG_DEFAULT): ApiContext |
185 | 257 | { |
186 | | - $contextJsonString = self::getContextJsonString($fileName); |
| 258 | + $contextJsonString = static::getContextJsonString($fileName); |
187 | 259 |
|
188 | 260 | return static::fromJson($contextJsonString); |
189 | 261 | } |
|
0 commit comments