@drawbridge/drawbridge-utils 0.0.41 → 0.0.42

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/dist/oauth.cjs CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,14 +16,105 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
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
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // oauth.js
20
30
  var oauth_exports = {};
21
31
  __export(oauth_exports, {
22
- createOAuthClient: () => createOAuthClient
32
+ DEVELOPER_ALLOWED_SCOPES: () => DEVELOPER_ALLOWED_SCOPES,
33
+ SCOPES: () => SCOPES,
34
+ compare: () => compare,
35
+ createOAuthClient: () => createOAuthClient,
36
+ generateToken: () => generateToken,
37
+ hashToken: () => hashToken,
38
+ intersect: () => intersect,
39
+ isDeveloperAllowed: () => isDeveloperAllowed,
40
+ parse: () => parse,
41
+ validate: () => validate
23
42
  });
24
43
  module.exports = __toCommonJS(oauth_exports);
44
+
45
+ // token.js
46
+ var import_crypto = __toESM(require("crypto"), 1);
47
+ var generate = (bytes = 32, encoding = "base64url") => {
48
+ const buf = import_crypto.default.randomBytes(bytes);
49
+ return encoding ? buf.toString(encoding) : buf;
50
+ };
51
+ var hash = (value, key) => {
52
+ if (!value) return null;
53
+ if (key) {
54
+ return import_crypto.default.createHmac("sha256", key).update(String(value)).digest("base64url");
55
+ }
56
+ return import_crypto.default.createHash("sha256").update(String(value)).digest("base64url");
57
+ };
58
+ var compare = (a, b) => {
59
+ if (typeof a !== "string" || typeof b !== "string") return false;
60
+ if (a.length !== b.length) return false;
61
+ return import_crypto.default.timingSafeEqual(
62
+ Buffer.from(a),
63
+ Buffer.from(b)
64
+ );
65
+ };
66
+
67
+ // oauth.js
68
+ var SCOPES = [
69
+ "profile:read",
70
+ "profile:write",
71
+ "organization:read",
72
+ "organization:write",
73
+ "campaigns:read",
74
+ "campaigns:write",
75
+ "contacts:read",
76
+ "contacts:write",
77
+ "workflows:read",
78
+ "workflows:write",
79
+ "connections:read",
80
+ "connections:write",
81
+ "billing:read",
82
+ "billing:write",
83
+ "admin"
84
+ ];
85
+ var DEVELOPER_ALLOWED_SCOPES = [
86
+ "profile:read",
87
+ "organization:read",
88
+ "campaigns:read",
89
+ "campaigns:write",
90
+ "contacts:read",
91
+ "contacts:write",
92
+ "workflows:read",
93
+ "workflows:write",
94
+ "connections:read",
95
+ "connections:write"
96
+ ];
97
+ var parse = (raw) => {
98
+ if (!raw) return [];
99
+ if (Array.isArray(raw)) return raw;
100
+ return String(raw).split(/\s+/).filter(Boolean);
101
+ };
102
+ var validate = (scopes) => {
103
+ const parsed = parse(scopes);
104
+ const invalid = parsed.filter((scope) => !SCOPES.includes(scope));
105
+ return {
106
+ invalid,
107
+ valid: invalid.length === 0
108
+ };
109
+ };
110
+ var intersect = (requested, allowed) => {
111
+ const r = parse(requested);
112
+ const a = parse(allowed);
113
+ return r.filter((scope) => a.includes(scope));
114
+ };
115
+ var isDeveloperAllowed = (scope) => DEVELOPER_ALLOWED_SCOPES.includes(scope);
116
+ var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
117
+ var generateToken = () => generate(32, "base64url");
25
118
  var REFRESH_LEAD_SECONDS = 60;
