@drawbridge/drawbridge-utils 0.0.21 → 0.0.23

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.
@@ -0,0 +1,10 @@
1
+ // token.js
2
+ import crypto from "crypto";
3
+ var generate = (bytes = 32, encoding = "base64url") => {
4
+ const buf = crypto.randomBytes(bytes);
5
+ return encoding ? buf.toString(encoding) : buf;
6
+ };
7
+
8
+ export {
9
+ generate
10
+ };
@@ -1,9 +1,13 @@
1
+ import {
2
+ generate
3
+ } from "./chunk-HOA2JHXC.js";
4
+
1
5
  // encrypt.js
2
6
  import crypto from "crypto";
3
7
  var ALGORITHM = "aes-256-gcm";
4
8
  var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
5
9
  var encrypt = (value) => {
6
- const iv = crypto.randomBytes(12);
10
+ const iv = generate(12, null);
7
11
  const cipher = crypto.createCipheriv(ALGORITHM, getKey(), iv);
8
12
  const data = Buffer.concat([
9
13
  cipher.update(JSON.stringify(value), "utf8"),
package/dist/encrypt.cjs CHANGED
@@ -33,12 +33,21 @@ __export(encrypt_exports, {
33
33
  encrypt: () => encrypt
34
34
  });
35
35
  module.exports = __toCommonJS(encrypt_exports);
36
+ var import_crypto2 = __toESM(require("crypto"), 1);
37
+
38
+ // token.js
36
39
  var import_crypto = __toESM(require("crypto"), 1);
40
+ var generate = (bytes = 32, encoding = "base64url") => {
41
+ const buf = import_crypto.default.randomBytes(bytes);
42
+ return encoding ? buf.toString(encoding) : buf;
43
+ };
44
+
45
+ // encrypt.js
37
46
  var ALGORITHM = "aes-256-gcm";
38
47
  var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
39
48
  var encrypt = (value) => {
40
- const iv = import_crypto.default.randomBytes(12);
41
- const cipher = import_crypto.default.createCipheriv(ALGORITHM, getKey(), iv);
49
+ const iv = generate(12, null);
50
+ const cipher = import_crypto2.default.createCipheriv(ALGORITHM, getKey(), iv);
42
51
  const data = Buffer.concat([
43
52
  cipher.update(JSON.stringify(value), "utf8"),
44
53
  cipher.final()
@@ -49,7 +58,7 @@ var encrypt = (value) => {
49
58
  var decrypt = (value) => {
50
59
  if (typeof value !== "string") return value;
51
60
  const [ivHex, tagHex, dataHex] = value.split(":");
52
- const decipher = import_crypto.default.createDecipheriv(
61
+ const decipher = import_crypto2.default.createDecipheriv(
53
62
  ALGORITHM,
54
63
  getKey(),
55
64
  Buffer.from(ivHex, "hex")
@@ -1,4 +1,5 @@
1
1
  import crypto from 'crypto';
2
+ import { generate } from './token.cjs';
2
3
 
3
4
  const ALGORITHM = 'aes-256-gcm';
4
5
 
@@ -6,7 +7,7 @@ const getKey = () => Buffer.from( process.env.ENCRYPT_CONNECTION_SECRET, 'hex' )
6
7
 
7
8
  const encrypt = ( value ) => {
8
9
 
9
- const iv = crypto.randomBytes( 12 );
10
+ const iv = generate( 12, null );
10
11
  const cipher = crypto.createCipheriv( ALGORITHM, getKey(), iv );
11
12
  const data = Buffer.concat([
12
13
  cipher.update( JSON.stringify( value ), 'utf8' ),
package/dist/encrypt.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import crypto from 'crypto';
2
+ import { generate } from './token.js';
2
3
 
3
4
  const ALGORITHM = 'aes-256-gcm';
4
5
 
@@ -6,7 +7,7 @@ const getKey = () => Buffer.from( process.env.ENCRYPT_CONNECTION_SECRET, 'hex' )
6
7
 
7
8
  const encrypt = ( value ) => {
8
9
 
9
- const iv = crypto.randomBytes( 12 );
10
+ const iv = generate( 12, null );
10
11
  const cipher = crypto.createCipheriv( ALGORITHM, getKey(), iv );
11
12
  const data = Buffer.concat([
12
13
  cipher.update( JSON.stringify( value ), 'utf8' ),
package/dist/encrypt.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  decrypt,
3
3
  encrypt
4
- } from "./chunk-CUUQCM5T.js";
4
+ } from "./chunk-N5EMSZLI.js";
5
+ import "./chunk-HOA2JHXC.js";
5
6
  export {
6
7
  decrypt,
7
8
  encrypt
package/dist/shopify.cjs CHANGED
@@ -32,17 +32,30 @@ __export(shopify_exports, {
32
32
  getAdminToken: () => getAdminToken,
33
33
  ping: () => ping,
34
34
  refreshAdminToken: () => refreshAdminToken,
35
- resolveConnectionSettings: () => resolveConnectionSettings
35
+ resolveConnectionSettings: () => resolveConnectionSettings,
36
+ signOAuthState: () => signOAuthState,
37
+ verifyOAuthCallbackHmac: () => verifyOAuthCallbackHmac,
38
+ verifyOAuthState: () => verifyOAuthState
36
39
  });
37
40
  module.exports = __toCommonJS(shopify_exports);
41
+ var import_crypto3 = __toESM(require("crypto"), 1);
38
42
 
39
43
  // encrypt.js
44
+ var import_crypto2 = __toESM(require("crypto"), 1);
45
+
46
+ // token.js
40
47
  var import_crypto = __toESM(require("crypto"), 1);
48
+ var generate = (bytes = 32, encoding = "base64url") => {
49
+ const buf = import_crypto.default.randomBytes(bytes);
50
+ return encoding ? buf.toString(encoding) : buf;
51
+ };
52
+
53
+ // encrypt.js
41
54
  var ALGORITHM = "aes-256-gcm";
42
55
  var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
43
56
  var encrypt = (value) => {
44
- const iv = import_crypto.default.randomBytes(12);
45
- const cipher = import_crypto.default.createCipheriv(ALGORITHM, getKey(), iv);
57
+ const iv = generate(12, null);
58
+ const cipher = import_crypto2.default.createCipheriv(ALGORITHM, getKey(), iv);
46
59
  const data = Buffer.concat([
47
60
  cipher.update(JSON.stringify(value), "utf8"),
48
61
  cipher.final()
@@ -53,7 +66,7 @@ var encrypt = (value) => {
53
66
  var decrypt = (value) => {
54
67
  if (typeof value !== "string") return value;
55
68
  const [ivHex, tagHex, dataHex] = value.split(":");
56
- const decipher = import_crypto.default.createDecipheriv(
69
+ const decipher = import_crypto2.default.createDecipheriv(
57
70
  ALGORITHM,
58
71
  getKey(),
59
72
  Buffer.from(ivHex, "hex")
@@ -70,6 +83,48 @@ var decrypt = (value) => {
70
83
  var SHOPIFY_ADMIN_API_VERSION = "2025-01";
71
84
  var REFRESH_TOKEN_LIFETIME_MS = 90 * 24 * 60 * 60 * 1e3;
72
85
  var ACCESS_TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1e3;
86
+ var OAUTH_STATE_TTL_MS = 5 * 60 * 1e3;
87
+ var signOAuthState = ({ organizationId, shop }) => {
88
+ const payload = Buffer.from(JSON.stringify({
89
+ exp: Date.now() + OAUTH_STATE_TTL_MS,
90
+ organizationId,
91
+ shop
92
+ })).toString("base64url");
93
+ const signature = import_crypto3.default.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(payload).digest("base64url");
94
+ return payload + "." + signature;
95
+ };
96
+ var verifyOAuthState = (raw) => {
97
+ if (!raw) return null;
98
+ const [payload, signature] = raw.split(".");
99
+ if (!payload || !signature) return null;
100
+ const expected = import_crypto3.default.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(payload).digest("base64url");
101
+ if (signature.length !== expected.length) return null;
102
+ const valid = import_crypto3.default.timingSafeEqual(
103
+ Buffer.from(signature),
104
+ Buffer.from(expected)
105
+ );
106
+ if (!valid) return null;
107
+ let parsed;
108
+ try {
109
+ parsed = JSON.parse(Buffer.from(payload, "base64url").toString());
110
+ } catch {
111
+ return null;
112
+ }
113
+ ;
114
+ if (!(parsed == null ? void 0 : parsed.exp) || parsed.exp < Date.now()) return null;
115
+ return parsed;
116
+ };
117
+ var verifyOAuthCallbackHmac = ({ hmac, rawQuery }) => {
118
+ const message = (rawQuery || "").split("&").filter((pair) => !pair.startsWith("hmac=")).sort((a, b) => {
119
+ const nameA = a.split("=")[0];
120
+ const nameB = b.split("=")[0];
121
+ return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
122
+ }).join("&");
123
+ const digest = import_crypto3.default.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(message).digest("hex");
124
+ const digestBuf = Buffer.from(digest);
125
+ const hmacBuf = Buffer.from(hmac || "");
126
+ return digestBuf.length === hmacBuf.length && import_crypto3.default.timingSafeEqual(digestBuf, hmacBuf);
127
+ };
73
128
  var shopifyOAuthFetch = async (url, body) => {
74
129
  const response = await fetch(url, {
75
130
  method: "POST",
@@ -190,5 +245,8 @@ var getAdminToken = async ({ connection, controller }) => {
190
245
  getAdminToken,
191
246
  ping,
192
247
  refreshAdminToken,
193
- resolveConnectionSettings
248
+ resolveConnectionSettings,
249
+ signOAuthState,
250
+ verifyOAuthCallbackHmac,
251
+ verifyOAuthState
194
252
  });
@@ -1,9 +1,100 @@
1
+ import crypto from 'crypto';
1
2
  import { decrypt, encrypt } from './encrypt.cjs';
2
- import 'crypto';
3
+ import './token.cjs';
3
4
 
4
5
  const SHOPIFY_ADMIN_API_VERSION = '2025-01';
5
6
  const REFRESH_TOKEN_LIFETIME_MS = 90 * 24 * 60 * 60 * 1000;
6
7
  const ACCESS_TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
8
+ const OAUTH_STATE_TTL_MS = 5 * 60 * 1000;
9
+
10
+ const signOAuthState = ({ organizationId, shop }) => {
11
+
12
+ const payload = Buffer.from( JSON.stringify({
13
+ exp : Date.now() + OAUTH_STATE_TTL_MS,
14
+ organizationId,
15
+ shop
16
+ }) ).toString( 'base64url' );
17
+
18
+ const signature = crypto
19
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
20
+ .update( payload )
21
+ .digest( 'base64url' );
22
+
23
+ return payload + '.' + signature;
24
+
25
+ };
26
+
27
+ const verifyOAuthState = ( raw ) => {
28
+
29
+ if( ! raw ) return null;
30
+
31
+ const [ payload, signature ] = raw.split( '.' );
32
+
33
+ if( ! payload || ! signature ) return null;
34
+
35
+ const expected = crypto
36
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
37
+ .update( payload )
38
+ .digest( 'base64url' );
39
+
40
+ if( signature.length !== expected.length ) return null;
41
+
42
+ const valid = crypto.timingSafeEqual(
43
+ Buffer.from( signature ),
44
+ Buffer.from( expected )
45
+ );
46
+
47
+ if( ! valid ) return null;
48
+
49
+ let parsed;
50
+
51
+ try {
52
+
53
+ parsed = JSON.parse( Buffer.from( payload, 'base64url' ).toString() );
54
+
55
+ } catch {
56
+
57
+ return null;
58
+
59
+ }
60
+ if( ! parsed?.exp || parsed.exp < Date.now() ) return null;
61
+
62
+ return parsed;
63
+
64
+ };
65
+
66
+ // Verifies the HMAC Shopify includes on OAuth callback query strings. Sort by
67
+ // parameter NAME (not full key=value pair) per Shopify spec — matches the
68
+ // official @shopify/shopify-api SDK. A plain .sort() compares whole strings,
69
+ // which would diverge when one name is a prefix of another and the next char
70
+ // sorts before '=' in ASCII (e.g. `state` vs `state2`).
71
+ const verifyOAuthCallbackHmac = ({ hmac, rawQuery }) => {
72
+
73
+ const message = ( rawQuery || '' )
74
+ .split( '&' )
75
+ .filter( pair => ! pair.startsWith( 'hmac=' ) )
76
+ .sort( ( a, b ) => {
77
+
78
+ const nameA = a.split( '=' )[ 0 ];
79
+ const nameB = b.split( '=' )[ 0 ];
80
+
81
+ return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
82
+
83
+ })
84
+ .join( '&' );
85
+
86
+ const digest = crypto
87
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
88
+ .update( message )
89
+ .digest( 'hex' );
90
+
91
+ const digestBuf = Buffer.from( digest );
92
+ const hmacBuf = Buffer.from( hmac || '' );
93
+
94
+ return digestBuf.length === hmacBuf.length
95
+ && crypto.timingSafeEqual( digestBuf, hmacBuf );
96
+
97
+ };
7
98
 
8
99
  const shopifyOAuthFetch = async ( url, body ) => {
9
100
 
@@ -165,4 +256,4 @@ const getAdminToken = async ({ connection, controller }) => {
165
256
 
166
257
  };
167
258
 
168
- export { getAdminToken, ping, refreshAdminToken, resolveConnectionSettings };
259
+ export { getAdminToken, ping, refreshAdminToken, resolveConnectionSettings, signOAuthState, verifyOAuthCallbackHmac, verifyOAuthState };
package/dist/shopify.d.ts CHANGED
@@ -1,9 +1,100 @@
1
+ import crypto from 'crypto';
1
2
  import { decrypt, encrypt } from './encrypt.js';
2
- import 'crypto';
3
+ import './token.js';
3
4
 
4
5
  const SHOPIFY_ADMIN_API_VERSION = '2025-01';
5
6
  const REFRESH_TOKEN_LIFETIME_MS = 90 * 24 * 60 * 60 * 1000;
6
7
  const ACCESS_TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
8
+ const OAUTH_STATE_TTL_MS = 5 * 60 * 1000;
9
+
10
+ const signOAuthState = ({ organizationId, shop }) => {
11
+
12
+ const payload = Buffer.from( JSON.stringify({
13
+ exp : Date.now() + OAUTH_STATE_TTL_MS,
14
+ organizationId,
15
+ shop
16
+ }) ).toString( 'base64url' );
17
+
18
+ const signature = crypto
19
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
20
+ .update( payload )
21
+ .digest( 'base64url' );
22
+
23
+ return payload + '.' + signature;
24
+
25
+ };
26
+
27
+ const verifyOAuthState = ( raw ) => {
28
+
29
+ if( ! raw ) return null;
30
+
31
+ const [ payload, signature ] = raw.split( '.' );
32
+
33
+ if( ! payload || ! signature ) return null;
34
+
35
+ const expected = crypto
36
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
37
+ .update( payload )
38
+ .digest( 'base64url' );
39
+
40
+ if( signature.length !== expected.length ) return null;
41
+
42
+ const valid = crypto.timingSafeEqual(
43
+ Buffer.from( signature ),
44
+ Buffer.from( expected )
45
+ );
46
+
47
+ if( ! valid ) return null;
48
+
49
+ let parsed;
50
+
51
+ try {
52
+
53
+ parsed = JSON.parse( Buffer.from( payload, 'base64url' ).toString() );
54
+
55
+ } catch {
56
+
57
+ return null;
58
+
59
+ }
60
+ if( ! parsed?.exp || parsed.exp < Date.now() ) return null;
61
+
62
+ return parsed;
63
+
64
+ };
65
+
66
+ // Verifies the HMAC Shopify includes on OAuth callback query strings. Sort by
67
+ // parameter NAME (not full key=value pair) per Shopify spec — matches the
68
+ // official @shopify/shopify-api SDK. A plain .sort() compares whole strings,
69
+ // which would diverge when one name is a prefix of another and the next char
70
+ // sorts before '=' in ASCII (e.g. `state` vs `state2`).
71
+ const verifyOAuthCallbackHmac = ({ hmac, rawQuery }) => {
72
+
73
+ const message = ( rawQuery || '' )
74
+ .split( '&' )
75
+ .filter( pair => ! pair.startsWith( 'hmac=' ) )
76
+ .sort( ( a, b ) => {
77
+
78
+ const nameA = a.split( '=' )[ 0 ];
79
+ const nameB = b.split( '=' )[ 0 ];
80
+
81
+ return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
82
+
83
+ })
84
+ .join( '&' );
85
+
86
+ const digest = crypto
87
+ .createHmac( 'sha256', process.env.SHOPIFY_API_SECRET )
88
+ .update( message )
89
+ .digest( 'hex' );
90
+
91
+ const digestBuf = Buffer.from( digest );
92
+ const hmacBuf = Buffer.from( hmac || '' );
93
+
94
+ return digestBuf.length === hmacBuf.length
95
+ && crypto.timingSafeEqual( digestBuf, hmacBuf );
96
+
97
+ };
7
98
 
8
99
  const shopifyOAuthFetch = async ( url, body ) => {
9
100
 
@@ -165,4 +256,4 @@ const getAdminToken = async ({ connection, controller }) => {
165
256
 
166
257
  };
167
258
 
168
- export { getAdminToken, ping, refreshAdminToken, resolveConnectionSettings };
259
+ export { getAdminToken, ping, refreshAdminToken, resolveConnectionSettings, signOAuthState, verifyOAuthCallbackHmac, verifyOAuthState };
package/dist/shopify.js CHANGED
@@ -1,12 +1,56 @@
1
1
  import {
2
2
  decrypt,
3
3
  encrypt
4
- } from "./chunk-CUUQCM5T.js";
4
+ } from "./chunk-N5EMSZLI.js";
5
+ import "./chunk-HOA2JHXC.js";
5
6
 
6
7
  // shopify.js
8
+ import crypto from "crypto";
7
9
  var SHOPIFY_ADMIN_API_VERSION = "2025-01";
8
10
  var REFRESH_TOKEN_LIFETIME_MS = 90 * 24 * 60 * 60 * 1e3;
9
11
  var ACCESS_TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1e3;
12
+ var OAUTH_STATE_TTL_MS = 5 * 60 * 1e3;
13
+ var signOAuthState = ({ organizationId, shop }) => {
14
+ const payload = Buffer.from(JSON.stringify({
15
+ exp: Date.now() + OAUTH_STATE_TTL_MS,
16
+ organizationId,
17
+ shop
18
+ })).toString("base64url");
19
+ const signature = crypto.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(payload).digest("base64url");
20
+ return payload + "." + signature;
21
+ };
22
+ var verifyOAuthState = (raw) => {
23
+ if (!raw) return null;
24
+ const [payload, signature] = raw.split(".");
25
+ if (!payload || !signature) return null;
26
+ const expected = crypto.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(payload).digest("base64url");
27
+ if (signature.length !== expected.length) return null;
28
+ const valid = crypto.timingSafeEqual(
29
+ Buffer.from(signature),
30
+ Buffer.from(expected)
31
+ );
32
+ if (!valid) return null;
33
+ let parsed;
34
+ try {
35
+ parsed = JSON.parse(Buffer.from(payload, "base64url").toString());
36
+ } catch {
37
+ return null;
38
+ }
39
+ ;
40
+ if (!(parsed == null ? void 0 : parsed.exp) || parsed.exp < Date.now()) return null;
41
+ return parsed;
42
+ };
43
+ var verifyOAuthCallbackHmac = ({ hmac, rawQuery }) => {
44
+ const message = (rawQuery || "").split("&").filter((pair) => !pair.startsWith("hmac=")).sort((a, b) => {
45
+ const nameA = a.split("=")[0];
46
+ const nameB = b.split("=")[0];
47
+ return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
48
+ }).join("&");
49
+ const digest = crypto.createHmac("sha256", process.env.SHOPIFY_API_SECRET).update(message).digest("hex");
50
+ const digestBuf = Buffer.from(digest);
51
+ const hmacBuf = Buffer.from(hmac || "");
52
+ return digestBuf.length === hmacBuf.length && crypto.timingSafeEqual(digestBuf, hmacBuf);
53
+ };
10
54
  var shopifyOAuthFetch = async (url, body) => {
11
55
  const response = await fetch(url, {
12
56
  method: "POST",
@@ -126,5 +170,8 @@ export {
126
170
  getAdminToken,
127
171
  ping,
128
172
  refreshAdminToken,
129
- resolveConnectionSettings
173
+ resolveConnectionSettings,
174
+ signOAuthState,
175
+ verifyOAuthCallbackHmac,
176
+ verifyOAuthState
130
177
  };
package/dist/token.cjs ADDED
@@ -0,0 +1,43 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // token.js
30
+ var token_exports = {};
31
+ __export(token_exports, {
32
+ generate: () => generate
33
+ });
34
+ module.exports = __toCommonJS(token_exports);
35
+ var import_crypto = __toESM(require("crypto"), 1);
36
+ var generate = (bytes = 32, encoding = "base64url") => {
37
+ const buf = import_crypto.default.randomBytes(bytes);
38
+ return encoding ? buf.toString(encoding) : buf;
39
+ };
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ generate
43
+ });
@@ -0,0 +1,22 @@
1
+ import crypto from 'crypto';
2
+
3
+ // Cryptographically secure random bytes. Defaults to 32 bytes encoded as
4
+ // base64url — the shape used for unguessable shared secrets (preview tokens,
5
+ // admin tokens, share-link tokens, etc.). Pass `null` (or empty string) as
6
+ // `encoding` to receive the raw Buffer instead — used by encrypt.js for the
7
+ // AES-GCM IV.
8
+ //
9
+ // Usage:
10
+ // const { generate } = require( '@drawbridge/drawbridge-utils/token' );
11
+ // const tokenString = generate(); // 43-char base64url string
12
+ // const iv = generate( 12, null ); // 12-byte Buffer
13
+
14
+ const generate = ( bytes = 32, encoding = 'base64url' ) => {
15
+
16
+ const buf = crypto.randomBytes( bytes );
17
+
18
+ return encoding ? buf.toString( encoding ) : buf;
19
+
20
+ };
21
+
22
+ export { generate };
@@ -0,0 +1,22 @@
1
+ import crypto from 'crypto';
2
+
3
+ // Cryptographically secure random bytes. Defaults to 32 bytes encoded as
4
+ // base64url — the shape used for unguessable shared secrets (preview tokens,
5
+ // admin tokens, share-link tokens, etc.). Pass `null` (or empty string) as
6
+ // `encoding` to receive the raw Buffer instead — used by encrypt.js for the
7
+ // AES-GCM IV.
8
+ //
9
+ // Usage:
10
+ // const { generate } = require( '@drawbridge/drawbridge-utils/token' );
11
+ // const tokenString = generate(); // 43-char base64url string
12
+ // const iv = generate( 12, null ); // 12-byte Buffer
13
+
14
+ const generate = ( bytes = 32, encoding = 'base64url' ) => {
15
+
16
+ const buf = crypto.randomBytes( bytes );
17
+
18
+ return encoding ? buf.toString( encoding ) : buf;
19
+
20
+ };
21
+
22
+ export { generate };
package/dist/token.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ generate
3
+ } from "./chunk-HOA2JHXC.js";
4
+ export {
5
+ generate
6
+ };
package/package.json CHANGED
@@ -64,6 +64,11 @@
64
64
  "types": "./dist/slugify.d.ts",
65
65
  "import": "./dist/slugify.js",
66
66
  "require": "./dist/slugify.cjs"
67
+ },
68
+ "./token": {
69
+ "types": "./dist/token.d.ts",
70
+ "import": "./dist/token.js",
71
+ "require": "./dist/token.cjs"
67
72
  }
68
73
  },
69
74
  "files": [
@@ -81,5 +86,5 @@
81
86
  "build": "tsup && npm publish"
82
87
  },
83
88
  "types": "dist/index.d.ts",
84
- "version": "0.0.21"
89
+ "version": "0.0.23"
85
90
  }