JSON Web Tokens (JWT) verify/sign implementation using W3C Web Cryptography (crypto.subtle).
The following browsers are supported without shims: IE TP, Firefox 35+ and Chrome 37+. Safari is not currently working. For more information about compatiblity check web cryptography browser support.
With npm:
npminstallwebcrypto-jwtor with bower:
bowerinstallwebcrypto-jwtand then add the following script tag:
<scriptsrc="webcrypto-jwt/index.js"></script>JWT verification and decoding:
varjwt=require('webcrypto-jwt');// token signed using 'secret' as secretvartoken='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.'+'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.'+'TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';jwt.verifyJWT(token,'secret','HS256',function(err,isValid){console.log(isValid);// true});jwt.verifyJWT(token,'nosecret','HS256',function(err,isValid){console.log(isValid);// false});jwt.decodeJWT(token);// '{"sub":"1234567890","name":"John Doe","admin":true}'jwt.parseJWT(token)// Object {sub: "1234567890", name: "John Doe", admin: true}JWT signing:
varsignJWT=require('webcrypto-jwt').signJWT;signJWT({foo: 'bar'},'secret','HS256',function(err,token){// ey...console.log(token);});