|
1 |
| ---- |
2 |
| -title: "DID JWT" |
3 |
| -index: 3 |
4 |
| -category: "did-jwt" |
5 |
| -type: "reference" |
6 |
| ---- |
7 |
| - |
8 |
| -## Algorithms supported |
9 |
| - |
10 |
| -- `ES256K` the [secp256k1 ECDSA curve](https://en.bitcoin.it/wiki/Secp256k1) |
11 |
| -- `ES256K-R` the [secp256k1 ECDSA curve](https://en.bitcoin.it/wiki/Secp256k1) with recovery parameter |
12 |
| - |
13 |
| -## DID PublicKey Types |
14 |
| - |
15 |
| -The `PublicKey` section of a DID document contains one or more Public Keys. We support the following types: |
16 |
| - |
17 |
| -Name | Encoding | Algorithm's |
18 |
| ----- | -------- | ----------- |
19 |
| -`Secp256k1SignatureVerificationKey2018` | `publicKeyHex` | `ES256K`, `ES256K-R` |
20 |
| -`Secp256k1VerificationKey2018` | `publicKeyHex` | `ES256K`, `ES256K-R` |
21 |
| -`Secp256k1VerificationKey2018` | `ethereumAddress` | `ES256K-R` |
22 |
| - |
23 |
| -## Claims |
24 |
| - |
25 |
| -Name | Description | Required |
26 |
| ----- | ----------- | -------- |
27 |
| -[`iss`](https://tools.ietf.org/html/rfc7519#section-4.1.1) | The [DID](https://w3c-ccg.github.io/did-spec/) of the signing identity| yes |
28 |
| -[`sub`](https://tools.ietf.org/html/rfc7519#section-4.1.2) | The [DID](https://w3c-ccg.github.io/did-spec/) of the subject of the JWT| no |
29 |
| -[`aud`](https://tools.ietf.org/html/rfc7519#section-4.1.3) | The [DID](https://w3c-ccg.github.io/did-spec/) or URL of the audience of the JWT. Our libraries or app will not accept any JWT that has someone else as the audience| no |
30 |
| -[`iat`](https://tools.ietf.org/html/rfc7519#section-4.1.6) | The time of issuance | yes |
31 |
| -[`exp`](https://tools.ietf.org/html/rfc7519#section-4.1.4) | Expiration time of JWT | no |
32 |
| - |
33 |
| -## Modules |
34 |
| - |
35 |
| -<dl> |
36 |
| -<dt><a href="#module_did-jwt/JWT">did-jwt/JWT</a></dt> |
37 |
| -<dd></dd> |
38 |
| -</dl> |
39 |
| - |
40 |
| -## Functions |
41 |
| - |
42 |
| -<dl> |
43 |
| -<dt><a href="#SimpleSigner">SimpleSigner(hexPrivateKey)</a> ⇒ <code>function</code></dt> |
44 |
| -<dd><p>The SimpleSigner returns a configured function for signing data. It also defines |
45 |
| - an interface that you can also implement yourself and use in our other modules.</p> |
46 |
| -</dd> |
47 |
| -</dl> |
48 |
| - |
49 |
| -<a name="module_did-jwt/JWT"></a> |
50 |
| - |
51 |
| -## did-jwt/JWT |
52 |
| - |
53 |
| -* [did-jwt/JWT](#module_did-jwt/JWT) |
54 |
| - * [.decodeJWT(jwt)](#module_did-jwt/JWT.decodeJWT) ⇒ <code>Object</code> |
55 |
| - * [.createJWT(payload, [options])](#module_did-jwt/JWT.createJWT) ⇒ <code>Promise.<Object, Error></code> |
56 |
| - * [.verifyJWT(jwt, [options])](#module_did-jwt/JWT.verifyJWT) ⇒ <code>Promise.<Object, Error></code> |
57 |
| - * [.resolveAuthenticator(alg, did, auth)](#module_did-jwt/JWT.resolveAuthenticator) ⇒ <code>Promise.<Object, Error></code> |
58 |
| - |
59 |
| -<a name="module_did-jwt/JWT.decodeJWT"></a> |
60 |
| - |
61 |
| -### did-jwt/JWT.decodeJWT(jwt) ⇒ <code>Object</code> |
62 |
| -Decodes a JWT and returns an object representing the payload |
63 |
| - |
64 |
| -**Kind**: static method of [<code>did-jwt/JWT</code>](#module_did-jwt/JWT) |
65 |
| -**Returns**: <code>Object</code> - a JS object representing the decoded JWT |
66 |
| - |
67 |
| -| Param | Type | Description | |
68 |
| -| --- | --- | --- | |
69 |
| -| jwt | <code>String</code> | a JSON Web Token to verify | |
70 |
| - |
71 |
| -**Example** |
72 |
| -```js |
73 |
| -decodeJWT('eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE1MjU5Mjc1MTcsImF1ZCI6ImRpZDp1cG9ydDoyb3NuZko0V3k3TEJBbTJuUEJYaXJlMVdmUW43NVJyVjZUcyIsImV4cCI6MTU1NzQ2MzQyMSwibmFtZSI6InVQb3J0IERldmVsb3BlciIsImlzcyI6ImRpZDp1cG9ydDoyb3NuZko0V3k3TEJBbTJuUEJYaXJlMVdmUW43NVJyVjZUcyJ9.R7owbvNZoL4ti5ec-Kpktb0datw9Y-FshHsF5R7cXuKaiGlQz1dcOOXbXTOb-wg7-30CDfchFERR6Yc8F61ymw') |
74 |
| - |
75 |
| - |
76 |
| -``` |
77 |
| -<a name="module_did-jwt/JWT.createJWT"></a> |
78 |
| - |
79 |
| -### did-jwt/JWT.createJWT(payload, [options]) ⇒ <code>Promise.<Object, Error></code> |
80 |
| -Creates a signed JWT given an address which becomes the issuer, a signer, and a payload for which the signature is over. |
81 |
| - |
82 |
| -**Kind**: static method of [<code>did-jwt/JWT</code>](#module_did-jwt/JWT) |
83 |
| -**Returns**: <code>Promise.<Object, Error></code> - a promise which resolves with a signed JSON Web Token or rejects with an error |
84 |
| - |
85 |
| -| Param | Type | Description | |
86 |
| -| --- | --- | --- | |
87 |
| -| payload | <code>Object</code> | payload object | |
88 |
| -| [options] | <code>Object</code> | an unsigned credential object | |
89 |
| -| options.issuer | <code>String</code> | The DID of the issuer (signer) of JWT | |
90 |
| -| options.alg | <code>String</code> | The JWT signing algorithm to use. Supports: [ES256K, ES256K-R], Defaults to: ES256K | |
91 |
| -| options.signer | [<code>SimpleSigner</code>](#SimpleSigner) | a signer, reference our SimpleSigner.js | |
92 |
| - |
93 |
| -**Example** |
94 |
| -```js |
95 |
| -const signer = SimpleSigner(process.env.PRIVATE_KEY) |
96 |
| - createJWT({address: '5A8bRWU3F7j3REx3vkJ...', signer}, {key1: 'value', key2: ..., ... }).then(jwt => { |
97 |
| - ... |
98 |
| - }) |
99 |
| - |
100 |
| - |
101 |
| -``` |
102 |
| -<a name="module_did-jwt/JWT.verifyJWT"></a> |
103 |
| - |
104 |
| -### did-jwt/JWT.verifyJWT(jwt, [options]) ⇒ <code>Promise.<Object, Error></code> |
105 |
| -Verifies given JWT. If the JWT is valid, the promise returns an object including the JWT, the payload of the JWT, |
106 |
| - and the did doc of the issuer of the JWT. |
107 |
| - |
108 |
| -**Kind**: static method of [<code>did-jwt/JWT</code>](#module_did-jwt/JWT) |
109 |
| -**Returns**: <code>Promise.<Object, Error></code> - a promise which resolves with a response object or rejects with an error |
110 |
| - |
111 |
| -| Param | Type | Description | |
112 |
| -| --- | --- | --- | |
113 |
| -| jwt | <code>String</code> | a JSON Web Token to verify | |
114 |
| -| [options] | <code>Object</code> | an unsigned credential object | |
115 |
| -| options.auth | <code>Boolean</code> | Require signer to be listed in the authentication section of the DID document (for Authentication purposes) | |
116 |
| -| options.audience | <code>String</code> | DID of the recipient of the JWT | |
117 |
| -| options.callbackUrl | <code>String</code> | callback url in JWT | |
118 |
| - |
119 |
| -**Example** |
120 |
| -```js |
121 |
| -verifyJWT('did:uport:eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJyZXF1Z....', {audience: '5A8bRWU3F7j3REx3vkJ...', callbackUrl: 'https://...'}).then(obj => { |
122 |
| - const did = obj.did // DID of signer |
123 |
| - const payload = obj.payload |
124 |
| - const doc = obj.doc // DID Document of signer |
125 |
| - const jwt = obj.jwt |
126 |
| - const signerKeyId = obj.signerKeyId // ID of key in DID document that signed JWT |
127 |
| - ... |
128 |
| - }) |
129 |
| - |
130 |
| - |
131 |
| -``` |
132 |
| -<a name="module_did-jwt/JWT.resolveAuthenticator"></a> |
133 |
| - |
134 |
| -### did-jwt/JWT.resolveAuthenticator(alg, did, auth) ⇒ <code>Promise.<Object, Error></code> |
135 |
| -Resolves relevant public keys or other authenticating material used to verify signature from the DID document of provided DID |
136 |
| - |
137 |
| -**Kind**: static method of [<code>did-jwt/JWT</code>](#module_did-jwt/JWT) |
138 |
| -**Returns**: <code>Promise.<Object, Error></code> - a promise which resolves with a response object containing an array of authenticators or if non exist rejects with an error |
139 |
| - |
140 |
| -| Param | Type | Description | |
141 |
| -| --- | --- | --- | |
142 |
| -| alg | <code>String</code> | a JWT algorithm | |
143 |
| -| did | <code>String</code> | a Decentralized IDentifier (DID) to lookup | |
144 |
| -| auth | <code>Boolean</code> | Restrict public keys to ones specifically listed in the 'authentication' section of DID document | |
145 |
| - |
146 |
| -**Example** |
147 |
| -```js |
148 |
| -resolveAuthenticator('ES256K', 'did:uport:2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX').then(obj => { |
149 |
| - const payload = obj.payload |
150 |
| - const profile = obj.profile |
151 |
| - const jwt = obj.jwt |
152 |
| - ... |
153 |
| - }) |
154 |
| - |
155 |
| - |
156 |
| -``` |
157 |
| -<a name="SimpleSigner"></a> |
158 |
| - |
159 |
| -## SimpleSigner(hexPrivateKey) ⇒ <code>function</code> |
160 |
| -The SimpleSigner returns a configured function for signing data. It also defines |
161 |
| - an interface that you can also implement yourself and use in our other modules. |
162 |
| - |
163 |
| -**Kind**: global function |
164 |
| -**Returns**: <code>function</code> - a configured signer function |
165 |
| - |
166 |
| -| Param | Type | Description | |
167 |
| -| --- | --- | --- | |
168 |
| -| hexPrivateKey | <code>String</code> | a hex encoded private key | |
169 |
| - |
170 |
| -**Example** |
171 |
| -```js |
172 |
| -const signer = SimpleSigner(process.env.PRIVATE_KEY) |
173 |
| - signer(data, (err, signature) => { |
174 |
| - ... |
175 |
| - }) |
176 |
| - |
177 |
| - |
178 |
| -``` |
0 commit comments