@drawbridge/drawbridge-utils 0.0.42 → 0.0.44
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/ai.cjs +4 -4
- package/dist/ai.js +127 -8
- package/dist/axios.cjs +1 -1
- package/dist/axios.js +1 -1
- package/dist/cdn.cjs +1 -1
- package/dist/cdn.js +1 -1
- package/dist/circuit.cjs +1 -1
- package/dist/circuit.js +50 -3
- package/dist/encrypt.cjs +3 -3
- package/dist/encrypt.js +38 -5
- package/dist/fetch.cjs +1 -1
- package/dist/fetch.js +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.js +1 -1
- package/dist/nanoid.cjs +1 -1
- package/dist/nanoid.js +1 -1
- package/dist/{oauth.cjs → oauth/index.cjs} +45 -16
- package/dist/{oauth.d.ts → oauth/index.d.cts} +40 -8
- package/dist/{oauth.d.cts → oauth/index.d.ts} +40 -8
- package/dist/{oauth.js → oauth/index.js} +59 -17
- package/dist/oauth/server.cjs +417 -0
- package/dist/oauth/server.d.cts +387 -0
- package/dist/oauth/server.d.ts +387 -0
- package/dist/oauth/server.js +381 -0
- package/dist/orders.cjs +1 -1
- package/dist/orders.js +1 -1
- package/dist/redirect.cjs +1 -1
- package/dist/redirect.js +1 -1
- package/dist/shopify.cjs +5 -5
- package/dist/shopify.js +46 -11
- package/dist/slugify.cjs +1 -1
- package/dist/slugify.js +1 -1
- package/dist/token.cjs +1 -1
- package/dist/token.js +21 -5
- package/dist/transactions.cjs +1 -1
- package/dist/transactions.js +108 -4
- package/dist/upload.cjs +1 -1
- package/dist/upload.js +1 -1
- package/package.json +20 -4
- package/dist/chunk-2DRAIKGZ.js +0 -38
- package/dist/chunk-6HH36OK6.js +0 -54
- package/dist/chunk-JT2PAZAZ.js +0 -113
- package/dist/chunk-OS2AHUWX.js +0 -27
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { generate, hash } from '
|
|
2
|
-
export { compare } from '
|
|
1
|
+
import { generate, hash } from '../token.js';
|
|
2
|
+
export { compare } from '../token.js';
|
|
3
3
|
import 'crypto';
|
|
4
4
|
|
|
5
5
|
// Shared OAuth helpers used across drawbridge-api, drawbridge-sync,
|
|
6
6
|
// drawbridge-app-web, and any first-party app that needs to participate
|
|
7
7
|
// in the OAuth 2.1 provider:
|
|
8
8
|
//
|
|
9
|
-
// -
|
|
9
|
+
// - scopes, developer: the locked scope vocabulary
|
|
10
10
|
// - parse, validate, intersect, isDeveloperAllowed: scope set operations
|
|
11
11
|
// - hashToken, generateToken, compare: opaque-token primitives
|
|
12
12
|
// - createOAuthClient: cached client_credentials access token getter
|
|
@@ -16,7 +16,7 @@ import 'crypto';
|
|
|
16
16
|
// Scope vocabulary (locked — 15 scopes, 10 in the developer-allowed subset)
|
|
17
17
|
// ─────────────────────────────────────────────────────────────────────────
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const scopes = [
|
|
20
20
|
'profile:read',
|
|
21
21
|
'profile:write',
|
|
22
22
|
'organization:read',
|
|
@@ -37,7 +37,7 @@ const SCOPES = [
|
|
|
37
37
|
// Subset granted to third-party developer apps via /oauth/clients self-service
|
|
38
38
|
// registration. Excludes profile:write (account-level), organization:write
|
|
39
39
|
// (org admin only), billing:* (financial), admin (privileged).
|
|
40
|
-
const
|
|
40
|
+
const developer = [
|
|
41
41
|
'profile:read',
|
|
42
42
|
'organization:read',
|
|
43
43
|
'campaigns:read',
|
|
@@ -63,7 +63,7 @@ const parse = ( raw ) => {
|
|
|
63
63
|
const validate = ( scopes ) => {
|
|
64
64
|
|
|
65
65
|
const parsed = parse( scopes );
|
|
66
|
-
const invalid = parsed.filter( ( scope ) => !
|
|
66
|
+
const invalid = parsed.filter( ( scope ) => ! scopes.includes( scope ) );
|
|
67
67
|
|
|
68
68
|
return {
|
|
69
69
|
invalid,
|
|
@@ -81,7 +81,39 @@ const intersect = ( requested, allowed ) => {
|
|
|
81
81
|
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
const isDeveloperAllowed = ( scope ) =>
|
|
84
|
+
const isDeveloperAllowed = ( scope ) => developer.includes( scope );
|
|
85
|
+
|
|
86
|
+
// User-facing labels for each scope — used by UI components rendering
|
|
87
|
+
// scope pickers. Centralized here so the dashboard, future admin tools,
|
|
88
|
+
// and any other consumer surface the same strings.
|
|
89
|
+
const scopeLabels = {
|
|
90
|
+
'profile:read' : 'Read own profile',
|
|
91
|
+
'profile:write' : 'Update own profile',
|
|
92
|
+
'organization:read' : 'Read organization info',
|
|
93
|
+
'organization:write' : 'Manage organization',
|
|
94
|
+
'campaigns:read' : 'Read campaigns',
|
|
95
|
+
'campaigns:write' : 'Manage campaigns',
|
|
96
|
+
'contacts:read' : 'Read contacts',
|
|
97
|
+
'contacts:write' : 'Manage contacts',
|
|
98
|
+
'workflows:read' : 'Read workflows',
|
|
99
|
+
'workflows:write' : 'Manage workflows',
|
|
100
|
+
'connections:read' : 'Read integrations',
|
|
101
|
+
'connections:write' : 'Manage integrations',
|
|
102
|
+
'billing:read' : 'Read billing',
|
|
103
|
+
'billing:write' : 'Manage billing',
|
|
104
|
+
admin : 'Admin access'
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const toOption = ( scope ) => ({
|
|
108
|
+
key : scopeLabels[ scope ] || scope,
|
|
109
|
+
value : scope
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// `{ key, value }` option arrays ready to feed any standard checkbox-list
|
|
113
|
+
// component. Derived from the canonical scope arrays so vocabulary
|
|
114
|
+
// changes auto-propagate to every consumer on the next utils bump.
|
|
115
|
+
const developerOptions = developer.map( toOption );
|
|
116
|
+
const scopeOptions = scopes.map( toOption );
|
|
85
117
|
|
|
86
118
|
// ─────────────────────────────────────────────────────────────────────────
|
|
87
119
|
// Token primitives — convenience wrappers over drawbridge-utils/token that
|
|
@@ -228,4 +260,4 @@ const createOAuthClient = ({
|
|
|
228
260
|
|
|
229
261
|
};
|
|
230
262
|
|
|
231
|
-
export {
|
|
263
|
+
export { createOAuthClient, developer, developerOptions, generateToken, hashToken, intersect, isDeveloperAllowed, parse, scopeLabels, scopeOptions, scopes, validate };
|
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// lib/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
|
+
var hash = (value, key) => {
|
|
8
|
+
if (!value) return null;
|
|
9
|
+
if (key) {
|
|
10
|
+
return crypto.createHmac("sha256", key).update(String(value)).digest("base64url");
|
|
11
|
+
}
|
|
12
|
+
return crypto.createHash("sha256").update(String(value)).digest("base64url");
|
|
13
|
+
};
|
|
14
|
+
var compare = (a, b) => {
|
|
15
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
16
|
+
if (a.length !== b.length) return false;
|
|
17
|
+
return crypto.timingSafeEqual(
|
|
18
|
+
Buffer.from(a),
|
|
19
|
+
Buffer.from(b)
|
|
20
|
+
);
|
|
21
|
+
};
|
|
6
22
|
|
|
7
|
-
// oauth.js
|
|
8
|
-
var
|
|
23
|
+
// lib/oauth/index.js
|
|
24
|
+
var scopes = [
|
|
9
25
|
"profile:read",
|
|
10
26
|
"profile:write",
|
|
11
27
|
"organization:read",
|
|
@@ -22,7 +38,7 @@ var SCOPES = [
|
|
|
22
38
|
"billing:write",
|
|
23
39
|
"admin"
|
|
24
40
|
];
|
|
25
|
-
var
|
|
41
|
+
var developer = [
|
|
26
42
|
"profile:read",
|
|
27
43
|
"organization:read",
|
|
28
44
|
"campaigns:read",
|
|
@@ -39,9 +55,9 @@ var parse = (raw) => {
|
|
|
39
55
|
if (Array.isArray(raw)) return raw;
|
|
40
56
|
return String(raw).split(/\s+/).filter(Boolean);
|
|
41
57
|
};
|
|
42
|
-
var validate = (
|
|
43
|
-
const parsed = parse(
|
|
44
|
-
const invalid = parsed.filter((scope) => !
|
|
58
|
+
var validate = (scopes2) => {
|
|
59
|
+
const parsed = parse(scopes2);
|
|
60
|
+
const invalid = parsed.filter((scope) => !scopes2.includes(scope));
|
|
45
61
|
return {
|
|
46
62
|
invalid,
|
|
47
63
|
valid: invalid.length === 0
|
|
@@ -52,14 +68,37 @@ var intersect = (requested, allowed) => {
|
|
|
52
68
|
const a = parse(allowed);
|
|
53
69
|
return r.filter((scope) => a.includes(scope));
|
|
54
70
|
};
|
|
55
|
-
var isDeveloperAllowed = (scope) =>
|
|
71
|
+
var isDeveloperAllowed = (scope) => developer.includes(scope);
|
|
72
|
+
var scopeLabels = {
|
|
73
|
+
"profile:read": "Read own profile",
|
|
74
|
+
"profile:write": "Update own profile",
|
|
75
|
+
"organization:read": "Read organization info",
|
|
76
|
+
"organization:write": "Manage organization",
|
|
77
|
+
"campaigns:read": "Read campaigns",
|
|
78
|
+
"campaigns:write": "Manage campaigns",
|
|
79
|
+
"contacts:read": "Read contacts",
|
|
80
|
+
"contacts:write": "Manage contacts",
|
|
81
|
+
"workflows:read": "Read workflows",
|
|
82
|
+
"workflows:write": "Manage workflows",
|
|
83
|
+
"connections:read": "Read integrations",
|
|
84
|
+
"connections:write": "Manage integrations",
|
|
85
|
+
"billing:read": "Read billing",
|
|
86
|
+
"billing:write": "Manage billing",
|
|
87
|
+
admin: "Admin access"
|
|
88
|
+
};
|
|
89
|
+
var toOption = (scope) => ({
|
|
90
|
+
key: scopeLabels[scope] || scope,
|
|
91
|
+
value: scope
|
|
92
|
+
});
|
|
93
|
+
var developerOptions = developer.map(toOption);
|
|
94
|
+
var scopeOptions = scopes.map(toOption);
|
|
56
95
|
var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
|
|
57
96
|
var generateToken = () => generate(32, "base64url");
|
|
58
97
|
var REFRESH_LEAD_SECONDS = 60;
|
|
59
98
|
var createOAuthClient = ({
|
|
60
99
|
clientId,
|
|
61
100
|
clientSecret,
|
|
62
|
-
scopes = [],
|
|
101
|
+
scopes: scopes2 = [],
|
|
63
102
|
tokenUri
|
|
64
103
|
}) => {
|
|
65
104
|
if (!clientId) throw new Error("createOAuthClient: clientId required");
|
|
@@ -75,8 +114,8 @@ var createOAuthClient = ({
|
|
|
75
114
|
const fetchToken = async () => {
|
|
76
115
|
const params = new URLSearchParams();
|
|
77
116
|
params.set("grant_type", "client_credentials");
|
|
78
|
-
if (
|
|
79
|
-
params.set("scope",
|
|
117
|
+
if (scopes2.length > 0) {
|
|
118
|
+
params.set("scope", scopes2.join(" "));
|
|
80
119
|
}
|
|
81
120
|
const basic = Buffer.from(clientId + ":" + clientSecret).toString("base64");
|
|
82
121
|
const response = await fetch(
|
|
@@ -122,14 +161,17 @@ var createOAuthClient = ({
|
|
|
122
161
|
};
|
|
123
162
|
};
|
|
124
163
|
export {
|
|
125
|
-
DEVELOPER_ALLOWED_SCOPES,
|
|
126
|
-
SCOPES,
|
|
127
164
|
compare,
|
|
128
165
|
createOAuthClient,
|
|
166
|
+
developer,
|
|
167
|
+
developerOptions,
|
|
129
168
|
generateToken,
|
|
130
169
|
hashToken,
|
|
131
170
|
intersect,
|
|
132
171
|
isDeveloperAllowed,
|
|
133
172
|
parse,
|
|
173
|
+
scopeLabels,
|
|
174
|
+
scopeOptions,
|
|
175
|
+
scopes,
|
|
134
176
|
validate
|
|
135
177
|
};
|
|
@@ -0,0 +1,417 @@
|
|
|
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
|
+
// lib/oauth/server.js
|
|
30
|
+
var server_exports = {};
|
|
31
|
+
__export(server_exports, {
|
|
32
|
+
createClient: () => createClient,
|
|
33
|
+
createOAuthModel: () => createOAuthModel,
|
|
34
|
+
createOAuthServer: () => createOAuthServer
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(server_exports);
|
|
37
|
+
var import_oauth2_server = __toESM(require("@node-oauth/oauth2-server"), 1);
|
|
38
|
+
|
|
39
|
+
// lib/token.js
|
|
40
|
+
var import_crypto = __toESM(require("crypto"), 1);
|
|
41
|
+
var generate = (bytes = 32, encoding = "base64url") => {
|
|
42
|
+
const buf = import_crypto.default.randomBytes(bytes);
|
|
43
|
+
return encoding ? buf.toString(encoding) : buf;
|
|
44
|
+
};
|
|
45
|
+
var hash = (value, key) => {
|
|
46
|
+
if (!value) return null;
|
|
47
|
+
if (key) {
|
|
48
|
+
return import_crypto.default.createHmac("sha256", key).update(String(value)).digest("base64url");
|
|
49
|
+
}
|
|
50
|
+
return import_crypto.default.createHash("sha256").update(String(value)).digest("base64url");
|
|
51
|
+
};
|
|
52
|
+
var compare = (a, b) => {
|
|
53
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
54
|
+
if (a.length !== b.length) return false;
|
|
55
|
+
return import_crypto.default.timingSafeEqual(
|
|
56
|
+
Buffer.from(a),
|
|
57
|
+
Buffer.from(b)
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// lib/oauth/index.js
|
|
62
|
+
var scopes = [
|
|
63
|
+
"profile:read",
|
|
64
|
+
"profile:write",
|
|
65
|
+
"organization:read",
|
|
66
|
+
"organization:write",
|
|
67
|
+
"campaigns:read",
|
|
68
|
+
"campaigns:write",
|
|
69
|
+
"contacts:read",
|
|
70
|
+
"contacts:write",
|
|
71
|
+
"workflows:read",
|
|
72
|
+
"workflows:write",
|
|
73
|
+
"connections:read",
|
|
74
|
+
"connections:write",
|
|
75
|
+
"billing:read",
|
|
76
|
+
"billing:write",
|
|
77
|
+
"admin"
|
|
78
|
+
];
|
|
79
|
+
var developer = [
|
|
80
|
+
"profile:read",
|
|
81
|
+
"organization:read",
|
|
82
|
+
"campaigns:read",
|
|
83
|
+
"campaigns:write",
|
|
84
|
+
"contacts:read",
|
|
85
|
+
"contacts:write",
|
|
86
|
+
"workflows:read",
|
|
87
|
+
"workflows:write",
|
|
88
|
+
"connections:read",
|
|
89
|
+
"connections:write"
|
|
90
|
+
];
|
|
91
|
+
var parse = (raw) => {
|
|
92
|
+
if (!raw) return [];
|
|
93
|
+
if (Array.isArray(raw)) return raw;
|
|
94
|
+
return String(raw).split(/\s+/).filter(Boolean);
|
|
95
|
+
};
|
|
96
|
+
var intersect = (requested, allowed) => {
|
|
97
|
+
const r = parse(requested);
|
|
98
|
+
const a = parse(allowed);
|
|
99
|
+
return r.filter((scope) => a.includes(scope));
|
|
100
|
+
};
|
|
101
|
+
var scopeLabels = {
|
|
102
|
+
"profile:read": "Read own profile",
|
|
103
|
+
"profile:write": "Update own profile",
|
|
104
|
+
"organization:read": "Read organization info",
|
|
105
|
+
"organization:write": "Manage organization",
|
|
106
|
+
"campaigns:read": "Read campaigns",
|
|
107
|
+
"campaigns:write": "Manage campaigns",
|
|
108
|
+
"contacts:read": "Read contacts",
|
|
109
|
+
"contacts:write": "Manage contacts",
|
|
110
|
+
"workflows:read": "Read workflows",
|
|
111
|
+
"workflows:write": "Manage workflows",
|
|
112
|
+
"connections:read": "Read integrations",
|
|
113
|
+
"connections:write": "Manage integrations",
|
|
114
|
+
"billing:read": "Read billing",
|
|
115
|
+
"billing:write": "Manage billing",
|
|
116
|
+
admin: "Admin access"
|
|
117
|
+
};
|
|
118
|
+
var toOption = (scope) => ({
|
|
119
|
+
key: scopeLabels[scope] || scope,
|
|
120
|
+
value: scope
|
|
121
|
+
});
|
|
122
|
+
var developerOptions = developer.map(toOption);
|
|
123
|
+
var scopeOptions = scopes.map(toOption);
|
|
124
|
+
var hashToken = (raw) => hash(raw, process.env.OAUTH_TOKEN_HMAC_KEY);
|
|
125
|
+
var generateToken = () => generate(32, "base64url");
|
|
126
|
+
|
|
127
|
+
// lib/oauth/server.js
|
|
128
|
+
var ACCESS_TOKEN_LIFETIME_SECONDS = 60 * 60;
|
|
129
|
+
var REFRESH_TOKEN_LIFETIME_SECONDS = 60 * 60 * 24 * 30;
|
|
130
|
+
var AUTHORIZATION_CODE_LIFETIME_MS = 10 * 60 * 1e3;
|
|
131
|
+
var createOAuthModel = ({ controller }) => ({
|
|
132
|
+
getClient: async (clientId, clientSecret) => {
|
|
133
|
+
var _a;
|
|
134
|
+
const client = await controller.get({
|
|
135
|
+
collection: "oauth",
|
|
136
|
+
query: {
|
|
137
|
+
clientId,
|
|
138
|
+
status: "active"
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
if (!client) return false;
|
|
142
|
+
if (clientSecret) {
|
|
143
|
+
const validSecret = (_a = client.clientSecretHashes) == null ? void 0 : _a.some((entry) => {
|
|
144
|
+
if (entry.retiredAt) return false;
|
|
145
|
+
return compare(hashToken(clientSecret), entry.hash);
|
|
146
|
+
});
|
|
147
|
+
if (!validSecret) return false;
|
|
148
|
+
}
|
|
149
|
+
;
|
|
150
|
+
return {
|
|
151
|
+
clientId: client.clientId,
|
|
152
|
+
grants: ["authorization_code", "client_credentials", "refresh_token"],
|
|
153
|
+
id: client.id,
|
|
154
|
+
redirectUris: client.redirectUris ?? [],
|
|
155
|
+
scopes: client.allowedScopes ?? []
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
saveAuthorizationCode: async (code, client, user) => {
|
|
159
|
+
const rawCode = code.authorizationCode;
|
|
160
|
+
const tokenHash = hashToken(rawCode);
|
|
161
|
+
const expiresAt = new Date(Date.now() + AUTHORIZATION_CODE_LIFETIME_MS);
|
|
162
|
+
await controller.create({
|
|
163
|
+
collection: "otc",
|
|
164
|
+
data: {
|
|
165
|
+
client: client.clientId,
|
|
166
|
+
codeChallenge: code.codeChallenge ?? null,
|
|
167
|
+
codeChallengeMethod: code.codeChallengeMethod ?? null,
|
|
168
|
+
consumedAt: null,
|
|
169
|
+
context: "oauth.authorize",
|
|
170
|
+
expiresAt,
|
|
171
|
+
redirectUri: code.redirectUri ?? null,
|
|
172
|
+
scopes: code.scope ?? [],
|
|
173
|
+
tokenHash,
|
|
174
|
+
user: user.id
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
authorizationCode: rawCode,
|
|
179
|
+
client: { id: client.id },
|
|
180
|
+
expiresAt,
|
|
181
|
+
redirectUri: code.redirectUri,
|
|
182
|
+
scope: code.scope,
|
|
183
|
+
user: { id: user.id }
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
getAuthorizationCode: async (authorizationCode) => {
|
|
187
|
+
const tokenHash = hashToken(authorizationCode);
|
|
188
|
+
const now = /* @__PURE__ */ new Date();
|
|
189
|
+
const record = await controller.get({
|
|
190
|
+
collection: "otc",
|
|
191
|
+
query: {
|
|
192
|
+
consumedAt: null,
|
|
193
|
+
context: "oauth.authorize",
|
|
194
|
+
expiresAt: { $gt: now },
|
|
195
|
+
tokenHash
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
if (!record) return false;
|
|
199
|
+
return {
|
|
200
|
+
authorizationCode,
|
|
201
|
+
client: { id: record.client },
|
|
202
|
+
codeChallenge: record.codeChallenge,
|
|
203
|
+
codeChallengeMethod: record.codeChallengeMethod,
|
|
204
|
+
expiresAt: record.expiresAt,
|
|
205
|
+
redirectUri: record.redirectUri,
|
|
206
|
+
scope: record.scopes,
|
|
207
|
+
user: { id: record.user }
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
revokeAuthorizationCode: async (code) => {
|
|
211
|
+
const tokenHash = hashToken(code.authorizationCode);
|
|
212
|
+
await controller.update({
|
|
213
|
+
collection: "otc",
|
|
214
|
+
data: {
|
|
215
|
+
$set: {
|
|
216
|
+
consumedAt: /* @__PURE__ */ new Date()
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
query: {
|
|
220
|
+
context: "oauth.authorize",
|
|
221
|
+
tokenHash
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return true;
|
|
225
|
+
},
|
|
226
|
+
saveToken: async (token, client, user) => {
|
|
227
|
+
const now = /* @__PURE__ */ new Date();
|
|
228
|
+
const accessRaw = token.accessToken;
|
|
229
|
+
const accessHash = hashToken(accessRaw);
|
|
230
|
+
const accessExpiresAt = token.accessTokenExpiresAt ?? new Date(now.getTime() + ACCESS_TOKEN_LIFETIME_SECONDS * 1e3);
|
|
231
|
+
await controller.create({
|
|
232
|
+
collection: "token",
|
|
233
|
+
data: {
|
|
234
|
+
client: client.clientId,
|
|
235
|
+
consumedAt: null,
|
|
236
|
+
data: {},
|
|
237
|
+
email: null,
|
|
238
|
+
expiresAt: accessExpiresAt,
|
|
239
|
+
lastUsedAt: null,
|
|
240
|
+
name: null,
|
|
241
|
+
revoked: false,
|
|
242
|
+
revokedAt: null,
|
|
243
|
+
rotatedFrom: null,
|
|
244
|
+
scopes: token.scope ?? [],
|
|
245
|
+
tokenHash: accessHash,
|
|
246
|
+
type: "access",
|
|
247
|
+
user: (user == null ? void 0 : user.id) ?? null
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
const result = {
|
|
251
|
+
accessToken: accessRaw,
|
|
252
|
+
accessTokenExpiresAt: accessExpiresAt,
|
|
253
|
+
client: { id: client.id },
|
|
254
|
+
scope: token.scope,
|
|
255
|
+
user: { id: (user == null ? void 0 : user.id) ?? null }
|
|
256
|
+
};
|
|
257
|
+
if (token.refreshToken) {
|
|
258
|
+
const refreshRaw = token.refreshToken;
|
|
259
|
+
const refreshHash = hashToken(refreshRaw);
|
|
260
|
+
const refreshExpiresAt = token.refreshTokenExpiresAt ?? new Date(now.getTime() + REFRESH_TOKEN_LIFETIME_SECONDS * 1e3);
|
|
261
|
+
await controller.create({
|
|
262
|
+
collection: "token",
|
|
263
|
+
data: {
|
|
264
|
+
client: client.clientId,
|
|
265
|
+
consumedAt: null,
|
|
266
|
+
data: {},
|
|
267
|
+
email: null,
|
|
268
|
+
expiresAt: refreshExpiresAt,
|
|
269
|
+
lastUsedAt: null,
|
|
270
|
+
name: null,
|
|
271
|
+
revoked: false,
|
|
272
|
+
revokedAt: null,
|
|
273
|
+
rotatedFrom: null,
|
|
274
|
+
scopes: token.scope ?? [],
|
|
275
|
+
tokenHash: refreshHash,
|
|
276
|
+
type: "refresh",
|
|
277
|
+
user: (user == null ? void 0 : user.id) ?? null
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
result.refreshToken = refreshRaw;
|
|
281
|
+
result.refreshTokenExpiresAt = refreshExpiresAt;
|
|
282
|
+
}
|
|
283
|
+
;
|
|
284
|
+
return result;
|
|
285
|
+
},
|
|
286
|
+
getAccessToken: async (accessToken) => {
|
|
287
|
+
const tokenHash = hashToken(accessToken);
|
|
288
|
+
const now = /* @__PURE__ */ new Date();
|
|
289
|
+
const record = await controller.get({
|
|
290
|
+
collection: "token",
|
|
291
|
+
query: {
|
|
292
|
+
expiresAt: { $gt: now },
|
|
293
|
+
revoked: false,
|
|
294
|
+
tokenHash,
|
|
295
|
+
type: "access"
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
if (!record) return false;
|
|
299
|
+
return {
|
|
300
|
+
accessToken,
|
|
301
|
+
accessTokenExpiresAt: record.expiresAt,
|
|
302
|
+
client: { id: record.client },
|
|
303
|
+
scope: record.scopes,
|
|
304
|
+
user: { id: record.user }
|
|
305
|
+
};
|
|
306
|
+
},
|
|
307
|
+
getRefreshToken: async (refreshToken) => {
|
|
308
|
+
const tokenHash = hashToken(refreshToken);
|
|
309
|
+
const record = await controller.get({
|
|
310
|
+
collection: "token",
|
|
311
|
+
query: {
|
|
312
|
+
revoked: false,
|
|
313
|
+
tokenHash,
|
|
314
|
+
type: "refresh"
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
if (!record) return false;
|
|
318
|
+
return {
|
|
319
|
+
client: { id: record.client },
|
|
320
|
+
refreshToken,
|
|
321
|
+
refreshTokenExpiresAt: record.expiresAt,
|
|
322
|
+
scope: record.scopes,
|
|
323
|
+
user: { id: record.user }
|
|
324
|
+
};
|
|
325
|
+
},
|
|
326
|
+
revokeToken: async (token) => {
|
|
327
|
+
const rawToken = token.refreshToken ?? token.accessToken;
|
|
328
|
+
const tokenHash = hashToken(rawToken);
|
|
329
|
+
await controller.update({
|
|
330
|
+
collection: "token",
|
|
331
|
+
data: {
|
|
332
|
+
$set: {
|
|
333
|
+
revoked: true,
|
|
334
|
+
revokedAt: /* @__PURE__ */ new Date()
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
query: { tokenHash }
|
|
338
|
+
});
|
|
339
|
+
return true;
|
|
340
|
+
},
|
|
341
|
+
verifyScope: (token, scope) => {
|
|
342
|
+
const granted = intersect(scope, token.scope ?? []);
|
|
343
|
+
return granted.length > 0;
|
|
344
|
+
},
|
|
345
|
+
// client_credentials grant: treat the client as the resource owner.
|
|
346
|
+
getUserFromClient: (client) => ({ id: client.id, type: "client" })
|
|
347
|
+
});
|
|
348
|
+
var createOAuthServer = ({ controller }) => new import_oauth2_server.default({
|
|
349
|
+
accessTokenLifetime: ACCESS_TOKEN_LIFETIME_SECONDS,
|
|
350
|
+
allowBearerTokensInQueryString: false,
|
|
351
|
+
model: createOAuthModel({ controller }),
|
|
352
|
+
refreshTokenLifetime: REFRESH_TOKEN_LIFETIME_SECONDS,
|
|
353
|
+
requireClientAuthentication: {
|
|
354
|
+
authorization_code: true,
|
|
355
|
+
client_credentials: true,
|
|
356
|
+
refresh_token: true
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
var createClient = async ({
|
|
360
|
+
allowedScopes = [],
|
|
361
|
+
controller,
|
|
362
|
+
createdBy = null,
|
|
363
|
+
description = null,
|
|
364
|
+
grantTypes = ["authorization_code"],
|
|
365
|
+
name,
|
|
366
|
+
organization = null,
|
|
367
|
+
redirectUris = [],
|
|
368
|
+
webOrigins = []
|
|
369
|
+
}) => {
|
|
370
|
+
const invalid = allowedScopes.filter((scope) => !developer.includes(scope));
|
|
371
|
+
if (invalid.length > 0) {
|
|
372
|
+
const error = new Error("Scopes not permitted for developer apps: " + invalid.join(", "));
|
|
373
|
+
error.status = 400;
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
;
|
|
377
|
+
const rawClientId = generateToken();
|
|
378
|
+
const clientId = "drawbridge." + rawClientId;
|
|
379
|
+
const rawSecret = generateToken();
|
|
380
|
+
const secretHash = hashToken(rawSecret);
|
|
381
|
+
const now = /* @__PURE__ */ new Date();
|
|
382
|
+
const doc = await controller.insert({
|
|
383
|
+
collection: "oauth",
|
|
384
|
+
data: {
|
|
385
|
+
allowedScopes,
|
|
386
|
+
clientId,
|
|
387
|
+
clientSecretHashes: [
|
|
388
|
+
{
|
|
389
|
+
createdAt: now,
|
|
390
|
+
hash: secretHash,
|
|
391
|
+
retiredAt: null
|
|
392
|
+
}
|
|
393
|
+
],
|
|
394
|
+
clientType: "confidential",
|
|
395
|
+
createdBy,
|
|
396
|
+
description,
|
|
397
|
+
grantTypes,
|
|
398
|
+
name,
|
|
399
|
+
organization,
|
|
400
|
+
rateLimitOverrides: null,
|
|
401
|
+
redirectUris,
|
|
402
|
+
status: "active",
|
|
403
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
404
|
+
webOrigins
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
return {
|
|
408
|
+
client: doc,
|
|
409
|
+
rawSecret
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
413
|
+
0 && (module.exports = {
|
|
414
|
+
createClient,
|
|
415
|
+
createOAuthModel,
|
|
416
|
+
createOAuthServer
|
|
417
|
+
});
|