26
119
  var createOAuthClient = ({
27
120
  clientId,
@@ -90,5 +183,14 @@ var createOAuthClient = ({
90
183
  };
91
184
  // Annotate the CommonJS export names for ESM import in node:
92
185
  0 && (module.exports = {
93
- createOAuthClient
186
+ DEVELOPER_ALLOWED_SCOPES,
187
+ SCOPES,
188
+ compare,
189
+ createOAuthClient,
190
+ generateToken,
191
+ hashToken,
192
+ intersect,
193
+ isDeveloperAllowed,
194
+ parse,
195
+ validate
94
196
  });
package/dist/oauth.d.cts CHANGED
@@ -1,5 +1,106 @@
1
- // Client_credentials token caching helper for first-party Drawbridge apps
2
- // that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
1
+ import { generate, hash } from './token.cjs';
2
+ export { compare } from './token.cjs';
3
+ import 'crypto';
4
+
5
+ // Shared OAuth helpers used across drawbridge-api, drawbridge-sync,
6
+ // drawbridge-app-web, and any first-party app that needs to participate
7
+ // in the OAuth 2.1 provider:
8
+ //
9
+ // - SCOPES, DEVELOPER_ALLOWED_SCOPES: the locked scope vocabulary
10
+ // - parse, validate, intersect, isDeveloperAllowed: scope set operations
11
+ // - hashToken, generateToken, compare: opaque-token primitives
12
+ // - createOAuthClient: cached client_credentials access token getter
13
+
14
+
15
+ // ─────────────────────────────────────────────────────────────────────────
16
+ // Scope vocabulary (locked — 15 scopes, 10 in the developer-allowed subset)
17
+ // ─────────────────────────────────────────────────────────────────────────
18
+
19
+ const SCOPES = [
20
+ 'profile:read',
21
+ 'profile:write',
22
+ 'organization:read',
23
+ 'organization:write',
24
+ 'campaigns:read',
25
+ 'campaigns:write',
26
+ 'contacts:read',
27
+ 'contacts:write',
28
+ 'workflows:read',
29
+ 'workflows:write',
30
+ 'connections:read',
31
+ 'connections:write',
32
+ 'billing:read',
33
+ 'billing:write',
34
+ 'admin'
35
+ ];
36
+
37
+ // Subset granted to third-party developer apps via /oauth/clients self-service
38
+ // registration. Excludes profile:write (account-level), organization:write
39
+ // (org admin only), billing:* (financial), admin (privileged).
40
+ const DEVELOPER_ALLOWED_SCOPES = [
41
+ 'profile:read',
42
+ 'organization:read',
43
+ 'campaigns:read',
44
+ 'campaigns:write',
45
+ 'contacts:read',
46
+ 'contacts:write',
47
+ 'workflows:read',
48
+ 'workflows:write',
49
+ 'connections:read',
50
+ 'connections:write'
51
+ ];
52
+
53
+ const parse = ( raw ) => {
54
+
55
+ if( ! raw ) return [];
56
+
57
+ if( Array.isArray( raw ) ) return raw;
58
+
59
+ return String( raw ).split( /\s+/ ).filter( Boolean );
60
+
61
+ };
62
+
63
+ const validate = ( scopes ) => {
64
+
65
+ const parsed = parse( scopes );
66
+ const invalid = parsed.filter( ( scope ) => ! SCOPES.includes( scope ) );
67
+
68
+ return {
69
+ invalid,
70
+ valid : invalid.length === 0
71
+ };
72
+
73
+ };
74
+
75
+ const intersect = ( requested, allowed ) => {
76
+
77
+ const r = parse( requested );
78
+ const a = parse( allowed );
79
+
80
+ return r.filter( ( scope ) => a.includes( scope ) );
81
+
82
+ };
83
+
84
+ const isDeveloperAllowed = ( scope ) => DEVELOPER_ALLOWED_SCOPES.includes( scope );
85
+
86
+ // ─────────────────────────────────────────────────────────────────────────
87
+ // Token primitives — convenience wrappers over drawbridge-utils/token that
88
+ // fix the OAuth-specific conventions (32 bytes base64url, env-keyed HMAC)
89
+ // ─────────────────────────────────────────────────────────────────────────
90
+
91
+ // Hash an OAuth token with the deployment's HMAC key (OAUTH_TOKEN_HMAC_KEY).
92
+ // Falls back to plain SHA256 if the env var is unset — fine for local dev,
93
+ // not for production (DB compromise would then allow token forgery).
94
+ const hashToken = ( raw ) => hash( raw, process.env.OAUTH_TOKEN_HMAC_KEY );
95
+
96
+ // 32 cryptographically random bytes encoded as a 43-char base64url string —
97
+ // the shape used for access tokens, refresh tokens, PATs, and authorization
98
+ // codes.
99
+ const generateToken = () => generate( 32, 'base64url' );
100
+
101
+ // ─────────────────────────────────────────────────────────────────────────
102
+ // Client_credentials token caching for first-party apps that call
103
+ // drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
3
104
  // drawbridge-webhooks, etc.).
4
105
  //
5
106
  // Caches the access token in process memory; refreshes when nearing expiry
@@ -13,10 +114,11 @@
13
114
  // clientId : process.env.OAUTH_CLIENT_ID,
14
115
  // clientSecret : process.env.OAUTH_CLIENT_SECRET,
15
116
  // tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
16
- // scopes : [ 'share:read' ]
117
+ // scopes : [ 'campaigns:read' ]
17
118
  // });
18
119
  //
19
120
  // await request({ endpoint : '/page/abc', token : await client.token() });
121
+ // ─────────────────────────────────────────────────────────────────────────
20
122
 
21
123
  const REFRESH_LEAD_SECONDS = 60;
22
124
 
@@ -126,4 +228,4 @@ const createOAuthClient = ({
126
228
 
127
229
  };
128
230
 
129
- export { createOAuthClient };
231
+ export { DEVELOPER_ALLOWED_SCOPES, SCOPES, createOAuthClient, generateToken, hashToken, intersect, isDeveloperAllowed, parse, validate };
package/dist/oauth.d.ts CHANGED
@@ -1,5 +1,106 @@
1
- // Client_credentials token caching helper for first-party Drawbridge apps
2
- // that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
1
+ import { generate, hash } from './token.js';
2
+ export { compare } from './token.js';
3
+ import 'crypto';
4
+
5
+ // Shared OAuth helpers used across drawbridge-api, drawbridge-sync,
6
+ // drawbridge-app-web, and any first-party app that needs to participate
7
+ // in the OAuth 2.1 provider:
8
+ //
9
+ // - SCOPES, DEVELOPER_ALLOWED_SCOPES: the locked scope vocabulary
10
+ // - parse, validate, intersect, isDeveloperAllowed: scope set operations
11
+ // - hashToken, generateToken, compare: opaque-token primitives
12
+ // - createOAuthClient: cached client_credentials access token getter
13
+
14
+
15
+ // ─────────────────────────────────────────────────────────────────────────
16
+ // Scope vocabulary (locked — 15 scopes, 10 in the developer-allowed subset)
17
+ // ─────────────────────────────────────────────────────────────────────────
18
+
19
+ const SCOPES = [
20
+ 'profile:read',
21
+ 'profile:write',
22
+ 'organization:read',
23
+ 'organization:write',
24
+ 'campaigns:read',
25
+ 'campaigns:write',
26
+ 'contacts:read',
27
+ 'contacts:write',
28
+ 'workflows:read',
29
+ 'workflows:write',
30
+ 'connections:read',
31
+ 'connections:write',
32
+ 'billing:read',
33
+ 'billing:write',
34
+ 'admin'
35
+ ];
36
+
37
+ // Subset granted to third-party developer apps via /oauth/clients self-service
38
+ // registration. Excludes profile:write (account-level), organization:write
39
+ // (org admin only), billing:* (financial), admin (privileged).
40
+ const DEVELOPER_ALLOWED_SCOPES = [
41
+ 'profile:read',
42
+ 'organization:read',
43
+ 'campaigns:read',
44
+ 'campaigns:write',
45
+ 'contacts:read',
46
+ 'contacts:write',
47
+ 'workflows:read',
48
+ 'workflows:write',
49
+ 'connections:read',
50
+ 'connections:write'
51
+ ];
52
+
53
+ const parse = ( raw ) => {
54
+
55
+ if( ! raw ) return [];
56
+
57
+ if( Array.isArray( raw ) ) return raw;
58
+
59
+ return String( raw ).split( /\s+/ ).filter( Boolean );
60
+
61
+ };
62
+
63
+ const validate = ( scopes ) => {
64
+
65
+ const parsed = parse( scopes );
66
+ const invalid = parsed.filter( ( scope ) => ! SCOPES.includes( scope ) );
67
+
68
+ return {
69
+ invalid,
70
+ valid : invalid.length === 0
71
+ };
72
+
73
+ };
74
+
75
+ const intersect = ( requested, allowed ) => {
76
+
77
+ const r = parse( requested );
78
+ const a = parse( allowed );
79
+
80
+ return r.filter( ( scope ) => a.includes( scope ) );
81
+
82
+ };
83
+
84
+ const isDeveloperAllowed = ( scope ) => DEVELOPER_ALLOWED_SCOPES.includes( scope );
85
+
86
+ // ─────────────────────────────────────────────────────────────────────────
87
+ // Token primitives — convenience wrappers over drawbridge-utils/token that
88
+ // fix the OAuth-specific conventions (32 bytes base64url, env-keyed HMAC)
89
+ // ─────────────────────────────────────────────────────────────────────────
90
+
91
+ // Hash an OAuth token with the deployment's HMAC key (OAUTH_TOKEN_HMAC_KEY).
92
+ // Falls back to plain SHA256 if the env var is unset — fine for local dev,
93
+ // not for production (DB compromise would then allow token forgery).
94
+ const hashToken = ( raw ) => hash( raw, process.env.OAUTH_TOKEN_HMAC_KEY );
95
+
96
+ // 32 cryptographically random bytes encoded as a 43-char base64url string —
97
+ // the shape used for access tokens, refresh tokens, PATs, and authorization
98
+ // codes.
99
+ const generateToken = () => generate( 32, 'base64url' );
100
+
101
+ // ─────────────────────────────────────────────────────────────────────────
102
+ // Client_credentials token caching for first-party apps that call
103
+ // drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
3
104
  // drawbridge-webhooks, etc.).
4
105
  //
5
106
  // Caches the access token in process memory; refreshes when nearing expiry
@@ -13,10 +114,11 @@
13
114
  // clientId : process.env.OAUTH_CLIENT_ID,
14
115
  // clientSecret : process.env.OAUTH_CLIENT_SECRET,
15
116
  // tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
16
- // scopes : [ 'share:read' ]
117
+ // scopes : [ 'campaigns:read' ]
17
118
  // });
18
119
  //
19
120
  // await request({ endpoint : '/page/abc', token : await client.token() });
121
+ // ─────────────────────────────────────────────────────────────────────────
20
122
 
21
123
  const REFRESH_LEAD_SECONDS = 60;
22
124
 
@@ -126,4 +228,4 @@ const createOAuthClient = ({
126
228
 
127
229
  };
128
230
 
129
- export { createOAuthClient };
231
+ export { DEVELOPER_ALLOWED_SCOPES, SCOPES, createOAuthClient, generateToken, hashToken, intersect, isDeveloperAllowed, parse, validate };
package/dist/oauth.js CHANGED
@@ -1,4 +1,60 @@
1
+ import {
2
+ compare,
3
+ generate,
4
+ hash
5
+ } from "./chunk-OS2AHUWX.js";
6
+
1
7
  // oauth.js
8
+ var SCOPES = [
9
+ "profile:read",
10
+ "profile:write",
11
+ "organization:read",
12
+ "organization:write",
13
+ "campaigns:read",
14
+ "campaigns:write",
15
+ "contacts:read",
16
+ "contacts:write",
17
+ "workflows:read",
18
+ "workflows:write",
19
+ "connections:read",
20
+ "connections:write",
21
+ "billing:read",
22
+ "billing:write",
23
+ "admin"
24
+ ];
25
+ var DEVELOPER_ALLOWED_SCOPES = [
26
+ "profile:read",
27
+ "organization:read",
28
+ "campaigns:read",
29
+ "campaigns:write",
30
+ "contacts:read",
31
+ "contacts:write",
32
+ "workflows:read",
33
+ "workflows:write",
34
+ "connections:read",
35
+ "connections:write"
36
+ ];
37
+ var parse = (raw) => {
38
+ if (!raw) return [];
39
+ if (Array.isArray(raw)) return raw;
40
+ return String(raw).split(/\s+/).filter(Boolean);
41
+ };
42
+ var validate = (scopes) => {
43
+ const parsed = parse(scopes);
44
+ const invalid = parsed.filter((scope) => !SCOPES.includes(scope));
45
+ return {
46
+ invalid,
47
+ valid: invalid.length === 0
48
+ };
49
+ };
50
+ var intersect = (requested, allowed) => {
51
+ const r = parse(requested);
52
+ const a = parse(allowed);
53
+ return r.filter((scope) => a.includes(scope));
54
+ };
55
+ var isDeveloperAllowed = (scope) => DEVELOPER_ALLOWED_SCOPES.includes(scope);
56
+ var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
57
+ var generateToken = () => generate(32, "base64url");
2
58
  var REFRESH_LEAD_SECONDS = 60;
3
59
  var createOAuthClient = ({
4
60
  clientId,
@@ -66,5 +122,14 @@ var createOAuthClient = ({
66
122
  };
67
123
  };
68
124
  export {
69
- createOAuthClient
125
+ DEVELOPER_ALLOWED_SCOPES,
126
+ SCOPES,
127
+ compare,
128
+ createOAuthClient,
129
+ generateToken,
130
+ hashToken,
131
+ intersect,
132
+ isDeveloperAllowed,
133
+ parse,
134
+ validate
70
135
  };
package/package.json CHANGED
@@ -114,5 +114,5 @@
114
114
  "build": "tsup && npm publish"
115
115
  },
116
116
  "types": "dist/index.d.ts",
117
- "version": "0.0.41"
117
+ "version": "0.0.42"
118
118
  }