title: Verify JWT signature with RSA public key in PHP
body: <p>In PHP, I'm trying to validate an AWS auth token (JWT returned from getOpenIdTokenForDeveloperIdentity) using the AWS's RSA public key (which I generated from modulus/exponent at <a href="https://cognito-identity.amazonaws.com/.well-known/jwks_uri" rel="nofollow noreferrer">https://cognito-identity.amazonaws.com/.well-known/jwks_uri</a>). The key begins with the appropriate headers/footers <code>-----BEGIN RSA PUBLIC KEY-----</code> etc. I've looked at a few PHP libraries like <code>Emarref\Jwt\Jwt</code>, however I get the error: <code>error:0906D06C:PEM routines:PEM_read_bio:no start line</code>. It all boils down to the basic php function: <code>openssl_verify</code>.</p>
  
  <p>I've looked at the php.net/manual for openssl-verify, but I'm still not clear on the parameter details. The algorithm needed is <code>RS512</code>.</p>
  
  <p>I am able to verify the JWT token using node.js with no problems (same key and token). For that I used the library: <a href="https://github.com/auth0/node-jsonwebtoken" rel="nofollow noreferrer">https://github.com/auth0/node-jsonwebtoken</a></p>
  
  <p>Not sure why this doesn't work in PHP. Can I not use an RSA Public Key?  </p>
  
  <pre><code>function verifyKey($public_key) {
    $jwt = new Emarref\Jwt\Jwt();
  
    $algorithm = new Emarref\Jwt\Algorithm\Rs512();
    $factory = new Emarref\Jwt\Encryption\Factory();
    $encryption = $factory-&gt;create($algorithm);
    $encryption-&gt;setPublicKey($public_key);
    $context = new Emarref\Jwt\Verification\Context($encryption);
    $token = $jwt-&gt;deserialize($authToken);
  
    try {
      $jwt-&gt;verify($token, $context);
    } catch (Emarref\Jwt\Exception\VerificationException $e) {
      debug($e-&gt;getMessage());
    }
  }
  </code></pre>