Skip to content
Advertisement

Generate a Paseto V2 Public Key/Token in PHP, Verify in Node.js

Preface:

What is Paseto?: https://developer.okta.com/blog/2019/10/17/a-thorough-introduction-to-paseto

  • I am using the PHP library for Paseto from here
  • I am using the Node.js Paseto lib from here

I have been able to successfully achieve the creation of a Paseto V1 token and corresponding public key using the PHP lib (with a RSA private key on the server side for keypair), and then use the public key to verify the given token on the Node.js side:

PHP Paseto Public V1:

JavaScript

NodeJS Paseto Public V1:

JavaScript

This works great, I can verify my claim in Node.js and process what I need using the ingested data.

Now if I try the following w/ V2, calling bin2hex() on the public key to be able to store it and use it on the Node.js side, I am unable to properly verify in Node.js. I believe it has something to do with the sodium crypto binary key generation, and how the $publicKey->encode() uses Base64UrlSafe::encodeUnpadded($this->key); but am not sure.. I don’t ever get the BEGIN PUBLIC KEY from the publicKey created using V2, as I believe it is just stored as binary?

PHP Paseto Public V2:

JavaScript

NodeJS Paseto Public V2:

JavaScript

I appreciate any feedback you may be able to provide. Please let me know if you have any additional questions. I tagged this with slim for the PHP framework, as I am using the PHP paseto lib within a slim project to store the public/private keys on my slim container, etc. and NodeJS within the context of a lambda.

Advertisement

Answer

In case anyone still needs an answer to this – using

JavaScript

is what worked for me.

So instead of using the crypto library’s createPublicKey method, use paseto V2’s bytesToKeyObject method to generate the key to feed into V2.verify.

JavaScript

In my case, I was using a public paseto scheme, where the signer is a Ruby on Rails app, and the verifier is a downstream node app.

In Ruby, I created the public/private key pair using mguymon’s paseto gem https://github.com/mguymon/paseto.rb (v2)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement