@terreno/api 57.16.2 → 57.18.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/dist/actions.d.ts +26 -3
- package/dist/actions.js +102 -16
- package/dist/api.d.ts +7 -0
- package/dist/api.js +59 -3
- package/dist/auth.d.ts +28 -4
- package/dist/auth.js +140 -15
- package/dist/authRecovery.d.ts +31 -0
- package/dist/authRecovery.js +86 -6
- package/dist/betterAuth.d.ts +86 -0
- package/dist/betterAuthApp.d.ts +17 -0
- package/dist/betterAuthApp.js +12 -1
- package/dist/betterAuthOAuthHandoff.d.ts +24 -0
- package/dist/betterAuthOAuthHandoff.js +305 -0
- package/dist/betterAuthSetup.d.ts +37 -2
- package/dist/betterAuthSetup.js +304 -24
- package/dist/expressServer.d.ts +16 -0
- package/dist/expressServer.js +61 -14
- package/dist/githubAuth.d.ts +9 -0
- package/dist/githubAuth.js +230 -31
- package/dist/index.d.ts +9 -1
- package/dist/index.js +27 -3
- package/dist/mcp/actionTools.d.ts +4 -0
- package/dist/mcp/actionTools.js +158 -0
- package/dist/mcp/auth.d.ts +30 -0
- package/dist/mcp/auth.js +83 -28
- package/dist/mcp/createMCPRequest.d.ts +5 -1
- package/dist/mcp/createMCPRequest.js +3 -3
- package/dist/mcp/handlers.d.ts +3 -0
- package/dist/mcp/handlers.js +43 -27
- package/dist/mcp/oauth.d.ts +14 -0
- package/dist/mcp/oauth.js +618 -0
- package/dist/mcp/oauthMetadata.d.ts +89 -0
- package/dist/mcp/oauthMetadata.js +137 -0
- package/dist/mcp/registry.d.ts +5 -0
- package/dist/mcp/registry.js +9 -1
- package/dist/mcp/server.d.ts +6 -0
- package/dist/mcp/server.js +44 -7
- package/dist/mcp/serviceTokens.js +12 -2
- package/dist/mcp/toolExecution.d.ts +12 -0
- package/dist/mcp/toolExecution.js +60 -0
- package/dist/mcp/toolGenerator.d.ts +4 -1
- package/dist/mcp/toolGenerator.js +26 -60
- package/dist/mcp/types.d.ts +4 -4
- package/dist/models/mcpOAuthAuthorization.d.ts +2 -0
- package/dist/models/mcpOAuthAuthorization.js +86 -0
- package/dist/models/mcpOAuthClient.d.ts +2 -0
- package/dist/models/mcpOAuthClient.js +42 -0
- package/dist/models/mcpOAuthRevokedFamily.d.ts +2 -0
- package/dist/models/mcpOAuthRevokedFamily.js +36 -0
- package/dist/models/mcpOAuthToken.d.ts +12 -0
- package/dist/models/mcpOAuthToken.js +159 -0
- package/dist/models/mcpServiceToken.d.ts +1 -0
- package/dist/models/mcpServiceToken.js +21 -4
- package/dist/permissions.d.ts +7 -0
- package/dist/permissions.js +18 -1
- package/dist/rateLimit/policies.d.ts +4 -0
- package/dist/rateLimit/policies.js +46 -7
- package/dist/rbac/access.js +1 -0
- package/dist/rbac/membershipOrgAdminSource.d.ts +4 -0
- package/dist/rbac/membershipOrgAdminSource.js +5 -0
- package/dist/rbac/modelRouterAccess.d.ts +7 -1
- package/dist/rbac/modelRouterAccess.js +37 -24
- package/dist/rbac/realtimeAccess.js +17 -3
- package/dist/rbac/resolve.js +66 -9
- package/dist/rbac/roleManager.js +60 -5
- package/dist/rbac/roleModel.js +1 -1
- package/dist/rbac/statements.d.ts +1 -1
- package/dist/rbac/statements.js +13 -1
- package/dist/rbac/types.d.ts +12 -0
- package/dist/rbac/userWriteGuards.d.ts +50 -0
- package/dist/rbac/userWriteGuards.js +156 -0
- package/dist/realtime/changeStreamWatcher.d.ts +4 -1
- package/dist/realtime/changeStreamWatcher.js +157 -73
- package/dist/realtime/queryMatcher.d.ts +3 -0
- package/dist/realtime/queryMatcher.js +25 -10
- package/dist/realtime/realtimeApp.d.ts +16 -1
- package/dist/realtime/realtimeApp.js +76 -8
- package/dist/realtime/socketAuth.d.ts +17 -6
- package/dist/realtime/socketAuth.js +77 -16
- package/dist/realtime/types.d.ts +8 -5
- package/dist/requestContext.d.ts +22 -0
- package/dist/requestContext.js +22 -1
- package/dist/seedRunner.js +3 -1
- package/dist/sortFields.d.ts +33 -0
- package/dist/sortFields.js +55 -0
- package/dist/sync/executors.js +140 -2
- package/dist/sync/models.d.ts +2 -0
- package/dist/sync/models.js +5 -0
- package/dist/sync/mutationHandler.js +70 -7
- package/dist/sync/routes.js +4 -2
- package/dist/sync/syncSeqPlugin.js +6 -5
- package/dist/terrenoApp.d.ts +14 -0
- package/dist/terrenoApp.js +21 -6
- package/dist/types/mcpOAuth.d.ts +101 -0
- package/dist/types/mcpOAuth.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +8 -0
- package/package.json +3 -3
- package/src/actions.test.ts +118 -0
- package/src/actions.ts +139 -17
- package/src/api.arrayOperations.test.ts +78 -0
- package/src/api.test.ts +36 -0
- package/src/api.ts +76 -4
- package/src/auth.test.ts +521 -3
- package/src/auth.ts +171 -17
- package/src/authRecovery.test.ts +99 -0
- package/src/authRecovery.ts +117 -5
- package/src/authVerification.test.ts +3 -3
- package/src/betterAuth.ts +104 -1
- package/src/betterAuthApp.test.ts +634 -1
- package/src/betterAuthApp.ts +28 -0
- package/src/betterAuthOAuthHandoff.test.ts +946 -0
- package/src/betterAuthOAuthHandoff.ts +375 -0
- package/src/betterAuthSetup.test.ts +393 -4
- package/src/betterAuthSetup.ts +402 -29
- package/src/expressServer.test.ts +93 -0
- package/src/expressServer.ts +81 -14
- package/src/githubAuth.test.ts +448 -6
- package/src/githubAuth.ts +276 -34
- package/src/index.ts +35 -1
- package/src/mcp/actionTools.test.ts +498 -0
- package/src/mcp/actionTools.ts +220 -0
- package/src/mcp/auth.test.ts +114 -1
- package/src/mcp/auth.ts +122 -35
- package/src/mcp/createMCPRequest.ts +8 -2
- package/src/mcp/handlers.test.ts +27 -0
- package/src/mcp/handlers.ts +21 -6
- package/src/mcp/oauth.test.ts +1026 -0
- package/src/mcp/oauth.ts +798 -0
- package/src/mcp/oauthMetadata.ts +234 -0
- package/src/mcp/registry.ts +11 -0
- package/src/mcp/server.ts +54 -14
- package/src/mcp/serviceTokens.ts +16 -3
- package/src/mcp/toolExecution.ts +67 -0
- package/src/mcp/toolGenerator.ts +27 -83
- package/src/mcp/types.ts +4 -4
- package/src/models/mcpOAuthAuthorization.ts +97 -0
- package/src/models/mcpOAuthClient.ts +49 -0
- package/src/models/mcpOAuthRevokedFamily.ts +47 -0
- package/src/models/mcpOAuthToken.test.ts +82 -0
- package/src/models/mcpOAuthToken.ts +195 -0
- package/src/models/mcpServiceToken.ts +26 -2
- package/src/permissions.ts +17 -0
- package/src/rateLimit/policies.ts +54 -10
- package/src/rateLimit/rateLimit.test.ts +41 -0
- package/src/rbac/access.ts +1 -0
- package/src/rbac/membershipOrgAdminSource.ts +5 -0
- package/src/rbac/modelRouterAccess.ts +40 -23
- package/src/rbac/realtimeAccess.test.ts +43 -0
- package/src/rbac/realtimeAccess.ts +23 -3
- package/src/rbac/resolve.test.ts +179 -1
- package/src/rbac/resolve.ts +78 -9
- package/src/rbac/roleManager.test.ts +132 -0
- package/src/rbac/roleManager.ts +69 -5
- package/src/rbac/roleModel.ts +1 -1
- package/src/rbac/statements.ts +14 -1
- package/src/rbac/types.ts +12 -0
- package/src/rbac/userWriteGuards.test.ts +407 -0
- package/src/rbac/userWriteGuards.ts +185 -0
- package/src/realtime/changeStreamWatcher.ts +203 -75
- package/src/realtime/queryMatcher.ts +30 -13
- package/src/realtime/realtime.test.ts +132 -0
- package/src/realtime/realtimeApp.ts +94 -10
- package/src/realtime/realtimeAppSetup.test.ts +282 -1
- package/src/realtime/socketAuth.test.ts +140 -8
- package/src/realtime/socketAuth.ts +90 -19
- package/src/realtime/types.ts +8 -5
- package/src/requestContext.ts +52 -1
- package/src/requestCredential.test.ts +89 -0
- package/src/seedRunner.test.ts +21 -0
- package/src/seedRunner.ts +3 -1
- package/src/sortFields.ts +69 -0
- package/src/sync/executors.test.ts +382 -3
- package/src/sync/executors.ts +179 -4
- package/src/sync/models.ts +8 -0
- package/src/sync/mutationHandler.test.ts +166 -2
- package/src/sync/mutationHandler.ts +97 -5
- package/src/sync/routes.ts +4 -2
- package/src/sync/syncFrontier.test.ts +11 -0
- package/src/sync/syncPhaseC.test.ts +18 -0
- package/src/sync/syncSeqPlugin.ts +6 -5
- package/src/sync/syncSocket.test.ts +92 -0
- package/src/terrenoApp.ts +36 -21
- package/src/types/mcpOAuth.ts +129 -0
- package/src/types/mcpServiceToken.ts +6 -1
package/src/githubAuth.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import {createHmac, randomBytes, timingSafeEqual} from "node:crypto";
|
|
1
2
|
import type express from "express";
|
|
2
3
|
import {Router} from "express";
|
|
4
|
+
import {DateTime} from "luxon";
|
|
3
5
|
import type {Schema} from "mongoose";
|
|
4
6
|
import passport from "passport";
|
|
5
7
|
import {Strategy as GitHubStrategy, type Profile} from "passport-github2";
|
|
6
|
-
import {generateTokens, type User, type UserModel} from "./auth";
|
|
8
|
+
import {generateTokens, isUserDisabled, type User, type UserModel} from "./auth";
|
|
7
9
|
import {APIError} from "./errors";
|
|
8
10
|
import type {AuthOptions} from "./expressServer";
|
|
9
11
|
import {logger} from "./logger";
|
|
@@ -25,9 +27,18 @@ export interface GitHubAuthOptions {
|
|
|
25
27
|
* Defaults to true.
|
|
26
28
|
*/
|
|
27
29
|
allowAccountLinking?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Redirect targets allowed for the `returnTo` query parameter of `GET /auth/github`. Each entry
|
|
32
|
+
* is an exact http(s) origin (`https://app.example.com`) or an exact custom-scheme authority
|
|
33
|
+
* (`myapp://auth`). A `returnTo` that matches no entry is rejected with 400. When this is
|
|
34
|
+
* unset or empty, `returnTo` is ignored and the callback always returns JSON.
|
|
35
|
+
*/
|
|
36
|
+
allowedReturnTo?: string[];
|
|
28
37
|
/**
|
|
29
38
|
* Custom function to handle user creation or lookup from GitHub profile.
|
|
30
39
|
* If not provided, a default implementation will be used.
|
|
40
|
+
* `existingUser` is the user who started `GET /auth/github/link`, read from the verified
|
|
41
|
+
* OAuth state; it is never taken from a session or token on the callback request.
|
|
31
42
|
*/
|
|
32
43
|
findOrCreateUser?: (
|
|
33
44
|
profile: Profile,
|
|
@@ -77,6 +88,207 @@ export const githubUserPlugin = (schema: Schema<any, any, any, any>): void => {
|
|
|
77
88
|
});
|
|
78
89
|
};
|
|
79
90
|
|
|
91
|
+
/** Cookie that binds a GitHub OAuth flow to the browser that started it. */
|
|
92
|
+
const GITHUB_STATE_COOKIE = "terreno_github_oauth_state";
|
|
93
|
+
/** How long a started GitHub OAuth flow stays valid. */
|
|
94
|
+
const GITHUB_STATE_TTL_MINUTES = 10;
|
|
95
|
+
const STATE_VERIFY_FAILURE = {message: "Unable to verify authorization request state."};
|
|
96
|
+
|
|
97
|
+
interface GitHubOAuthState {
|
|
98
|
+
expiresAt: number;
|
|
99
|
+
linkUserId?: string;
|
|
100
|
+
nonce: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type StateStoreCallback = (error: unknown, ok?: unknown, info?: unknown) => void;
|
|
104
|
+
|
|
105
|
+
interface GitHubStateStore {
|
|
106
|
+
store: (
|
|
107
|
+
req: express.Request,
|
|
108
|
+
state: unknown,
|
|
109
|
+
meta: unknown,
|
|
110
|
+
callback: StateStoreCallback
|
|
111
|
+
) => void;
|
|
112
|
+
verify: (
|
|
113
|
+
req: express.Request,
|
|
114
|
+
providedState: unknown,
|
|
115
|
+
meta: unknown,
|
|
116
|
+
callback: StateStoreCallback
|
|
117
|
+
) => void;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Link targets read from verified state, keyed by request so no request input can set one.
|
|
121
|
+
const verifiedLinkTargets = new WeakMap<express.Request, string>();
|
|
122
|
+
|
|
123
|
+
const getStateSecret = (): string => {
|
|
124
|
+
const secret = process.env.SESSION_SECRET || process.env.TOKEN_SECRET;
|
|
125
|
+
if (!secret) {
|
|
126
|
+
throw new APIError({
|
|
127
|
+
status: 500,
|
|
128
|
+
title: "SESSION_SECRET or TOKEN_SECRET must be set for GitHub OAuth.",
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return secret;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const signStatePayload = (payload: string): string => {
|
|
135
|
+
return createHmac("sha256", getStateSecret()).update(payload).digest("base64url");
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const safeEqual = (a: string, b: string): boolean => {
|
|
139
|
+
const aBuffer = Buffer.from(a);
|
|
140
|
+
const bBuffer = Buffer.from(b);
|
|
141
|
+
if (aBuffer.length !== bBuffer.length) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return timingSafeEqual(aBuffer, bBuffer);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const readCookie = (req: express.Request, name: string): string | undefined => {
|
|
148
|
+
const header = req.headers?.cookie;
|
|
149
|
+
if (!header) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
for (const part of header.split(";")) {
|
|
153
|
+
const [rawName, ...rest] = part.trim().split("=");
|
|
154
|
+
if (rawName === name) {
|
|
155
|
+
try {
|
|
156
|
+
return decodeURIComponent(rest.join("="));
|
|
157
|
+
} catch {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const parseStateCookie = (raw: string | undefined): GitHubOAuthState | undefined => {
|
|
166
|
+
if (!raw) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
const [payload, signature, ...extra] = raw.split(".");
|
|
170
|
+
if (!payload || !signature || extra.length > 0) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
if (!safeEqual(signature, signStatePayload(payload))) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
const parsed = JSON.parse(Buffer.from(payload, "base64url").toString("utf8")) as unknown;
|
|
178
|
+
if (!parsed || typeof parsed !== "object") {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
const {expiresAt, linkUserId, nonce} = parsed as Record<string, unknown>;
|
|
182
|
+
if (typeof expiresAt !== "number" || typeof nonce !== "string") {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
if (linkUserId !== undefined && typeof linkUserId !== "string") {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
return {expiresAt, linkUserId, nonce};
|
|
189
|
+
} catch {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const isHttpsUrl = (value: string): boolean | undefined => {
|
|
195
|
+
try {
|
|
196
|
+
return new URL(value).protocol === "https:";
|
|
197
|
+
} catch {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* passport-oauth2 state store backed by a signed, HttpOnly, SameSite=Lax cookie. `store` issues a
|
|
204
|
+
* nonce for the `state` parameter and records it (and the link target for /auth/github/link) in
|
|
205
|
+
* the cookie; `verify` accepts the callback only when the cookie is intact, unexpired, and holds
|
|
206
|
+
* the same nonce. The cookie is single use.
|
|
207
|
+
*/
|
|
208
|
+
const createSignedCookieStateStore = (callbackURL: string): GitHubStateStore => {
|
|
209
|
+
const callbackIsHttps = isHttpsUrl(callbackURL);
|
|
210
|
+
const cookieOptions = (req: express.Request): express.CookieOptions => ({
|
|
211
|
+
httpOnly: true,
|
|
212
|
+
path: "/",
|
|
213
|
+
sameSite: "lax",
|
|
214
|
+
secure: callbackIsHttps ?? req.secure,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
store: (req, state, _meta, callback): void => {
|
|
219
|
+
if (!req.res) {
|
|
220
|
+
callback(new Error("GitHub OAuth state requires an Express response"));
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const linkUserId =
|
|
224
|
+
state && typeof state === "object" && "linkUserId" in state
|
|
225
|
+
? String((state as {linkUserId: unknown}).linkUserId)
|
|
226
|
+
: undefined;
|
|
227
|
+
const nonce = randomBytes(24).toString("base64url");
|
|
228
|
+
const record: GitHubOAuthState = {
|
|
229
|
+
expiresAt: DateTime.now().plus({minutes: GITHUB_STATE_TTL_MINUTES}).toMillis(),
|
|
230
|
+
nonce,
|
|
231
|
+
...(linkUserId ? {linkUserId} : {}),
|
|
232
|
+
};
|
|
233
|
+
const payload = Buffer.from(JSON.stringify(record)).toString("base64url");
|
|
234
|
+
req.res.cookie(GITHUB_STATE_COOKIE, `${payload}.${signStatePayload(payload)}`, {
|
|
235
|
+
...cookieOptions(req),
|
|
236
|
+
maxAge: GITHUB_STATE_TTL_MINUTES * 60 * 1000,
|
|
237
|
+
});
|
|
238
|
+
callback(null, nonce);
|
|
239
|
+
},
|
|
240
|
+
verify: (req, providedState, _meta, callback): void => {
|
|
241
|
+
const record = parseStateCookie(readCookie(req, GITHUB_STATE_COOKIE));
|
|
242
|
+
req.res?.clearCookie(GITHUB_STATE_COOKIE, cookieOptions(req));
|
|
243
|
+
if (!record || typeof providedState !== "string") {
|
|
244
|
+
callback(null, false, STATE_VERIFY_FAILURE);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (record.expiresAt <= DateTime.now().toMillis()) {
|
|
248
|
+
callback(null, false, STATE_VERIFY_FAILURE);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (!safeEqual(record.nonce, providedState)) {
|
|
252
|
+
callback(null, false, STATE_VERIFY_FAILURE);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (record.linkUserId) {
|
|
256
|
+
verifiedLinkTargets.set(req, record.linkUserId);
|
|
257
|
+
}
|
|
258
|
+
callback(null, true);
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// http(s) targets compare by origin; custom schemes compare by scheme plus authority.
|
|
264
|
+
const returnToKey = (value: string): string | undefined => {
|
|
265
|
+
let url: URL;
|
|
266
|
+
try {
|
|
267
|
+
url = new URL(value);
|
|
268
|
+
} catch {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
if (url.protocol === "http:" || url.protocol === "https:") {
|
|
272
|
+
return url.origin.toLowerCase();
|
|
273
|
+
}
|
|
274
|
+
if (!url.host || url.username || url.password) {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
return `${url.protocol}//${url.host}`.toLowerCase();
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/** Whether `value` is a redirect target the operator allowed in `allowedReturnTo`. */
|
|
281
|
+
const isAllowedReturnTo = (value: unknown, allowed: string[] | undefined): value is string => {
|
|
282
|
+
if (typeof value !== "string" || !allowed?.length) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
const key = returnToKey(value);
|
|
286
|
+
if (!key) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
return allowed.some((entry) => returnToKey(entry) === key);
|
|
290
|
+
};
|
|
291
|
+
|
|
80
292
|
/**
|
|
81
293
|
* Sets up GitHub OAuth authentication strategy.
|
|
82
294
|
* Call this after setupAuth() in your server initialization.
|
|
@@ -97,7 +309,8 @@ export const setupGitHubAuth = (
|
|
|
97
309
|
clientSecret: githubOptions.clientSecret,
|
|
98
310
|
passReqToCallback: true,
|
|
99
311
|
scope,
|
|
100
|
-
|
|
312
|
+
store: createSignedCookieStateStore(githubOptions.callbackURL),
|
|
313
|
+
} as ConstructorParameters<typeof GitHubStrategy>[0],
|
|
101
314
|
async (
|
|
102
315
|
req: express.Request,
|
|
103
316
|
accessToken: string,
|
|
@@ -106,7 +319,13 @@ export const setupGitHubAuth = (
|
|
|
106
319
|
done: (error: unknown, user?: unknown) => void
|
|
107
320
|
) => {
|
|
108
321
|
try {
|
|
109
|
-
|
|
322
|
+
// The link target comes only from state issued by /auth/github/link, never from an
|
|
323
|
+
// ambient session or token on the callback request.
|
|
324
|
+
const linkUserId = verifiedLinkTargets.get(req);
|
|
325
|
+
const linkUser = linkUserId ? await userModel.findById(linkUserId) : null;
|
|
326
|
+
if (linkUserId && !linkUser) {
|
|
327
|
+
return done(new APIError({status: 404, title: "User not found"}));
|
|
328
|
+
}
|
|
110
329
|
|
|
111
330
|
// If custom handler provided, use it
|
|
112
331
|
if (githubOptions.findOrCreateUser) {
|
|
@@ -114,7 +333,7 @@ export const setupGitHubAuth = (
|
|
|
114
333
|
profile,
|
|
115
334
|
accessToken,
|
|
116
335
|
refreshToken,
|
|
117
|
-
|
|
336
|
+
(linkUser ?? undefined) as Record<string, unknown> | undefined
|
|
118
337
|
);
|
|
119
338
|
return done(null, user);
|
|
120
339
|
}
|
|
@@ -125,15 +344,15 @@ export const setupGitHubAuth = (
|
|
|
125
344
|
// Check if user with this GitHub ID already exists
|
|
126
345
|
const existingGitHubUser = await findOneOrNoneFor(userModel, {githubId});
|
|
127
346
|
|
|
128
|
-
// Case 1:
|
|
129
|
-
if (
|
|
347
|
+
// Case 1: The user who started /auth/github/link is linking their GitHub account
|
|
348
|
+
if (linkUser) {
|
|
130
349
|
if (!githubOptions.allowAccountLinking) {
|
|
131
350
|
return done(new APIError({status: 400, title: "Account linking is disabled"}));
|
|
132
351
|
}
|
|
133
352
|
|
|
134
353
|
if (
|
|
135
354
|
existingGitHubUser &&
|
|
136
|
-
existingGitHubUser._id.toString() !==
|
|
355
|
+
existingGitHubUser._id.toString() !== linkUser._id.toString()
|
|
137
356
|
) {
|
|
138
357
|
return done(
|
|
139
358
|
new APIError({
|
|
@@ -143,18 +362,13 @@ export const setupGitHubAuth = (
|
|
|
143
362
|
);
|
|
144
363
|
}
|
|
145
364
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
userWithGitHub.githubAvatarUrl = profile.photos?.[0]?.value;
|
|
154
|
-
await user.save();
|
|
155
|
-
return done(null, user);
|
|
156
|
-
}
|
|
157
|
-
return done(new APIError({status: 404, title: "User not found"}));
|
|
365
|
+
const userWithGitHub = linkUser as unknown as GitHubUserFields;
|
|
366
|
+
userWithGitHub.githubId = githubId;
|
|
367
|
+
userWithGitHub.githubUsername = profile.username;
|
|
368
|
+
userWithGitHub.githubProfileUrl = profile.profileUrl;
|
|
369
|
+
userWithGitHub.githubAvatarUrl = profile.photos?.[0]?.value;
|
|
370
|
+
await linkUser.save();
|
|
371
|
+
return done(null, linkUser);
|
|
158
372
|
}
|
|
159
373
|
|
|
160
374
|
// Case 2: User with this GitHub ID exists - log them in
|
|
@@ -231,14 +445,24 @@ export const addGitHubAuthRoutes = (
|
|
|
231
445
|
router.get(
|
|
232
446
|
"/github",
|
|
233
447
|
(req: express.Request, _res: express.Response, next: express.NextFunction) => {
|
|
234
|
-
// Store
|
|
235
|
-
const returnTo = req.query.returnTo
|
|
236
|
-
if (returnTo) {
|
|
237
|
-
|
|
238
|
-
reqWithSession.session = reqWithSession.session ?? {};
|
|
239
|
-
reqWithSession.session.returnTo = returnTo;
|
|
448
|
+
// Store an allowlisted return URL in the session for the redirect after auth
|
|
449
|
+
const returnTo = req.query.returnTo;
|
|
450
|
+
if (returnTo === undefined || returnTo === "") {
|
|
451
|
+
return next();
|
|
240
452
|
}
|
|
241
|
-
|
|
453
|
+
if (!githubOptions.allowedReturnTo?.length) {
|
|
454
|
+
logger.warn("[github] Ignoring returnTo because githubAuth.allowedReturnTo is not set");
|
|
455
|
+
return next();
|
|
456
|
+
}
|
|
457
|
+
if (!isAllowedReturnTo(returnTo, githubOptions.allowedReturnTo)) {
|
|
458
|
+
return next(
|
|
459
|
+
new APIError({status: 400, title: "returnTo is not an allowed redirect target"})
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
const reqWithSession = req as express.Request & {session?: {returnTo?: string}};
|
|
463
|
+
reqWithSession.session = reqWithSession.session ?? {};
|
|
464
|
+
reqWithSession.session.returnTo = returnTo;
|
|
465
|
+
return next();
|
|
242
466
|
},
|
|
243
467
|
passport.authenticate("github", {session: false})
|
|
244
468
|
);
|
|
@@ -251,21 +475,33 @@ export const addGitHubAuthRoutes = (
|
|
|
251
475
|
session: false,
|
|
252
476
|
}),
|
|
253
477
|
async (req: express.Request, res: express.Response) => {
|
|
478
|
+
if (isUserDisabled(req.user)) {
|
|
479
|
+
logger.warn(`[github] Refusing GitHub login for disabled user ${req.user?._id}`);
|
|
480
|
+
return res.status(401).json({status: 401, title: "User is disabled"});
|
|
481
|
+
}
|
|
254
482
|
try {
|
|
255
483
|
const tokens = await generateTokens(req.user, authOptions);
|
|
256
|
-
const
|
|
257
|
-
|
|
484
|
+
const session = (req as express.Request & {session?: {returnTo?: string}}).session;
|
|
485
|
+
const returnTo = session?.returnTo;
|
|
486
|
+
if (session && returnTo !== undefined) {
|
|
487
|
+
delete session.returnTo;
|
|
488
|
+
}
|
|
258
489
|
|
|
259
|
-
//
|
|
260
|
-
|
|
490
|
+
// Redirect only to an allowlisted target, with tokens in the fragment so they stay out
|
|
491
|
+
// of server logs and Referer headers
|
|
492
|
+
if (returnTo && isAllowedReturnTo(returnTo, githubOptions.allowedReturnTo)) {
|
|
261
493
|
const url = new URL(returnTo);
|
|
262
|
-
|
|
494
|
+
const fragment = new URLSearchParams({token: tokens.token ?? ""});
|
|
263
495
|
if (tokens.refreshToken) {
|
|
264
|
-
|
|
496
|
+
fragment.set("refreshToken", tokens.refreshToken);
|
|
265
497
|
}
|
|
266
|
-
|
|
498
|
+
fragment.set("userId", req.user?._id ? String(req.user._id) : "");
|
|
499
|
+
url.hash = fragment.toString();
|
|
267
500
|
return res.redirect(url.toString());
|
|
268
501
|
}
|
|
502
|
+
if (returnTo) {
|
|
503
|
+
logger.warn("[github] Not redirecting to a returnTo outside githubAuth.allowedReturnTo");
|
|
504
|
+
}
|
|
269
505
|
|
|
270
506
|
// Otherwise return JSON response
|
|
271
507
|
return res.json({
|
|
@@ -306,7 +542,13 @@ export const addGitHubAuthRoutes = (
|
|
|
306
542
|
}
|
|
307
543
|
)(req, res, next);
|
|
308
544
|
},
|
|
309
|
-
|
|
545
|
+
(req: express.Request, res: express.Response, next: express.NextFunction): void => {
|
|
546
|
+
// Record the bearer-authenticated user in the signed state as the only link target.
|
|
547
|
+
passport.authenticate("github", {
|
|
548
|
+
session: false,
|
|
549
|
+
state: {linkUserId: String(req.user?._id)},
|
|
550
|
+
} as unknown as passport.AuthenticateOptions)(req, res, next);
|
|
551
|
+
}
|
|
310
552
|
);
|
|
311
553
|
|
|
312
554
|
// Unlink GitHub from user account
|
package/src/index.ts
CHANGED
|
@@ -53,7 +53,22 @@ export {
|
|
|
53
53
|
export * from "./githubAuth";
|
|
54
54
|
export * from "./httpClient";
|
|
55
55
|
export * from "./logger";
|
|
56
|
-
export {
|
|
56
|
+
export {
|
|
57
|
+
extractAuthFromHeaders,
|
|
58
|
+
extractUserFromHeaders,
|
|
59
|
+
type MCPAuthContext,
|
|
60
|
+
type MCPAuthResult,
|
|
61
|
+
} from "./mcp/auth";
|
|
62
|
+
export {addMcpOAuthRoutes, type McpOAuthRoutesOptions} from "./mcp/oauth";
|
|
63
|
+
export {
|
|
64
|
+
buildAuthorizationServerMetadata,
|
|
65
|
+
buildProtectedResourceMetadata,
|
|
66
|
+
type McpOAuthAppOption,
|
|
67
|
+
type McpOAuthOptions,
|
|
68
|
+
type ResolvedMcpOAuthOptions,
|
|
69
|
+
resolveMcpOAuthOption,
|
|
70
|
+
sendMcpOAuthChallenge,
|
|
71
|
+
} from "./mcp/oauthMetadata";
|
|
57
72
|
export type {MCPCustomTool} from "./mcp/registry";
|
|
58
73
|
export {
|
|
59
74
|
clearMCPRegistry,
|
|
@@ -94,6 +109,10 @@ export * from "./migrations/schemaCatalog";
|
|
|
94
109
|
export * from "./migrations/types";
|
|
95
110
|
export * from "./models/consentForm";
|
|
96
111
|
export * from "./models/consentResponse";
|
|
112
|
+
export {McpOAuthAuthorization} from "./models/mcpOAuthAuthorization";
|
|
113
|
+
export {McpOAuthClient} from "./models/mcpOAuthClient";
|
|
114
|
+
export {McpOAuthRevokedFamily} from "./models/mcpOAuthRevokedFamily";
|
|
115
|
+
export {McpOAuthFamilyRevokedError, McpOAuthToken} from "./models/mcpOAuthToken";
|
|
97
116
|
export * from "./models/mcpServiceToken";
|
|
98
117
|
export * from "./models/notification";
|
|
99
118
|
export * from "./models/notificationPreference";
|
|
@@ -183,6 +202,14 @@ export * from "./rbac/scopes";
|
|
|
183
202
|
export * from "./rbac/statements";
|
|
184
203
|
export * from "./rbac/types";
|
|
185
204
|
export * from "./rbac/userPlugin";
|
|
205
|
+
export {
|
|
206
|
+
assertCanWriteTargetUser,
|
|
207
|
+
assertCanWriteUserIdentity,
|
|
208
|
+
isRbacUserModelName,
|
|
209
|
+
isWriteKeyForField,
|
|
210
|
+
USER_IDENTITY_FIELDS,
|
|
211
|
+
USER_UPDATE_IDENTITY_ACTION,
|
|
212
|
+
} from "./rbac/userWriteGuards";
|
|
186
213
|
export {
|
|
187
214
|
type AuthorizedEmitEntry,
|
|
188
215
|
emitPayloadToAuthorizedRoom,
|
|
@@ -292,6 +319,13 @@ export * from "./terrenoPlugin";
|
|
|
292
319
|
export * from "./transformers";
|
|
293
320
|
export * from "./types/consentForm";
|
|
294
321
|
export * from "./types/consentResponse";
|
|
322
|
+
export type {
|
|
323
|
+
McpOAuthAuthorizationDocument,
|
|
324
|
+
McpOAuthAuthorizationStatus,
|
|
325
|
+
McpOAuthClientDocument,
|
|
326
|
+
McpOAuthRevokedFamilyDocument,
|
|
327
|
+
McpOAuthTokenDocument,
|
|
328
|
+
} from "./types/mcpOAuth";
|
|
295
329
|
export * from "./utils";
|
|
296
330
|
export * from "./versionCheckPlugin";
|
|
297
331
|
export type {
|