Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a correct apns-topic value based on a push type #101

Merged
merged 5 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/AuthProvider/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,30 @@ public function authenticateClient(Request $request)
]
);
$request->addHeaders([
"apns-topic" => $this->appBundleId
'apns-topic' => $this->generateApnsTopic($request->getHeaders()['apns-push-type']),
]);
}

/**
* Generate a correct apns-topic string
*
* @param string $pushType
* @return string
*/
public function generateApnsTopic(string $pushType)
{
switch ($pushType) {
case 'voip':
return $this->appBundleId . '.voip';

case 'complication':
return $this->appBundleId . '.complication';

case 'fileprovider':
return $this->appBundleId . '.pushkit.fileprovider';

default:
return $this->appBundleId;
}
}
}
27 changes: 25 additions & 2 deletions src/AuthProvider/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,34 @@ public static function useExisting(string $tokenString, array $options): Token
public function authenticateClient(Request $request)
{
$request->addHeaders([
"apns-topic" => $this->appBundleId,
'Authorization' => 'bearer ' . $this->token
'apns-topic' => $this->generateApnsTopic($request->getHeaders()['apns-push-type']),
edamov marked this conversation as resolved.
Show resolved Hide resolved
'Authorization' => 'bearer ' . $this->token,
]);
}

/**
* Generate a correct apns-topic string
*
* @param string $pushType
* @return string
*/
public function generateApnsTopic(string $pushType)
{
switch ($pushType) {
case 'voip':
return $this->appBundleId . '.voip';

case 'complication':
return $this->appBundleId . '.complication';

case 'fileprovider':
return $this->appBundleId . '.pushkit.fileprovider';

default:
return $this->appBundleId;
}
}

/**
* Get last generated token.
*
Expand Down
58 changes: 46 additions & 12 deletions tests/AuthProvider/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,69 @@ class CertificateTest extends TestCase
{
public function testCreatingCertificateAuthProvider()
{
$options = [];
$options['certificate_path'] = __DIR__ . '/../files/certificate.pem';
$options['certificate_secret'] = 'secret';
$options = $this->getOptions();
$authProvider = Certificate::create($options);

$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
}

public function testCreatingCertificateAuthProviderWithAppBundleId()
public function testAuthenticatingClient()
{
$options = [];
$options['certificate_path'] = __DIR__ . '/../files/certificate.pem';
$options['certificate_secret'] = 'secret';
$options['app_bundle_id'] = 'com.apple.test';
$options = $this->getOptions();
$authProvider = Certificate::create($options);

$request = $this->createRequest();
$authProvider->authenticateClient($request);

$this->assertSame($request->getOptions()[CURLOPT_SSLCERT], $options['certificate_path']);
$this->assertSame($request->getOptions()[CURLOPT_SSLCERTPASSWD], $options['certificate_secret']);
$this->assertSame($request->getOptions()[CURLOPT_SSLCERTPASSWD], $options['certificate_secret']);
}

public function testVoipApnsTopic()
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);

$request = $this->createRequest('voip');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.voip');
}

public function testComplicationApnsTopic()
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);

$this->assertSame($request->getHeaders()["apns-topic"], $options['app_bundle_id']);
$request = $this->createRequest('complication');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.complication');
}

public function testFileproviderApnsTopic()
{
$options = $this->getOptions();
$authProvider = Certificate::create($options);

$request = $this->createRequest('fileprovider');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.pushkit.fileprovider');
}

private function getOptions()
{
return [
'certificate_path' => __DIR__ . '/../files/certificate.pem',
'certificate_secret' => 'secret',
'app_bundle_id' => 'com.apple.test',
];
}

private function createRequest(): Request
private function createRequest(string $pushType = 'alert'): Request
{
$notification = new Notification(Payload::create(), '123');
$notification = new Notification(Payload::create()->setPushType($pushType), '123');
$request = new Request($notification, $production = false);

return $request;
Expand Down
78 changes: 61 additions & 17 deletions tests/AuthProvider/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,30 @@
namespace Pushok\AuthProvider;

use PHPUnit\Framework\TestCase;
use Pushok\AuthProvider;
use Pushok\AuthProviderInterface;
use Pushok\Notification;
use Pushok\Payload;
use Pushok\Request;

class TokenTest extends TestCase
{
public function testCreatingTokenAuthProvider()
public function testCreatingTokenAuthProviderWithKeyPath()
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
$authProvider = AuthProvider\Token::create($options);
$authProvider = Token::create($options);

$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
$this->assertTrue(is_string($authProvider->get()));
}

private function getOptions()
{
return [
'key_id' => '1234567890',
'team_id' => '1234567890',
'app_bundle_id' => 'com.app.Test',
];
}

public function testCreatingTokenAuthProviderWithKeyContent()
{
$options = $this->getOptions();

$options['private_key_content'] = file_get_contents(
implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'files', 'private_key.p8'])
);

$authProvider = AuthProvider\Token::create($options);
$authProvider = Token::create($options);

$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
$this->assertTrue(is_string($authProvider->get()));
Expand All @@ -55,7 +46,7 @@ public function testCreatingTokenAuthProviderWithoutKey()
$this->expectException(\InvalidArgumentException::class);

$options = $this->getOptions();
AuthProvider\Token::create($options);
Token::create($options);
}

public function testUseExistingToken()
Expand All @@ -64,9 +55,62 @@ public function testUseExistingToken()
'YxR8Hw--Hp8YH8RF2QDiwOjmGhTC_7g2DpbnzKQZ8Sj20-q12LrLrAMafcuxf97CTHl9hCVere0vYrzcLmGV-A';

$options = $this->getOptions();
$authProvider = AuthProvider\Token::useExisting($token, $options);
$authProvider = Token::useExisting($token, $options);

$this->assertInstanceOf(AuthProviderInterface::class, $authProvider);
$this->assertEquals($token, $authProvider->get());
}

public function testVoipApnsTopic()
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
$authProvider = Token::create($options);

$request = $this->createRequest('voip');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.voip');
}

public function testComplicationApnsTopic()
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
$authProvider = Token::create($options);

$request = $this->createRequest('complication');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.complication');
}

public function testFileproviderApnsTopic()
{
$options = $this->getOptions();
$options['private_key_path'] = __DIR__ . '/../files/private_key.p8';
$authProvider = Token::create($options);

$request = $this->createRequest('fileprovider');
$authProvider->authenticateClient($request);

$this->assertSame($request->getHeaders()['apns-topic'], $options['app_bundle_id'] . '.pushkit.fileprovider');
}

private function getOptions()
{
return [
'key_id' => '1234567890',
'team_id' => '1234567890',
'app_bundle_id' => 'com.app.Test',
];
}

private function createRequest(string $pushType = 'alert'): Request
{
$notification = new Notification(Payload::create()->setPushType($pushType), '123');
$request = new Request($notification, $production = false);

return $request;
}
}