Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Tuesday, May 16, 2017

Simple JavaScript Web Token (JWT) Signing and Validation


.


<script src="https://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js"></script>
<script>
 // JWS signing
sJWT = KJUR.jws.JWS.sign(null, '{"alg":"HS256"}', '{"name": "user"}', {"utf8": "password"});
console.log("Signing JSON Web Token":+sJWT);

// JWT validation
isValid = KJUR.jws.JWS.verifyJWT(sJWT, {"utf8": "password"}, {alg: ["HS256"]});
console.log("Validate Signature:"+isValid);

var headerObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(sJWT.split(".")[0]));
console.log("*** Header ***");
console.log("Parsing Header:"+headerObj);

var payloadObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(sJWT.split(".")[1]));
console.log("*** Payload ***");
console.log("Parsing Payload:"+payloadObj);
</script>


.
CODEPEN CLIENT EXAMPLE: https://codepen.io/aronrodrigues/pen/YXvBWW
.
SLIDES: http://kjur.github.io/jsrsasign/slide_e/#/
.
REFERENCE:
-https://kjur.github.io/jsrsasign/api/symbols/KJUR.jws.JWS.html
-https://www.jonathan-petitcolas.com/2014/11/27/creating-json-web-token-in-javascript.html

No comments:

Post a Comment