Last active
August 27, 2020 15:39
-
-
Save ricardo-dlc/51fb6569bfe3a889cc32bcec9298bdee to your computer and use it in GitHub Desktop.
Revisions
-
ricardo-dlc revised this gist
Aug 27, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ First you need to generate a pair of keys. Generate the private as follows: openssl genrsa -out privatekey.pem 4096 ``` Then generate the public one using the private generated previously: ```console openssl req -new -x509 -key privatekey.pem -out publickey.cer -
ricardo-dlc revised this gist
May 23, 2020 . 4 changed files with 42 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,11 @@ const jose = require('jose'); const fs = require('fs'); const path = require("path"); // project // ├── src // │ └── app.js // ├── package.json // └── privatekey.pub // └── publickey.cer const privateKey = jose.JWK.asKey(fs.readFileSync(path.resolve(__dirname, '../privatekey.pem'))); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,2 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ First you need to generate a pair of keys. Generate the private as follows: ```console openssl genrsa -out privatekey.pem 4096 ``` Then generate the public one using the private generated previuosly: ```console openssl req -new -x509 -key privatekey.pem -out publickey.cer ``` Then proceed to install node dependencies, execute the following in project root directory: ```console npm install ``` Finally run: ```console npm start ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ { "name": "use-of-jose", "version": "1.0.0", "description": "Use of JOSE library and private/public keys", "main": "src/app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start" "node src/app" }, "author": "Ricardo de la Cruz <[email protected]>", "license": "ISC", "dependencies": { "jose": "^1.27.0" } } -
ricardo-dlc created this gist
May 3, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ const jose = require('jose'); const fs = require('fs'); const path = require("path"); // main // ├── src // │ └── app.js // ├── privatekey.pub // └── publickey.cer const privateKey = jose.JWK.asKey(fs.readFileSync(path.resolve(__dirname, '../privatekey.pem'))); const publicKey = jose.JWK.asKey(fs.readFileSync(path.resolve(__dirname, '../publickey.cer'))); // Sign let jwt = jose.JWT.sign( { 'urn:example:claim': 'foo' }, privateKey, { algorithm: 'RS512', expiresIn: '1 min', header: { typ: 'JWT' }, audience: 'urn:example:client_id', issuer: 'https://op.example.com' } ); try { // Verify JWT and also verify payload content let payload = jose.JWT.verify( jwt, publicKey, { issuer: 'https://op.example.com', audience: 'urn:example:client_id' } ); console.log(payload); } catch (err) { console.log(err); if (err instanceof jose.errors.JOSEError && err.code === 'ERR_JWT_EXPIRED') { console.log('Expired token'); } else if (err instanceof jose.errors.JOSEError && err.code === 'ERR_JWT_MALFORMED') { console.log('Invalid token'); } else if (err instanceof jose.errors.JOSEError && err.code === 'ERR_JWT_CLAIM_INVALID') { console.log('Claim invalid'); } else { console.log('Unexpected error'); } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ openssl genrsa -out privatekey.pem 4096 openssl req -new -x509 -key privatekey.pem -out publickey.cer