Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare beta version #151

Merged
merged 35 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fa904b3
Integrate our automated release setup (#152)
frederikprijck Jul 26, 2023
e2f8592
feat: add support for node atob
frederikprijck Jul 12, 2023
aa1b35d
feat: drop atob polyfill
frederikprijck Jul 12, 2023
46d9d6a
feat: node 14 is not supported anymore
frederikprijck Jul 12, 2023
4416d98
chore: run CI on node 20
frederikprijck Jul 12, 2023
355f73d
feat: support node > 16
frederikprijck Jul 12, 2023
604d058
feat: add support for package.json's exports field
frederikprijck Jul 12, 2023
428b639
chore: migrate mocha to jest for running tests
frederikprijck Jul 6, 2023
ca1ff8c
feat: migrate to TypeScript
frederikprijck Jul 20, 2023
6fa3d3a
fix: fix CJS and ESM support
frederikprijck Jul 20, 2023
68c6b37
fix: fix jest tests for ESM
frederikprijck Jul 20, 2023
bae4df1
docs: update README
frederikprijck Jul 20, 2023
27c2f61
feat: cleanup UMD entry file
frederikprijck Jul 22, 2023
af8cf2c
fix: revert changing build directory to dist
frederikprijck Jul 22, 2023
9d79b78
chore: simplify strict typescript configuration
frederikprijck Jul 26, 2023
02c26e3
chore: update test description
frederikprijck Jul 26, 2023
181d185
fix: avoid using var
frederikprijck Jul 26, 2023
dad9a0d
chore: update package.json to remove incorrect author website
frederikprijck Jul 26, 2023
5dd5c61
Remove Babel and related dependencies (#155)
jonkoops Jul 26, 2023
0c3d975
Replace `.npmignore` with `files` field (#156)
jonkoops Jul 26, 2023
c23530a
Use named Jest imports rather than globals (#158)
jonkoops Jul 26, 2023
bbc7c6b
Remove unused Rollup dependencies (#159)
jonkoops Jul 26, 2023
74adeba
chore: bump dependencies
frederikprijck Jul 26, 2023
51de539
chore: update to rollup 3
frederikprijck Jul 26, 2023
c313744
chire: rebuild UMD bundle
frederikprijck Jul 26, 2023
0fca977
Convert Rollup configuration to TypeScript (#161)
jonkoops Jul 27, 2023
3ac7786
Replace `atob` module with default global (#162)
jonkoops Jul 27, 2023
fec39de
Remove unused `uglify-js` dependency (#160)
jonkoops Jul 27, 2023
d22ce75
fix: move sourcemap setting to rollup to avoid warning
frederikprijck Jul 27, 2023
e0f262b
chore: regenerate UMD bundle
frederikprijck Jul 27, 2023
cdc720c
Use official TypeScript plugin for Rollup (#164)
jonkoops Jul 27, 2023
f9d4c8f
Convert Jest configuration to TypeScript (#163)
jonkoops Jul 27, 2023
ecb03fb
chore: bring back JwtPayload generic in TypeScript sample
frederikprijck Jul 27, 2023
aca5435
chore: remove build artifacts
frederikprijck Jul 28, 2023
51354b3
fix: avoid excluding umd bundle from gitignore
frederikprijck Jul 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
version: 2.1
orbs:
ship: auth0/ship@0
jobs:
build:
parameters:
Expand All @@ -21,12 +23,25 @@ jobs:
name: Build
command: npm run build
- run:
name: Run Tests
command: npm run test
name: Run Tests against browser
command: npm run test:browser
- run:
name: Run Tests against node
command: npm run test:node
workflows:
build-and-test:
jobs:
- build:
matrix:
parameters:
node-version: ["14.20", "16.20", "18.16"]
node-version: ["16.20", "18.16", "20.4"]
- ship/node-publish:
context:
- publish-npm
- publish-gh
filters:
branches:
only:
- master
requires:
- build
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ lib-cov
pids
logs
results
test-results

npm-debug.log
node_modules
build/*
!build/jwt-decode.js
coverage/*
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Browser library that helps decoding JWT tokens which are Base64Url encoded](https://cdn.auth0.com/website/sdks/banners/jwt-decode-banner.png)

**IMPORTANT:** This library doesn't validate the token, any well formed JWT can be decoded. You should validate the token in your server-side logic by using something like [express-jwt](https://github.com/auth0/express-jwt), [koa-jwt](https://github.com/stiang/koa-jwt), [Owin Bearer JWT](https://github.com/michaelnoonan/Auth0-Owin-JwtBearerAuthentication), etc.
**IMPORTANT:** This library doesn't validate the token, any well-formed JWT can be decoded. You should validate the token in your server-side logic by using something like [express-jwt](https://github.com/auth0/express-jwt), [koa-jwt](https://github.com/stiang/koa-jwt), [Microsoft.AspNetCore.Authentication.JwtBearer](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer), etc.

![Release](https://img.shields.io/npm/v/jwt-decode)
![Downloads](https://img.shields.io/npm/dw/jwt-decode)
Expand Down Expand Up @@ -67,16 +67,17 @@ Not adhering to the format will result in a `InvalidTokenError` with one of the
- `Invalid token specified: must be a string` => the token passed was not a string, this library only works on strings.
- `Invalid token specified: missing part #` => this probably means you are missing a dot (`.`) in the token
- `Invalid token specified: invalid base64 for part #` => the part could not be base64 decoded (the message should contain the error the base64 decoder gave)
- `Invalid token specified: invalid json for part #` => the part was correctly base64 decoded, however the decoded value was not valid json (the message should contain the error the json parser gave)
- `Invalid token specified: invalid json for part #` => the part was correctly base64 decoded, however, the decoded value was not valid JSON (the message should contain the error the JSON parser gave)

#### Use with typescript

The `jwt_decode` function will return an `unknown` type by default. 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 `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 package also exports types for a `JwtHeader` and `JwtPayload` with some default claims. You can either use them as-is, or extend them to include non standard claims or properties.
You can extend both `JwtHeader` and `JwtPayload` to include non-standard claims or properties.

```typescript
import jwtDecode, { JwtPayload } from "jwt-decode";
import jwtDecode from "jwt-decode";

const token: string = "eyJhsw5c";
const decoded = jwtDecode<JwtPayload>(token); // Returns with the JwtPayload type
Expand All @@ -91,7 +92,7 @@ const jwt_decode = require('jwt-decode');

#### Include with a script tag
frederikprijck marked this conversation as resolved.
Show resolved Hide resolved

Copy the file `jwt-decode.js` from the `build/` folder to your project somewhere, then include like so:
Copy the file `jwt-decode.js` from the `build/` folder to your project somewhere, then include it like so:

```html
<script src="jwt-decode.js"></script>
Expand Down
135 changes: 0 additions & 135 deletions build/jwt-decode.js

This file was deleted.

25 changes: 25 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type JestConfigWithTsJest } from "ts-jest";

const jestConfig: JestConfigWithTsJest = {
rootDir: "./",
moduleFileExtensions: ["ts", "js"],
testMatch: ["**/test/**/*.test.ts", "**/test/tests.ts"],
coverageProvider: "v8",
coveragePathIgnorePatterns: ["/node_modules/", "./test"],
extensionsToTreatAsEsm: [".ts"],
reporters: [
"default",
["jest-junit", { outputDirectory: "test-results/jest" }],
],
coverageReporters: ["lcov", "text", "text-summary"],
setupFiles: [],
preset: "ts-jest/presets/default-esm",
transform: {
"^.+\\.ts?$": [
"ts-jest",
{ useESM: true, tsconfig: "./tsconfig.test.json" },
],
},
};

export default jestConfig;
45 changes: 0 additions & 45 deletions lib/atob.js

This file was deleted.

10 changes: 4 additions & 6 deletions lib/base64_url_decode.js → lib/base64_url_decode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import atob from "./atob";

function b64DecodeUnicode(str) {
function b64DecodeUnicode(str: string) {
return decodeURIComponent(
atob(str).replace(/(.)/g, function(m, p) {
var code = p.charCodeAt(0).toString(16).toUpperCase();
let code = p.charCodeAt(0).toString(16).toUpperCase();
if (code.length < 2) {
code = "0" + code;
}
Expand All @@ -12,8 +10,8 @@ function b64DecodeUnicode(str) {
);
}

export default function(str) {
var output = str.replace(/-/g, "+").replace(/_/g, "/");
export default function(str: string) {
let output = str.replace(/-/g, "+").replace(/_/g, "/");
switch (output.length % 4) {
case 0:
break;
Expand Down
7 changes: 0 additions & 7 deletions index.d.ts → lib/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export class InvalidTokenError extends Error {}

export interface JwtDecodeOptions {
header?: boolean;
}
Expand All @@ -19,8 +17,3 @@ export interface JwtPayload {
iat?: number;
jti?: string;
}

export default function jwtDecode<T = unknown>(
token: string,
options?: JwtDecodeOptions
): T;
2 changes: 1 addition & 1 deletion lib/index.cjs.js → lib/index.cjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jwtDecode, { InvalidTokenError } from "./index";

const wrapper = jwtDecode;
const wrapper = jwtDecode as any;
wrapper.default = jwtDecode;
wrapper.InvalidTokenError = InvalidTokenError;
export default wrapper;
frederikprijck marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 0 additions & 36 deletions lib/index.js

This file was deleted.

Loading