namshi / jose

JSON Object Signing and Encryption library for PHP.
MIT License
1.8k stars 133 forks source link

Generate JWT for Apple Push Notification #108

Closed edamov closed 5 years ago

edamov commented 8 years ago

I want to use jose library for generating JWT for Apple Push Notification. But I don't know how to implement it with this library. There are some requirements for generating this token: 1) Headers should be:

{
    "alg": "ES256",
    "kid": "ABC123DEFG"
}

2) Payload data should be:

{
    "iss": "DEF123GHIJ",
    "iat": 1437179036
 }

3) APNs supports only provider authentication tokens that are signed with the ES256 algorithm.

So could you please provide an example of generating JWT token for Apple Push Notifications with you library?

YaniceSC commented 7 years ago

Hi @edamov, did you find a solution ? thanks.

edamov commented 7 years ago

@YaniceSC Yes, I did. I found another library Spomky-Labs/jose. Here you can find an example. Also I've started to develop the library for sending push notifications - edamov/pushok

YaniceSC commented 7 years ago

Great! I will have look then. thanks.

NikitaKharkov commented 6 years ago

You can create OpenSSL key with any function (or manually by bash link how to do this), and then use next functions code:

    $key = openssl_pkey_get_private(file_get_contents($this->client->getContainer()->getParameter('jwt_secret_private')));

    $jws = new JWS(['alg' => 'RS256'], 'OpenSSL');

    $payload = [
        'roles' => $permissions,
        'username' => 'Vasya',
        'userId' => 8,
        'email' => 'test1@test.com',
        'exp' => time() + 3360,
        'iat' => time(),
    ];

    $jws->setPayload($payload);
    $jws->sign($key);
    var_dump($jws->getTokenString());