Skip to content

Commit 3241a18

Browse files
committed
PhpStorm was lost with Git changes
1 parent 7d3a089 commit 3241a18

File tree

5 files changed

+75
-16
lines changed

5 files changed

+75
-16
lines changed

src/Encryption.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ final class Encryption
2121

2222
/**
2323
* @param string $payload
24-
* @param bool $automatic
24+
* @param bool $automatic
25+
*
2526
* @return string padded payload (plaintext)
2627
*/
2728
public static function padPayload($payload, $automatic)
2829
{
2930
$payloadLen = Utils::safe_strlen($payload);
3031
$padLen = $automatic ? self::MAX_PAYLOAD_LENGTH - $payloadLen : 0;
32+
3133
return pack('n*', $padLen).str_pad($payload, $padLen + $payloadLen, chr(0), STR_PAD_LEFT);
3234
}
3335

3436
/**
35-
* @param string $payload With padding
36-
* @param string $userPublicKey Base 64 encoded (MIME or URL-safe)
37-
* @param string $userAuthToken Base 64 encoded (MIME or URL-safe)
37+
* @param string $payload With padding
38+
* @param string $userPublicKey Base 64 encoded (MIME or URL-safe)
39+
* @param string $userAuthToken Base 64 encoded (MIME or URL-safe)
3840
* @param bool $nativeEncryption Use OpenSSL (>PHP7.1)
3941
*
4042
* @return array
@@ -84,7 +86,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
8486
// encrypt
8587
// "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
8688
if (!$nativeEncryption) {
87-
list($encryptedText, $tag) = \AESGCM\AESGCM::encrypt($contentEncryptionKey, $nonce, $payload, "");
89+
list($encryptedText, $tag) = \AESGCM\AESGCM::encrypt($contentEncryptionKey, $nonce, $payload, '');
8890
} else {
8991
$encryptedText = openssl_encrypt($payload, 'aes-128-gcm', $contentEncryptionKey, OPENSSL_RAW_DATA, $nonce, $tag); // base 64 encoded
9092
}
@@ -98,7 +100,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
98100
}
99101

100102
/**
101-
* HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
103+
* HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
102104
*
103105
* This is used to derive a secure encryption key from a mostly-secure shared
104106
* secret.
@@ -114,6 +116,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
114116
* @param $ikm string Input keying material
115117
* @param $info string Application-specific context
116118
* @param $length int The length (in bytes) of the required output key
119+
*
117120
* @return string
118121
*/
119122
private static function hkdf($salt, $ikm, $info, $length)
@@ -129,11 +132,13 @@ private static function hkdf($salt, $ikm, $info, $length)
129132
* Creates a context for deriving encyption parameters.
130133
* See section 4.2 of
131134
* {@link https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-00}
132-
* From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}
135+
* From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}.
133136
*
134137
* @param $clientPublicKey string The client's public key
135138
* @param $serverPublicKey string Our public key
139+
*
136140
* @return string
141+
*
137142
* @throws \ErrorException
138143
*/
139144
private static function createContext($clientPublicKey, $serverPublicKey)
@@ -155,14 +160,17 @@ private static function createContext($clientPublicKey, $serverPublicKey)
155160
/**
156161
* Returns an info record. See sections 3.2 and 3.3 of
157162
* {@link https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-00}
158-
* From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}
163+
* From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}.
159164
*
160165
* @param $type string The type of the info record
161166
* @param $context string The context for the record
167+
*
162168
* @return string
169+
*
163170
* @throws \ErrorException
164171
*/
165-
private static function createInfo($type, $context) {
172+
private static function createInfo($type, $context)
173+
{
166174
if (Utils::safe_strlen($context) !== 135) {
167175
throw new \ErrorException('Context argument has invalid size');
168176
}

src/Notification.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Notification
2525
/** @var string */
2626
private $userAuthToken;
2727

28-
/** @var array Options : TTL, urgency, topic **/
28+
/** @var array Options : TTL, urgency, topic * */
2929
private $options;
3030

3131
public function __construct($endpoint, $payload, $userPublicKey, $userAuthToken, $options)
@@ -71,16 +71,16 @@ public function getUserAuthToken()
7171

7272
/**
7373
* @param array $defaultOptions
74+
*
7475
* @return array
7576
*/
7677
public function getOptions(array $defaultOptions = array())
7778
{
7879
$options = $this->options;
79-
$options['TTL'] = array_key_exists('TTL', $options) ? $options['TTL'] : $defaultOptions['TTL'];
80+
$options['TTL'] = array_key_exists('TTL', $options) ? $options['TTL'] : $defaultOptions['TTL'];
8081
$options['urgency'] = array_key_exists('urgency', $options) ? $options['urgency'] : $defaultOptions['urgency'];
8182
$options['topic'] = array_key_exists('topic', $options) ? $options['topic'] : $defaultOptions['topic'];
8283

8384
return $options;
8485
}
85-
8686
}

src/Utils.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
class Utils
1515
{
16-
public static function safe_strlen($string) {
17-
return mb_strlen($string, "8bit");
16+
public static function safe_strlen($string)
17+
{
18+
return mb_strlen($string, '8bit');
1819
}
1920
}

src/VAPID.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Base64Url\Base64Url;
1515
use Jose\Factory\JWKFactory;
1616
use Jose\Factory\JWSFactory;
17-
use Jose\Object\JWK;
18-
use Mdanter\Ecc\Curves\NistCurve;
1917
use Mdanter\Ecc\EccFactory;
2018
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer;
2119
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer;

tests/VAPIDTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the WebPush library.
5+
*
6+
* (c) Louis Lagrange <[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+
use Minishlink\WebPush\VAPID;
13+
14+
class VAPIDTest extends PHPUnit_Framework_TestCase
15+
{
16+
public function vapidProvider()
17+
{
18+
return array(
19+
array(
20+
'http://push.com',
21+
array(
22+
'subject' => 'http://test.com',
23+
'publicKey' => 'BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
24+
'privateKey' => '-3CdhFOqjzixgAbUSa0Zv9zi-dwDVmWO7672aBxSFPQ',
25+
),
26+
'1475452165',
27+
'WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJodHRwOi8vcHVzaC5jb20iLCJleHAiOjE0NzU0NTIxNjUsInN1YiI6Imh0dHA6Ly90ZXN0LmNvbSJ9.4F3ZKjeru4P9XM20rHPNvGBcr9zxhz8_ViyNfe11_xcuy7A9y7KfEPt6yuNikyW7eT9zYYD5mQZubDGa-5H2cA',
28+
'p256ecdsa=BA6jvk34k6YjElHQ6S0oZwmrsqHdCNajxcod6KJnI77Dagikfb--O_kYXcR2eflRz6l3PcI2r8fPCH3BElLQHDk',
29+
),
30+
);
31+
}
32+
33+
/**
34+
* @dataProvider vapidProvider
35+
*
36+
* @param $audience
37+
* @param $vapid
38+
* @param $expiration
39+
* @param $expectedAuthorization
40+
* @param $expectedCryptoKey
41+
*/
42+
public function testGetVapidHeaders($audience, $vapid, $expiration, $expectedAuthorization, $expectedCryptoKey)
43+
{
44+
$vapid = VAPID::validate($vapid);
45+
$headers = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $expiration);
46+
47+
$this->assertArrayHasKey('Authorization', $headers);
48+
$this->assertEquals($expectedAuthorization, $headers['Authorization']);
49+
$this->assertArrayHasKey('Crypto-Key', $headers);
50+
$this->assertEquals($expectedCryptoKey, $headers['Crypto-Key']);
51+
}
52+
}

0 commit comments

Comments
 (0)