jwt-decode
Advanced tools
Comparing version 4.0.0-beta.1 to 4.0.0-beta.2
@@ -1,2 +0,2 @@ | ||
function e(e){let t=e.replace(/-/g,"+").replace(/_/g,"/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw new Error("base64 string is not of the correct length")}try{return function(e){return decodeURIComponent(atob(e).replace(/(.)/g,(function(e,t){let r=t.charCodeAt(0).toString(16).toUpperCase();return r.length<2&&(r="0"+r),"%"+r})))}(t)}catch(e){return atob(t)}}class t extends Error{constructor(e){super(e)}}function r(r,n={header:!1}){if("string"!=typeof r)throw new t("Invalid token specified: must be a string");const a=!0===(n=n||{}).header?0:1,o=r.split(".")[a];if("string"!=typeof o)throw new t("Invalid token specified: missing part #"+(a+1));let i;try{i=e(o)}catch(e){throw new t("Invalid token specified: invalid base64 for part #"+(a+1)+" ("+e.message+")")}try{return JSON.parse(i)}catch(e){throw new t("Invalid token specified: invalid json for part #"+(a+1)+" ("+e.message+")")}}t.prototype.name="InvalidTokenError";export{t as InvalidTokenError,r as default}; | ||
function e(e){let t=e.replace(/-/g,"+").replace(/_/g,"/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw new Error("base64 string is not of the correct length")}try{return function(e){return decodeURIComponent(atob(e).replace(/(.)/g,(function(e,t){let r=t.charCodeAt(0).toString(16).toUpperCase();return r.length<2&&(r="0"+r),"%"+r})))}(t)}catch(e){return atob(t)}}class t extends Error{constructor(e){super(e)}}function r(r,n){if("string"!=typeof r)throw new t("Invalid token specified: must be a string");const o=!0===(n=n||{}).header?0:1,a=r.split(".")[o];if("string"!=typeof a)throw new t("Invalid token specified: missing part #"+(o+1));let i;try{i=e(a)}catch(e){throw new t("Invalid token specified: invalid base64 for part #"+(o+1)+" ("+e.message+")")}try{return JSON.parse(i)}catch(e){throw new t("Invalid token specified: invalid json for part #"+(o+1)+" ("+e.message+")")}}t.prototype.name="InvalidTokenError";export{t as InvalidTokenError,r as jwtDecode}; | ||
//# sourceMappingURL=jwt-decode.js.map |
@@ -16,3 +16,3 @@ (function (global, factory) { | ||
} | ||
function base64_url_decode (str) { | ||
function base64UrlDecode(str) { | ||
let output = str.replace(/-/g, "+").replace(/_/g, "/"); | ||
@@ -45,3 +45,3 @@ switch (output.length % 4) { | ||
InvalidTokenError.prototype.name = "InvalidTokenError"; | ||
function jwtDecode(token, options = { header: false }) { | ||
function jwtDecode(token, options) { | ||
if (typeof token !== "string") { | ||
@@ -58,3 +58,3 @@ throw new InvalidTokenError("Invalid token specified: must be a string"); | ||
try { | ||
decoded = base64_url_decode(part); | ||
decoded = base64UrlDecode(part); | ||
} | ||
@@ -61,0 +61,0 @@ catch (e) { |
122
package.json
{ | ||
"name": "jwt-decode", | ||
"version": "4.0.0-beta.1", | ||
"description": "Decode JWT tokens, mostly useful for browser applications.", | ||
"main": "build/cjs/jwt-decode.js", | ||
"module": "build/esm/jwt-decode.js", | ||
"types": "build/typings/index.d.ts", | ||
"keywords": [ | ||
"jwt", | ||
"browser" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/auth0/jwt-decode" | ||
}, | ||
"url": "https://github.com/auth0/jwt-decode/issues", | ||
"homepage": "https://github.com/auth0/jwt-decode#readme", | ||
"scripts": { | ||
"dev": "rollup --sourcemap --config --configPlugin typescript", | ||
"build": "rimraf build && rollup --sourcemap --config --configPlugin typescript --environment NODE_ENV:production", | ||
"postbuild": "echo '{\"type\": \"commonjs\"}'> build/cjs/package.json", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:node": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage", | ||
"test:browser": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage --testEnvironment=jsdom", | ||
"prepack": "npm run build" | ||
}, | ||
"author": "Jose F. Romaniello <jfromaniello@gmail.com>", | ||
"contributors": [ | ||
"Sam Bellen <sam.bellen@auth0.com>" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"@rollup/plugin-typescript": "^11.1.2", | ||
"jest": "^29.6.1", | ||
"jest-environment-jsdom": "^29.6.1", | ||
"jest-junit": "^16.0.0", | ||
"rimraf": "^5.0.1", | ||
"rollup": "^3.26.3", | ||
"rollup-plugin-livereload": "^2.0.5", | ||
"rollup-plugin-serve": "^2.0.2", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.1.6" | ||
}, | ||
"files": [ | ||
"build" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"require": "./build/cjs/jwt-decode.js", | ||
"import": "./build/esm/jwt-decode.js", | ||
"types": "./build/typings/index.d.ts" | ||
} | ||
"name": "jwt-decode", | ||
"version": "4.0.0-beta.2", | ||
"description": "Decode JWT tokens, mostly useful for browser applications.", | ||
"type": "module", | ||
"main": "build/cjs/jwt-decode.cjs", | ||
"module": "build/esm/jwt-decode.js", | ||
"types": "build/cjs/jwt-decode.d.cts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"default": "./build/esm/jwt-decode.js", | ||
"types": "./build/esm/jwt-decode.d.ts" | ||
}, | ||
"require": { | ||
"default": "./build/cjs/jwt-decode.cjs", | ||
"types": "./build/cjs/jwt-decode.d.cts" | ||
} | ||
} | ||
}, | ||
"keywords": [ | ||
"jwt", | ||
"browser" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/auth0/jwt-decode" | ||
}, | ||
"url": "https://github.com/auth0/jwt-decode/issues", | ||
"homepage": "https://github.com/auth0/jwt-decode#readme", | ||
"scripts": { | ||
"dev": "rollup --sourcemap --config --configPlugin typescript", | ||
"build": "rimraf build && rollup --sourcemap --config --configPlugin typescript --environment NODE_ENV:production && npm run generate-types", | ||
"generate-types": "npm run generate-types:es && npm run generate-types:cjs", | ||
"generate-types:es": "tsup --entry.jwt-decode lib/index.ts --out-dir build/esm --dts-only --format esm", | ||
"generate-types:cjs": "tsup --entry.jwt-decode lib/index.ts --out-dir build/cjs --dts-only --format cjs", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:node": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage", | ||
"test:browser": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --coverage --testEnvironment=jsdom", | ||
"prepack": "npm run build" | ||
}, | ||
"author": "Jose F. Romaniello <jfromaniello@gmail.com>", | ||
"contributors": [ | ||
"Sam Bellen <sam.bellen@auth0.com>" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"@rollup/plugin-typescript": "^11.1.2", | ||
"jest": "^29.6.1", | ||
"jest-environment-jsdom": "^29.6.1", | ||
"jest-junit": "^16.0.0", | ||
"rimraf": "^5.0.1", | ||
"rollup": "^3.26.3", | ||
"rollup-plugin-livereload": "^2.0.5", | ||
"rollup-plugin-serve": "^2.0.2", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"tsup": "^7.1.0", | ||
"typescript": "^5.1.6" | ||
}, | ||
"files": [ | ||
"build" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
} | ||
} |
@@ -27,6 +27,6 @@ ![Browser library that helps decoding JWT tokens which are Base64Url encoded](https://cdn.auth0.com/website/sdks/banners/jwt-decode-banner.png) | ||
```js | ||
import jwt_decode from "jwt-decode"; | ||
import { jwtDecode } from "jwt-decode"; | ||
var token = "eyJ0eXAiO.../// jwt token"; | ||
var decoded = jwt_decode(token); | ||
const token = "eyJ0eXAiO.../// jwt token"; | ||
const decoded = jwtDecode(token); | ||
@@ -44,3 +44,3 @@ console.log(decoded); | ||
// decode header by passing in options (useful for when you need `kid` to verify a JWT): | ||
var decodedHeader = jwt_decode(token, { header: true }); | ||
const decodedHeader = jwtDecode(token, { header: true }); | ||
console.log(decodedHeader); | ||
@@ -74,6 +74,6 @@ | ||
#### Use with typescript | ||
#### Use with TypeScript | ||
The return type of the `jwt_decode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`. | ||
If needed, you can specify what the expected return type should be by passing a type argument to the `jwt_decode` function. | ||
The return type of the `jwtDecode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`. | ||
If needed, you can specify what the expected return type should be by passing a type argument to the `jwtDecode` function. | ||
@@ -83,5 +83,5 @@ You can extend both `JwtHeader` and `JwtPayload` to include non-standard claims or properties. | ||
```typescript | ||
import jwtDecode from "jwt-decode"; | ||
import { jwtDecode } from "jwt-decode"; | ||
const token: string = "eyJhsw5c"; | ||
const token = "eyJhsw5c"; | ||
const decoded = jwtDecode<JwtPayload>(token); // Returns with the JwtPayload type | ||
@@ -93,3 +93,3 @@ ``` | ||
```javascript | ||
const jwt_decode = require('jwt-decode'); | ||
const { jwtDecode } = require('jwt-decode'); | ||
... | ||
@@ -100,3 +100,3 @@ ``` | ||
Copy the file `jwt-decode.js` from the `build/` folder to your project somewhere, then include it like so: | ||
Copy the file `jwt-decode.js` from the root of the `build/` folder to your project somewhere, then include it like so: | ||
@@ -107,3 +107,18 @@ ```html | ||
Once this script has loaded, the `jwt_decode` function will be exposed as a global: | ||
```javascript | ||
const token = "eyJhsw5c"; | ||
const decoded = jwt_decode(token); | ||
``` | ||
Alternatively, if you are using the [Asynchronous Module Definition (AMD) API](https://github.com/amdjs/amdjs-api/blob/master/AMD.md), you can load the same function as follows: | ||
```javascript | ||
define(["jwt_decode"], (jwtDecode) => { | ||
const token = "eyJhsw5c"; | ||
const decoded = jwtDecode(token); | ||
}); | ||
``` | ||
## Feedback | ||
@@ -110,0 +125,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
146
20204
13
11
111