unjwt 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,8 +6,8 @@ You may just use [Jose](https://github.com/panva/jose) or one of the other libra
6
6
 
7
7
  ## Features
8
8
 
9
- - 💨 Zero dependencies
10
- - 📦 Works in every context supporting the Web Crypto API
9
+ - 📦 Works in Node, browser and workers
10
+ - 💨 Zero dependencies except [`uncrypto`](https://github.com/unjs/uncrypto) for conditional Web Crypto API support
11
11
 
12
12
  ## Installation
13
13
 
package/dist/index.d.mts CHANGED
@@ -47,7 +47,7 @@ interface JWTRegisteredClaims {
47
47
  declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
48
48
  declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
49
49
 
50
- declare function signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, signatureMethod, hashMethod, }: {
50
+ declare function signJWT<T extends Record<string, any> = Record<string, any>>(options: {
51
51
  payload?: T;
52
52
  secret: string;
53
53
  issuer: string;
@@ -63,7 +63,7 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
63
63
  hashMethod?: string;
64
64
  }): Promise<string>;
65
65
 
66
- declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureMethod, hashMethod, }: {
66
+ declare function verifyJWT(options: {
67
67
  token: string;
68
68
  secret: string;
69
69
  issuer: string;
package/dist/index.d.ts CHANGED
@@ -47,7 +47,7 @@ interface JWTRegisteredClaims {
47
47
  declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
48
48
  declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
49
49
 
50
- declare function signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, signatureMethod, hashMethod, }: {
50
+ declare function signJWT<T extends Record<string, any> = Record<string, any>>(options: {
51
51
  payload?: T;
52
52
  secret: string;
53
53
  issuer: string;
@@ -63,7 +63,7 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
63
63
  hashMethod?: string;
64
64
  }): Promise<string>;
65
65
 
66
- declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureMethod, hashMethod, }: {
66
+ declare function verifyJWT(options: {
67
67
  token: string;
68
68
  secret: string;
69
69
  issuer: string;
package/dist/index.mjs CHANGED
@@ -1,19 +1,22 @@
1
+ import { subtle } from 'uncrypto';
2
+
1
3
  const DEFAULT_SIGNATURE_METHOD = "HMAC";
2
4
  const DEFAULT_HASH_METHODS = "SHA-256";
3
5
  const textEncoder = new TextEncoder();
4
6
  async function createSignature(key, algorithm, data) {
5
7
  const encoded = textEncoder.encode(data);
6
- const signature = await crypto.subtle.sign(algorithm, key, encoded);
8
+ const signature = await subtle.sign(algorithm, key, encoded);
7
9
  return encodeToBase64Url(signature);
8
10
  }
9
- async function importKey({
10
- secret,
11
- usage = "sign",
12
- name = DEFAULT_SIGNATURE_METHOD,
13
- hash = DEFAULT_HASH_METHODS
14
- }) {
11
+ async function importKey(options) {
12
+ const {
13
+ secret,
14
+ usage = "sign",
15
+ name = DEFAULT_SIGNATURE_METHOD,
16
+ hash = DEFAULT_HASH_METHODS
17
+ } = { ...options };
15
18
  const encodedKey = textEncoder.encode(secret);
16
- return await crypto.subtle.importKey(
19
+ return await subtle.importKey(
17
20
  "raw",
18
21
  encodedKey,
19
22
  { name, hash },
@@ -61,15 +64,16 @@ const headerAlgMap = {
61
64
  RSA: "RS",
62
65
  ECDSA: "ES"
63
66
  };
64
- async function signJWT({
65
- payload = {},
66
- secret,
67
- issuer,
68
- audience,
69
- expires = 30,
70
- signatureMethod = DEFAULT_SIGNATURE_METHOD,
71
- hashMethod = DEFAULT_HASH_METHODS
72
- }) {
67
+ async function signJWT(options) {
68
+ const {
69
+ payload = {},
70
+ secret,
71
+ issuer,
72
+ audience,
73
+ expires = 30,
74
+ signatureMethod = DEFAULT_SIGNATURE_METHOD,
75
+ hashMethod = DEFAULT_HASH_METHODS
76
+ } = { ...options };
73
77
  const key = await importKey({
74
78
  secret,
75
79
  name: signatureMethod,
@@ -98,23 +102,24 @@ async function signJWT({
98
102
  )}.${signature}`;
99
103
  }
100
104
 
101
- async function verifyJWT({
102
- token,
103
- secret,
104
- issuer,
105
- audience,
106
- leeway = 60,
107
- signatureMethod = DEFAULT_SIGNATURE_METHOD,
108
- hashMethod = DEFAULT_HASH_METHODS
109
- }) {
110
- const [header, payload, signature] = token.split(".");
105
+ async function verifyJWT(options) {
106
+ const {
107
+ token,
108
+ secret,
109
+ issuer,
110
+ audience,
111
+ leeway = 60,
112
+ signatureMethod = DEFAULT_SIGNATURE_METHOD,
113
+ hashMethod = DEFAULT_HASH_METHODS
114
+ } = { ...options };
115
+ const [header, payload, signature] = String(token).split(".");
111
116
  const key = await importKey({
112
117
  secret,
113
118
  usage: "verify",
114
119
  name: signatureMethod,
115
120
  hash: hashMethod
116
121
  });
117
- const isValid = await crypto.subtle.verify(
122
+ const isValid = await subtle.verify(
118
123
  signatureMethod,
119
124
  key,
120
125
  decodeFromBase64UrlToBuffer(signature),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unjwt",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.2.0",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "Easy creation and verification of JSON Web Tokens (JWT)",
7
7
  "author": "Johann Schopplich <pkg@johannschopplich.com>",
@@ -40,6 +40,9 @@
40
40
  "release": "bumpp --commit --push --tag",
41
41
  "test:types": "tsc --noEmit"
42
42
  },
43
+ "dependencies": {
44
+ "uncrypto": "^0.1.3"
45
+ },
43
46
  "devDependencies": {
44
47
  "@antfu/eslint-config": "^0.40.2",
45
48
  "bumpp": "^9.1.1",