PageSourceSearch

https://elastos-wiki.netlify.app/assets/js/ed526b4e.5b113e49.js

js elastos-wiki.netlify.app collected 2026-10-03 10:45:08 UTC 8,818 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[8756],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>g});var r=n(7294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function s(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},l=Object.keys(e);for(r=0;r<l.length;r++)n=l[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(r=0;r<l.length;r++)n=l[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var o=r.createContext({}),d=function(e){var t=r.useContext(o),n=t;return e&&(n="function"==typeof e?e(t):s(s({},t),e)),n},c=function(e){var t=d(e.components);return r.createElement(o.Provider,{value:t},e.children)},u="mdxType",p={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,l=e.originalType,o=e.parentName,c=i(e,["components","mdxType","originalType","parentName"]),u=d(n),m=a,g=u["".concat(o,".").concat(m)]||u[m]||p[m]||l;return n?r.createElement(g,s(s({ref:t},c),{},{components:n})):r.createElement(g,s({ref:t},c))}));function g(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var l=n.length,s=new Array(l);s[0]=m;var i={};for(var o in t)hasOwnProperty.call(t,o)&&(i[o]=t[o]);i.originalType=e,i[u]="string"==typeof e?e:a,s[1]=i;for(var d=2;d<l;d++)s[d]=n[d];return r.createElement.apply(null,s)}return r.createElement.apply(null,n)}m.displayName="MDXCreateElement"},9465:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>o,default:()=>g,frontMatter:()=>i,metadata:()=>d,toc:()=>u});var r=n(7462),a=n(3366),l=(n(7294),n(3905)),s=["components"],i={title:"JSON Web Token"},o=void 0,d={unversionedId:"sdk/did/js/guide/json",id:"sdk/did/js/guide/json",title:"JSON Web Token",description:"Create JWT",source:"@site/../docs/7.sdk/did/js/guide/json.md",sourceDirName:"7.sdk/did/js/guide",slug:"/sdk/did/js/guide/json",permalink:"/sdk/did/js/guide/json",draft:!1,editUrl:"https://github.com/elastos/Elastos.Wiki/edit/master/website/../docs/7.sdk/did/js/guide/json.md",tags:[],version:"current",lastUpdatedBy:"racollette",lastUpdatedAt:1677260880,formattedLastUpdatedAt:"Feb 24, 2023",frontMatter:{title:"JSON Web Token"},sidebar:"sdkSidebar",previous:{title:"DID",permalink:"/sdk/did/js/guide/did"},next:{title:"Credential and Presentation",permalink:"/sdk/did/js/guide/credential"}},c={},u=[{value:"Create JWT",id:"create-jwt",level:2},{value:"Example",id:"example",level:4},{value:"Usage",id:"usage",level:4},{value:"Verify and Use JWT",id:"verify-and-use-jwt",level:2},{value:"Example",id:"example-1",level:4},{value:"Usage",id:"usage-1",level:4}],p={toc:u},m="wrapper";function g(e){var t=e.components,n=(0,a.Z)(e,s);return(0,l.kt)(m,(0,r.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("h2",{id:"create-jwt"}
1,"Create JWT"),(0,l.kt)("p",null,"JWT content is compiled by JWTBuilder, which is divided into two parts: JWTHeader and Claims, which are filled separately. Finally, JWT or JWS Token String is obtained according to whether they are encapsulated and signed or not."),(0,l.kt)("h4",{id:"example"},"Example"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},'//create jwt header\nlet h = JWTBuilder.createHeader();\nh.setType(JWTHeader.JWT_TYPE).setContentType("json");\nh.put("library", "Elastos DID");\nh.put("version", "1.0");\n\nlet cal = dayjs();\nlet iat = cal.unix();\nlet nbf = cal.add(-1, "month").unix();\nlet exp = cal.add(4, "month").unix();\n\n//create jwt claims\nlet b = JWTBuilder.createClaims();\nb.setSubject("JwtTest")\n  .setId("0")\n  .setAudience("Test cases")\n  .setIssuedAt(iat)\n  .setExpiration(exp)\n  .setNotBefore(nbf)\n  .put("foo", "bar");\n\n//get jwt token\nlet token = doc.jwtBuilder().setHeader(h).setClaims(b).compact();\n\nif (token) console.log("create jwt token successfully.");\nelse console.log("create jwt token failed.");\n')),(0,l.kt)("h4",{id:"usage"},"Usage"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public jwtBuilder(): JWTBuilder\uff1b\n")),(0,l.kt)("p",null,"For better understanding, this method is introduced in the JWT module. The DID document offers a method to create JWTBuilder and get the compiled mode of JWT."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public static createHeader(): JWTHeader\uff1b\n")),(0,l.kt)("p",null,"This method creates an empty JWTHeader, and the API document can be referred to for adding elements."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public static createClaims(): Claims\uff1b\n")),(0,l.kt)("p",null,"This method creates empty claims, and the API document can be referred to for adding elements."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public setHeader(header: JWTHeader): JWTBuilder\uff1b\n")),(0,l.kt)("p",null,"This method fills header into JWTBuilder."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public setClaims(claims: Claims): JWTBuilder\uff1b\n")),(0,l.kt)("p",null,"This method fills claims into JWTBuilder."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public async sign(\n    password: string,\n    keyid: string = null // the authentication key in the DID document that creates JWTBuilder\n): Promise<string>\uff1b\n")),(0,l.kt)("p",null,"This method signs the content of JWTBuilder and generates JWT string."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public compact(): string\uff1b\n")),(0,l.kt)("p",null,"This method encapsulates the content in JWTBuilder and generates JWT string."),(0,l.kt)("h2",{id:"verify-and-use-jwt"},"Verify and Use JWT"),(0,l.kt)("p",null,"The SDK provides a method to analyze the type of JWToken (JWT/JWS). If JWS does the corresponding post-verification parsing, both JWT and JWS token string will convert JWT."),(0,l.kt)("p",null,"JWT provides a multitude of methods to obtain JWT elements for the usage and reference of users."),(0,l.kt)("h4",{id:"example-1"},"Example"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},'let jpb = doc.jwtParserBuilder();
1\nlet jp = jpb.requireSubject(doc.getSubject()).build();\nlet jwt =  await jp.parse(token);\nexpect(jwt).not.toBeNull();\nh = jwt.getHeader();\n//get each elem of head\nconsole.log("get header type: {}", h.getType());\n... ... ... ...\n//get each elem of body\nlet c = jwt.getBody();\nconsole.log("get issuer: {}", c.getIssuer());\n... ... ... ...\n')),(0,l.kt)("h4",{id:"usage-1"},"Usage"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public jwtParserBuilder(): JWTParserBuilder;\n")),(0,l.kt)("p",null,"This method is provided by the DID document to obtain the JWTParserBuilder object. JWTParserBuilder enables users to set the filter options to check whether the token meets the requirements or not. The interfaces include requireSubject, requireAudience, requireIssuer, requireIssuedAt, requireAlgorithms, requireHeaderType, and setAllowedClockSkewSeconds. Refer to the API document for specifics."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public build(): JWTParser;\n")),(0,l.kt)("p",null,"This method is provided by JWTParserBuilder to encapsulate JWTParser."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public async parse(token: string): Promise<JWT>\uff1b\n")),(0,l.kt)("p",null,"This method is provided by JWTParser to parse the token. If succeeds, return JWT object; otherwise, an error is returned."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public getHeader(): JWTHeader\uff1b\n")),(0,l.kt)("p",null,"This method is offered by JWT to get the content of JWT header."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-ts"},"public getBody(): Claims\uff1b\n")),(0,l.kt)("p",null,"This method is provided by JWT to get the content of JWT header body."))}g.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.