Skip to content

Instantly share code, notes, and snippets.

@ricardo-dlc
Last active August 27, 2020 15:39
Show Gist options
  • Select an option

  • Save ricardo-dlc/51fb6569bfe3a889cc32bcec9298bdee to your computer and use it in GitHub Desktop.

Select an option

Save ricardo-dlc/51fb6569bfe3a889cc32bcec9298bdee to your computer and use it in GitHub Desktop.

Revisions

  1. ricardo-dlc revised this gist Aug 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion instructions.md
    Original 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 previuosly:
    Then generate the public one using the private generated previously:

    ```console
    openssl req -new -x509 -key privatekey.pem -out publickey.cer
  2. ricardo-dlc revised this gist May 23, 2020. 4 changed files with 42 additions and 4 deletions.
    5 changes: 3 additions & 2 deletions app.js
    Original 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");

    // main
    // project
    // ├── src
    // │ └── app.js
    // ├── privatekey.pub
    // ├── package.json
    // └── privatekey.pub
    // └── publickey.cer

    const privateKey = jose.JWK.asKey(fs.readFileSync(path.resolve(__dirname, '../privatekey.pem')));
    2 changes: 0 additions & 2 deletions create-private-public-key.txt
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    openssl genrsa -out privatekey.pem 4096
    openssl req -new -x509 -key privatekey.pem -out publickey.cer
    24 changes: 24 additions & 0 deletions instructions.md
    Original 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
    ```
    15 changes: 15 additions & 0 deletions package.json
    Original 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"
    }
    }
  3. ricardo-dlc created this gist May 3, 2020.
    54 changes: 54 additions & 0 deletions app.js
    Original 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');
    }
    };
    2 changes: 2 additions & 0 deletions create-private-public-key.txt
    Original 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