ZhukV/AppleApnPush

Message: Cannot resolve available JWT Signature Generator

ahartman opened this issue · 8 comments

I am trying to use your apn-push package in CakePHP 3.6 with PHP 7.2.1

Following use declarations:
use Apple\ApnPush\Certificate\Certificate;
use Apple\ApnPush\Protocol\Http\Authenticator\CertificateAuthenticator;
use Apple\ApnPush\Sender\Builder\Http20Builder;
use Apple\ApnPush\Jwt\Jwt;
use Apple\ApnPush\Protocol\Http\Authenticator\JwtAuthenticator;

Following code snippet:
$authFile = 'AuthKey_JXKR3NB68G.p8';
$root = WWW_ROOT;
$path = "{$root}/files/{$authFile}";

	$jwt = new Jwt('V2LV7426WZ', 'JXKR3NB68G', $path);
	Log::write('notice',$jwt);
	$authenticator = new JwtAuthenticator($jwt);

The last line result in the message in the title.
I know teamIdentifier, keyID and .p8 file are good since I use them in a Swift server successfully.
Any ideas?

Kind regards, André Hartman, Belgium

ZhukV commented

Please provide the message of error.

Please find attached:

  1. log.txt with contents ofCakePHP error.log
  2. OrdersTable.php with sendNotification (the old way, works fine) and sendNotification1 (uses your package produces error).
  3. phpInfo.pdf

Could it be that my php 7.2.1 misses some extensions?
Hope you can help resolving this, thanks in advance.
André Hartman, Belgium

OrdersTable.php.txt
phpInfo.pdf
error.txt

ZhukV commented

Previously, you should know, what the method Sender::send can throw the exception if we receive error from Apple Apn Push service, and we must correct work in this case.

try {
    $sender->send($receiver, $notification);
    Log::write('info', 'Success send the push notification to device.');
} catch (\Apple\ApnPush\Exception\SendNotification\SendNotificationException  $e) {
    Log::write('error', sprintf(
        'Fail to send the push notification to device with error: %s',
        $e->getMessage()
    ));
}

Secondary, from your PHPInfo, I do no see what you system have cURL with HTTP/2 protocol.
The JWT token requires HTTP/2 protocol.

Thank your for your quick reaction.
I try AMPSS which should contain Apache 2.4 and HTTP/2.
I will get back to you on this.
Kind regards, André Hartman

I received this error also after installation, luckily I had played around with other packages before, including the spomky-labs/jose package.

Install the spomky-labs/jose and it should (if its the same problem I had) get rid of this error :)

Says on the token instructions to install the required package, however doesn't specify which one. After looking at the SignatureGeneratorFactory class I noticed it was using the Jose package.

Maybe add what package to install if the user wants to use JWT method

I have the same problem, but I do not understand why, my curl version support HTTP/2 or not?

curl --version curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.0g zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3 Release-Date: 2018-01-24 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL

thank you

I had same problem, I solved it by creating my own AppleSignatureGenerator class :

class AppleSignatureGenerator implements SignatureGeneratorInterface {

    /**
     * Generate the signature for JSON Web Token
     *
     * @param JwtInterface $jwt
     *
     * @return string
     */
    public function generate(JwtInterface $jwt): string
    {
        // do not generate anything actually : this is the job of refresh_apple_token.php which has access (and only it)
        // to the key to generate the token
        if($tokenString = file_get_contents("AppleAuthTokenNotif_".APPLE_KEY_NAME.".token"))
            return $tokenString;
        else
            trigger_error("Error : Unable to access Apple PushNotif Token file.", E_USER_ERROR);
    }
}

Used liked this :

 $jwt = new Jwt(APPLE_TEAM_ID, APPLE_KEY_NAME, APPLE_KEYAUTH_PATH);
 $signGenerator = new AppleSignatureGenerator();
 $authenticator = new JwtAuthenticator($jwt, null, $signGenerator);

I refresh myself the token every 30 minutes as explained in Apple Push Notification Guide, by launching a cron job that encrypt a new token and store it in a file, using the provided key Apple gave me

@jrm0695 thank you vut can you maybe help me with something?

i have this

`
$jwt = new Jwt('3KYB9D64M2', '2V44WT9693', '/var/www/backup/AuthKey_2V44AT9693.p8');
$signGenerator = new AppleSignatureGenerator();
$authenticator = new JwtAuthenticator($jwt, null, $signGenerator);

	$builder = new Http20Builder($authenticator);

	$protocol = $builder->buildProtocol();
	$sender = new Sender($protocol);


    $notification = Notification::createWithBody('Hello ;)');
	$receiver = new Receiver(new DeviceToken('06fdb7614fbd785956c50871390f18ace5d04c3e2860a77b905423f5bd84e0eb'), 'Wiro-Technologies.delimsysd');


	$sender->send($receiver, $notification);`

and this

`<?php

namespace App\Service;

use Apple\ApnPush\Jwt\JwtInterface;
use Apple\ApnPush\Jwt\SignatureGenerator;
use Apple\ApnPush\Jwt\SignatureGenerator\SignatureGeneratorInterface;
/**

  • AppleSignatureGenerator
    */
    class AppleSignatureGenerator implements SignatureGeneratorInterface {

    /**

    • Generate the signature for JSON Web Token
    • @param JwtInterface $jwt
    • @return string
      */
      public function generate(JwtInterface $jwt): string
      {
      // do not generate anything actually : this is the job of refresh_apple_token.php which has access (and only it)
      // to the key to generate the token
      if($tokenString = file_get_contents("AppleAuthTokenNotif_2V44AT9693.token"))
      return $tokenString;
      else
      trigger_error("Error : Unable to access Apple PushNotif Token file.", E_USER_ERROR);
      }
      }
      `

but i get

"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "Invalid provider token."

please help me thank you