ZhukV/AppleApnPush

JWT authentication refresh token

Tayfun74 opened this issue · 8 comments

Is there a way to save the current JWT somewhere and use it for at least the next 20 minutes. Apple says the following in their documentation APNs reports an error if you recreate your tokens more than once every 20 minutes. (https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns).

I did not find a way to do this directly, outside of implementing the AuthenticatorInterface and implementing the logic myself.

Any ideas?

ZhukV commented

You can create a decorator with use any additional storage (file system, redis, database, etc...) and control update process in it.

@Tayfun74 any update? I am having the same issue

ZhukV commented

@Tayfun74 , @axeljeremy7 , if it critical for you, I can add decorator in next days with use symfony/cache with implement psr/cache. But, your's application support it?

@axeljeremy7 I was thinking of implementing a decorator as @ZhukV said, but didn't get time to do it. We are using Symfony @ZhukV so our application would support it, if you got time to add a decorator that would be really nice.

@Tayfun74 @ZhukV I understand the decorator approach but I dont think i have access to the token so it can be stored, nevertheless I have found a weird trick or maybe i can be wrong, but when i write like this $authenticator = new JwtAuthenticator($jwt, new DateInterval('PT25M'), null); , I don’t longer have errors of to many updates provider tokens, so this seems like a hack, I did debug the debug the code behind and by change of jwsLifetime the following function seems to work fine public function authenticate(Request $request), it seems that new DateInterval('PT25M') does the trick, nevertheless l would like to know your opinion and if you can try this.

ZhukV commented

Added cache functionality in v3.1.0 release (#70).

use Apple\ApnPush\Jwt\SignatureGenerator\CacheJwtSignatureGenerator;
use Apple\ApnPush\Jwt\SignatureGenerator\SignatureGeneratorFactory;

$tokenGenerator = SignatureGeneratorFactory::resolve();
$cache = new AnyPsr16Cache();
$generator = new CacheJwtSignatureGenerator($tokenGenerator, $cache);

// Third (cache key generator) and fourth (TTL) arguments are optional.
ZhukV commented

@Tayfun74 , @axeljeremy7 , can you test on your environment?

ok i will test but i have this set up, tested

           $jwt = new Jwt(self::TEAM_ID, self::KEY_ID, self::PRIVATE_KEY_PATH);
           $authenticator = new JwtAuthenticator($jwt, new DateInterval('PT25M'), null);
           $builder = new Http20Builder($authenticator);
 // cache test
            $tokenGenerator = SignatureGeneratorFactory::resolve();
            $store = new Apc();
            $cache = new SimpleCache($store);
            $generator = new CacheJwtSignatureGenerator($tokenGenerator, $cache, null, new DateInterval('PT25M'));

            // Create authenticator system
            $jwt = new Jwt(self::TEAM_ID, self::KEY_ID, self::PRIVATE_KEY_PATH);
            $authenticator = new JwtAuthenticator($jwt, null, $generator);
            $builder = new Http20Builder($authenticator);
            $sender = $builder->build();

Also i try it , seems to work but i have to make modifications to the library since the key contains invalid characters with this string as "apn:jwt:R4243T6GH2" and the ":" cause an issue with the class, i can fork to make it pass but i dont know if is ok for your library or MatthiasMullie library .


use DateInterval;
use DateTime;
use MatthiasMullie\Scrapbook\KeyValueStore;
use Psr\SimpleCache\CacheInterface;
use Traversable;

/**
 * @author Matthias Mullie <scrapbook@mullie.eu>
 * @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved
 * @license LICENSE MIT
 */
class SimpleCache implements CacheInterface
{
    /**
     * List of invalid (or reserved) key characters.
     *
     * @var string
     */
    const KEY_INVALID_CHARACTERS = '{}()/\@:';``