Skip to content
Create and verify uPort and DID compliant JWT's in Javascript
TypeScript HTML JavaScript
Branch: develop
Clone or download
Latest commit 0abaf4c Oct 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci ci: add npm install to the release script (#47) Aug 26, 2019
.dependabot ci: add dependabot configuration (#53) Sep 6, 2019
.github/ISSUE_TEMPLATE Add issue templates Nov 30, 2018
docs update a few things in docs Feb 23, 2019
src feat: require resolver to be passed as a parameter to verifyJWT (#57) Sep 6, 2019
.eslintrc.js fixed a few things in eslint config Feb 20, 2019
.gitignore [WIP] set up typescript build process and convert js filetypes to ts May 1, 2019
.npmignore Initial commit of new JWT library Feb 24, 2018
.prettierrc [WIP] set up typescript build process and convert js filetypes to ts May 1, 2019
.releaserc chore: add release automation configuration (#44) Aug 15, 2019
LICENSE Initial commit Feb 24, 2018
README.md docs: update readme Oct 23, 2019
_config.yml Initial commit of new JWT library Feb 24, 2018
codecov.yml upgrade circleci to v2 Aug 16, 2018
issue-template.md Initial commit of new JWT library Feb 24, 2018
js2doc.conf Structural updates for documentation May 23, 2018
package-lock.json chore(deps): bump lodash from 4.17.11 to 4.17.15 Sep 6, 2019
package.json feat: require resolver to be passed as a parameter to verifyJWT (#57) Sep 6, 2019
template.hbs update a few things in docs Feb 23, 2019
tsconfig.json [WIP] set up typescript build process and convert js filetypes to ts May 1, 2019
tslint.json fix more codacy formatting issues May 8, 2019
webpack.config.js fix webpack config May 3, 2019

README.md

did-jwt

Join the chat at npm npm Twitter Follow

Algorithms supported | DID Public Key Types | Claim Specification

The did-JWT library allows you to sign and verify JSON Web Tokens (JWT) using ES256K, ES256K-R and Ed25519 algorithms.

Public keys are resolved using the Decentralized ID (DID) of the signing identity of the claim, which is passed as the iss attribute of the encoded JWT.

DID methods

We currently support the following DID methods:

You will need to install each one you need to support. See each method for how to configure it.

Support for other DID methods should be simple. Write a DID resolver supporting the `did-resolver' interface. Once you've verified that it works, please add a PR adding it to the above list so people can find it.

If your DID method requires a different signing algorithm than what is already supported, please create a PR.

Installation

npm install did-jwt

or if you use yarn

yarn add did-jwt

Example

1. Create a did-JWT

createJWT

In practice you should secure the key passed to SimpleSigner. The key provided in code below is for informational purposes; you will need to create an application identity at My Apps or use our uport-credentials library to generate an ethereum key pair.

const didJWT = require('did-jwt')
const signer = didJWT.SimpleSigner('278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f');

let jwt = '';
didJWT.createJWT({aud: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74', exp: 1957463421, name: 'uPort Developer'},
                 {alg: 'ES256K-R', issuer: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74', signer}).then( response =>
                 { jwt = response });

console.log(jwt);

2. Decode a did-JWT

Try decoding the JWT. You can also do this using jwt.io

//pass the jwt from step 1
let decoded = didJWT.decodeJWT(jwt)
console.log(decoded)

Once decoded a did-JWT will resemble:

{
  header: { typ: 'JWT', alg: 'ES256K-R' },
  payload: {
    iat: 1571692233,
    exp: 1957463421,
    aud: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74',
    name: 'uPort Developer',
    iss: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74'
  },
  signature: 'kkSmdNE9Xbiql_KCg3IptuJotm08pSEeCOICBCN_4YcgyzFc4wIfBdDQcz76eE-z7xUR3IBb6-r-lRfSJcHMiAA',
  data: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NzE2OTIyMzMsImV4cCI6MTk1NzQ2MzQyMSwiYXVkIjoiZGlkOmV0aHI6MHhmM2JlYWMzMGM0OThkOWUyNjg2NWYzNGZjYWE1N2RiYjkzNWIwZDc0IiwibmFtZSI6InVQb3J0IERldmVsb3BlciIsImlzcyI6ImRpZDpldGhyOjB4ZjNiZWFjMzBjNDk4ZDllMjY4NjVmMzRmY2FhNTdkYmI5MzViMGQ3NCJ9'
}

2. Verify a did-JWT

verifyJWT

You ned to provide a did-resolver for the verify function. For this example we will use ethr-did, but there are other methods available above. For more information on configuring the Resolver object please see did-resolver

npm install ethr-did-resolver
const Resolver = require('did-resolver')
const ethrDid =  require('ethr-did-resolver').getResolver()

let resolver = new Resolver.Resolver(ethrDid)

let verifiedRespone = {};
// pass the JWT from step 1 & 2
didJWT.verifyJWT(jwt, {resolver: resolver, audience: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74'}).then((response) =>
{ verifiedRespone = response });

console.log(verifiedRespone);

A verified did-JWT returns an object resembling:

{
  payload: {
    iat: 1571692448,
    exp: 1957463421,
    aud: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74',
    name: 'uPort Developer',
    iss: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74'
  },
  doc: {
    '@context': 'https://w3id.org/did/v1',
    id: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74',
    publicKey: [ [Object] ],
    authentication: [ [Object] ]
  },
  issuer: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74',
  signer: {
    id: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#owner',
    type: 'Secp256k1VerificationKey2018',
    owner: 'did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74',
    ethereumAddress: '0xf3beac30c498d9e26865f34fcaa57dbb935b0d74'
  },
  jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NzE2OTI0NDgsImV4cCI6MTk1NzQ2MzQyMSwiYXVkIjoiZGlkOmV0aHI6MHhmM2JlYWMzMGM0OThkOWUyNjg2NWYzNGZjYWE1N2RiYjkzNWIwZDc0IiwibmFtZSI6InVQb3J0IERldmVsb3BlciIsImlzcyI6ImRpZDpldGhyOjB4ZjNiZWFjMzBjNDk4ZDllMjY4NjVmMzRmY2FhNTdkYmI5MzViMGQ3NCJ9.xd_CSWukS6rK8y7GVvyH_c5yRsDXojM6BuKaf1ZMg0fsgpSBioS7jBfyk4ZZvS0iuFu4u4_771_PNWvmsvaZQQE'
}
You can’t perform that action at this time.