@terreno/api 57.18.0 → 57.20.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/CHANGELOG.md +2071 -2
- package/dist/accountSecurity/accessActions.d.ts +38 -0
- package/dist/accountSecurity/accessActions.js +157 -0
- package/dist/accountSecurity/accountSecurityApp.d.ts +219 -0
- package/dist/accountSecurity/accountSecurityApp.js +438 -0
- package/dist/accountSecurity/accountSecurityService.d.ts +200 -0
- package/dist/accountSecurity/accountSecurityService.js +481 -0
- package/dist/accountSecurity/credentialActions.d.ts +16 -0
- package/dist/accountSecurity/credentialActions.js +189 -0
- package/dist/accountSecurity/membershipActions.d.ts +15 -0
- package/dist/accountSecurity/membershipActions.js +81 -0
- package/dist/accountSecurity/orgSecurityRoutes.d.ts +50 -0
- package/dist/accountSecurity/orgSecurityRoutes.js +149 -0
- package/dist/accountSecurity/securityEventHooks.d.ts +44 -0
- package/dist/accountSecurity/securityEventHooks.js +223 -0
- package/dist/accountSecurity/securityEvents.d.ts +162 -0
- package/dist/accountSecurity/securityEvents.js +114 -0
- package/dist/accountSecurity/securityNotifications.d.ts +125 -0
- package/dist/accountSecurity/securityNotifications.js +333 -0
- package/dist/api.js +38 -6
- package/dist/audit/auditEventModel.d.ts +5 -3
- package/dist/audit/auditEventModel.js +8 -6
- package/dist/audit/processQueue.js +4 -2
- package/dist/audit/record.d.ts +2 -2
- package/dist/audit/record.js +3 -0
- package/dist/authRecovery.js +29 -1
- package/dist/betterAuth.d.ts +179 -10
- package/dist/betterAuthApp.d.ts +15 -0
- package/dist/betterAuthApp.js +23 -0
- package/dist/betterAuthChallengeHeaders.d.ts +37 -0
- package/dist/betterAuthChallengeHeaders.js +180 -0
- package/dist/betterAuthFeatures.d.ts +54 -0
- package/dist/betterAuthFeatures.js +239 -0
- package/dist/betterAuthMethods.d.ts +73 -0
- package/dist/betterAuthMethods.js +55 -0
- package/dist/betterAuthOAuthHandoff.d.ts +10 -2
- package/dist/betterAuthOAuthHandoff.js +222 -24
- package/dist/betterAuthSetPassword.d.ts +33 -0
- package/dist/betterAuthSetPassword.js +166 -0
- package/dist/betterAuthSetup.d.ts +57 -8
- package/dist/betterAuthSetup.js +327 -36
- package/dist/consentApp.d.ts +9 -4
- package/dist/consentApp.js +344 -28
- package/dist/consentStatus.d.ts +60 -0
- package/dist/consentStatus.js +111 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +41 -2
- package/dist/expressServer.js +3 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +23 -3
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +28 -4
- package/dist/orgs/membershipGuards.d.ts +7 -0
- package/dist/orgs/membershipGuards.js +32 -0
- package/dist/orgs/orgsApp.d.ts +19 -1
- package/dist/orgs/orgsApp.js +55 -24
- package/dist/rateLimit/policies.js +8 -0
- package/dist/rbac/access.js +2 -1
- package/dist/rbac/resolve.d.ts +9 -1
- package/dist/rbac/resolve.js +21 -16
- package/dist/rbac/roleManager.d.ts +6 -0
- package/dist/rbac/roleManager.js +8 -7
- package/dist/rbac/roleModel.d.ts +1 -0
- package/dist/rbac/roleModel.js +45 -3
- package/dist/rbac/statements.d.ts +2 -2
- package/dist/rbac/statements.js +10 -1
- package/dist/rbac/types.d.ts +6 -0
- package/dist/rbac/userWriteGuards.js +2 -1
- package/dist/schemaMetadata.js +8 -1
- package/dist/seedRunner.js +7 -2
- package/dist/terrenoApp.d.ts +4 -0
- package/dist/terrenoApp.js +15 -0
- package/dist/tests/models.d.ts +5 -0
- package/dist/tests/models.js +8 -0
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpOAuth.d.ts +35 -2
- package/package.json +6 -3
- package/src/__snapshots__/openApi.test.ts.snap +288 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +96 -0
- package/src/accountSecurity/accessActions.test.ts +723 -0
- package/src/accountSecurity/accessActions.ts +173 -0
- package/src/accountSecurity/accountSecurity.test.ts +947 -0
- package/src/accountSecurity/accountSecurityApp.ts +720 -0
- package/src/accountSecurity/accountSecurityService.ts +826 -0
- package/src/accountSecurity/credentialActions.test.ts +867 -0
- package/src/accountSecurity/credentialActions.ts +260 -0
- package/src/accountSecurity/membershipActions.ts +89 -0
- package/src/accountSecurity/orgSecurityRoutes.test.ts +841 -0
- package/src/accountSecurity/orgSecurityRoutes.ts +232 -0
- package/src/accountSecurity/securityEventHooks.ts +294 -0
- package/src/accountSecurity/securityEvents.ts +165 -0
- package/src/accountSecurity/securityNotifications.test.ts +1323 -0
- package/src/accountSecurity/securityNotifications.ts +516 -0
- package/src/api.arrayOperations.test.ts +71 -0
- package/src/api.ts +38 -7
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/authRecovery.test.ts +23 -0
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- package/src/betterAuth.ts +194 -8
- package/src/betterAuthApp.ts +32 -0
- package/src/betterAuthChallengeHeaders.test.ts +476 -0
- package/src/betterAuthChallengeHeaders.ts +227 -0
- package/src/betterAuthFeatures.test.ts +489 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +844 -0
- package/src/betterAuthMethods.test.ts +138 -0
- package/src/betterAuthMethods.ts +98 -0
- package/src/betterAuthOAuthHandoff.test.ts +482 -69
- package/src/betterAuthOAuthHandoff.ts +293 -33
- package/src/betterAuthOAuthProxy.test.ts +421 -0
- package/src/betterAuthPasswordReset.test.ts +190 -0
- package/src/betterAuthSetPassword.test.ts +378 -0
- package/src/betterAuthSetPassword.ts +213 -0
- package/src/betterAuthSetup.test.ts +39 -7
- package/src/betterAuthSetup.ts +457 -38
- package/src/consentApp.test.ts +869 -2
- package/src/consentApp.ts +438 -36
- package/src/consentStatus.test.ts +226 -0
- package/src/consentStatus.ts +171 -0
- package/src/errors.test.ts +81 -0
- package/src/errors.ts +50 -1
- package/src/expressServer.test.ts +29 -0
- package/src/expressServer.ts +3 -2
- package/src/index.ts +30 -0
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.ts +30 -5
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +101 -2
- package/src/orgs/orgsApp.ts +69 -28
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rbac/access.ts +4 -1
- package/src/rbac/resolve.test.ts +65 -0
- package/src/rbac/resolve.ts +37 -17
- package/src/rbac/roleManager.test.ts +64 -0
- package/src/rbac/roleManager.ts +15 -6
- package/src/rbac/roleModel.test.ts +174 -1
- package/src/rbac/roleModel.ts +66 -3
- package/src/rbac/routes.test.ts +58 -0
- package/src/rbac/statements.test.ts +4 -1
- package/src/rbac/statements.ts +11 -1
- package/src/rbac/types.ts +6 -1
- package/src/rbac/userWriteGuards.test.ts +48 -0
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/terrenoApp.ts +20 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +188 -0
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +13 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpOAuth.ts +39 -3
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import {afterAll, beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
-
import {once} from "node:events";
|
|
3
|
-
import type {Server} from "node:http";
|
|
4
|
-
import type {AddressInfo} from "node:net";
|
|
5
2
|
import type {BetterAuthPlugin} from "better-auth";
|
|
6
3
|
import {createAuthMiddleware} from "better-auth/api";
|
|
7
4
|
import {genericOAuth} from "better-auth/plugins";
|
|
@@ -16,78 +13,13 @@ import {
|
|
|
16
13
|
getMongoClientFromMongoose,
|
|
17
14
|
mountBetterAuthRoutes,
|
|
18
15
|
} from "./betterAuthSetup";
|
|
16
|
+
import {type MockOidcProvider, startMockOidcProvider} from "./tests/mockOidcProvider";
|
|
19
17
|
|
|
20
18
|
const API_ORIGIN = "http://localhost:3000";
|
|
21
19
|
const WEB_ORIGIN = "https://web.example.com";
|
|
22
20
|
const PROVIDER_ID = "mock-oidc";
|
|
23
|
-
const MOCK_CODE = "mock-authorization-code";
|
|
24
|
-
const MOCK_ACCESS_TOKEN = "mock-access-token";
|
|
25
21
|
const SECRET = "test-secret-at-least-32-characters-long";
|
|
26
22
|
|
|
27
|
-
interface MockOidcUser {
|
|
28
|
-
email: string;
|
|
29
|
-
id: string;
|
|
30
|
-
name: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface MockOidcProvider {
|
|
34
|
-
close: () => Promise<void>;
|
|
35
|
-
issuer: string;
|
|
36
|
-
/** The user the next sign-in authenticates as. */
|
|
37
|
-
setUser: (user: MockOidcUser) => void;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// A minimal OIDC provider: discovery, an authorize step that approves at once, a token
|
|
41
|
-
// endpoint, and userinfo. Real enough for Better Auth's generic OAuth code flow with PKCE.
|
|
42
|
-
const startMockOidcProvider = async (): Promise<MockOidcProvider> => {
|
|
43
|
-
let issuer = "";
|
|
44
|
-
let user: MockOidcUser = {email: "oidc@example.com", id: "oidc-user", name: "OIDC User"};
|
|
45
|
-
const app = express();
|
|
46
|
-
app.use(express.urlencoded({extended: false}));
|
|
47
|
-
app.get("/.well-known/openid-configuration", (_req, res) => {
|
|
48
|
-
res.json({
|
|
49
|
-
authorization_endpoint: `${issuer}/authorize`,
|
|
50
|
-
issuer,
|
|
51
|
-
response_types_supported: ["code"],
|
|
52
|
-
token_endpoint: `${issuer}/token`,
|
|
53
|
-
userinfo_endpoint: `${issuer}/userinfo`,
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
app.get("/authorize", (req, res) => {
|
|
57
|
-
const redirect = new URL(String(req.query.redirect_uri));
|
|
58
|
-
redirect.searchParams.set("code", MOCK_CODE);
|
|
59
|
-
redirect.searchParams.set("state", String(req.query.state));
|
|
60
|
-
res.redirect(redirect.toString());
|
|
61
|
-
});
|
|
62
|
-
app.post("/token", (req, res) => {
|
|
63
|
-
if (req.body.code !== MOCK_CODE) {
|
|
64
|
-
res.status(400).json({error: "invalid_grant"});
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
res.json({access_token: MOCK_ACCESS_TOKEN, expires_in: 3600, token_type: "Bearer"});
|
|
68
|
-
});
|
|
69
|
-
app.get("/userinfo", (req, res) => {
|
|
70
|
-
if (req.headers.authorization !== `Bearer ${MOCK_ACCESS_TOKEN}`) {
|
|
71
|
-
res.status(401).json({error: "invalid_token"});
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
res.json({...user, email_verified: true, sub: user.id});
|
|
75
|
-
});
|
|
76
|
-
const server: Server = app.listen(0, "127.0.0.1");
|
|
77
|
-
await once(server, "listening");
|
|
78
|
-
issuer = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
|
79
|
-
return {
|
|
80
|
-
close: async (): Promise<void> => {
|
|
81
|
-
server.close();
|
|
82
|
-
await once(server, "close");
|
|
83
|
-
},
|
|
84
|
-
issuer,
|
|
85
|
-
setUser: (next: MockOidcUser): void => {
|
|
86
|
-
user = next;
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
|
|
91
23
|
// Lets a case replace what Better Auth put on the callback response before the handoff hook
|
|
92
24
|
// reads it, to probe the hook with redirects Better Auth's own origin check would refuse.
|
|
93
25
|
interface CallbackOverride {
|
|
@@ -944,3 +876,484 @@ describe("oauthHandoff callbackPaths", () => {
|
|
|
944
876
|
});
|
|
945
877
|
}
|
|
946
878
|
});
|
|
879
|
+
|
|
880
|
+
describe("linking a provider through the handoff", () => {
|
|
881
|
+
const LINK_TOKEN_ROWS = {identifier: {$regex: /^terreno-link-intent:/}};
|
|
882
|
+
const START_URL = `${API_ORIGIN}/api/auth/oauth-handoff/link-start`;
|
|
883
|
+
|
|
884
|
+
const betterAuthRows = (name: string): ReturnType<mongoose.mongo.Db["collection"]> => {
|
|
885
|
+
const db = mongoose.connection.db;
|
|
886
|
+
if (!db) {
|
|
887
|
+
throw new Error("The test database is not connected");
|
|
888
|
+
}
|
|
889
|
+
return db.collection(name);
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
interface SignedInUser {
|
|
893
|
+
bearer: string;
|
|
894
|
+
cookie: string;
|
|
895
|
+
email: string;
|
|
896
|
+
userId: string;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// Signs up with email and password; the provider then reports the same email, verified.
|
|
900
|
+
const signUpUser = async (app: express.Application): Promise<SignedInUser> => {
|
|
901
|
+
const email = `linker-${Math.random().toString(36).slice(2)}@example.com`;
|
|
902
|
+
provider.setUser({email, id: `oidc-${Math.random().toString(36).slice(2)}`, name: "Linker"});
|
|
903
|
+
const signUp = await supertest(app)
|
|
904
|
+
.post("/api/auth/sign-up/email")
|
|
905
|
+
.set("origin", WEB_ORIGIN)
|
|
906
|
+
.set("content-type", "application/json")
|
|
907
|
+
.send({email, name: "Linker", password: "password-123456"});
|
|
908
|
+
expect(signUp.status).toBe(200);
|
|
909
|
+
const bearer = signUp.headers["set-auth-token"] as string;
|
|
910
|
+
expect(bearer).toBeTruthy();
|
|
911
|
+
return {bearer, cookie: cookieHeaderFrom(signUp), email, userId: signUp.body.user.id};
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
// The web app's cross-site fetch, authenticated with its bearer.
|
|
915
|
+
const linkIntent = (
|
|
916
|
+
app: express.Application,
|
|
917
|
+
bearer: string,
|
|
918
|
+
body: Record<string, string>
|
|
919
|
+
): supertest.Test =>
|
|
920
|
+
supertest(app)
|
|
921
|
+
.post("/api/auth/oauth-handoff/link-intent")
|
|
922
|
+
.set("origin", WEB_ORIGIN)
|
|
923
|
+
.set("sec-fetch-site", "cross-site")
|
|
924
|
+
.set("sec-fetch-mode", "cors")
|
|
925
|
+
.set("authorization", `Bearer ${bearer}`)
|
|
926
|
+
.set("content-type", "application/json")
|
|
927
|
+
.send(body);
|
|
928
|
+
|
|
929
|
+
// The hidden form's top-level POST navigation from the web app: no cookie, no bearer.
|
|
930
|
+
const linkStart = (app: express.Application, token: string): supertest.Test =>
|
|
931
|
+
supertest(app)
|
|
932
|
+
.post("/api/auth/oauth-handoff/link-start")
|
|
933
|
+
.set("origin", WEB_ORIGIN)
|
|
934
|
+
.set("sec-fetch-site", "cross-site")
|
|
935
|
+
.set("sec-fetch-mode", "navigate")
|
|
936
|
+
.set("sec-fetch-dest", "document")
|
|
937
|
+
.type("form")
|
|
938
|
+
.send({token});
|
|
939
|
+
|
|
940
|
+
const mintToken = async (
|
|
941
|
+
app: express.Application,
|
|
942
|
+
bearer: string,
|
|
943
|
+
body: Record<string, string> = {callbackURL: `${WEB_ORIGIN}/after`, provider: PROVIDER_ID}
|
|
944
|
+
): Promise<string> => {
|
|
945
|
+
const intent = await linkIntent(app, bearer, body);
|
|
946
|
+
expect(intent.status).toBe(200);
|
|
947
|
+
return intent.body.token as string;
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
const setCookies = (res: supertest.Response): string[] =>
|
|
951
|
+
(res.headers["set-cookie"] as unknown as string[] | undefined) ?? [];
|
|
952
|
+
|
|
953
|
+
it("links the provider for a signed-in bearer user, with no new session and no ott", async () => {
|
|
954
|
+
const {app} = buildApp();
|
|
955
|
+
const user = await signUpUser(app);
|
|
956
|
+
const sessionsBefore = await betterAuthRows("session").countDocuments({});
|
|
957
|
+
|
|
958
|
+
const intent = await linkIntent(app, user.bearer, {
|
|
959
|
+
callbackURL: `${WEB_ORIGIN}/after?tab=security`,
|
|
960
|
+
errorCallbackURL: `${WEB_ORIGIN}/settings/security`,
|
|
961
|
+
provider: PROVIDER_ID,
|
|
962
|
+
});
|
|
963
|
+
expect(intent.status).toBe(200);
|
|
964
|
+
expect(intent.body.startURL).toBe(START_URL);
|
|
965
|
+
expect(intent.headers["cache-control"]).toBe("no-store");
|
|
966
|
+
|
|
967
|
+
const started = await linkStart(app, intent.body.token);
|
|
968
|
+
expect(started.status).toBe(302);
|
|
969
|
+
expect(started.headers.location).toStartWith(`${provider.issuer}/authorize?`);
|
|
970
|
+
const startCookies = setCookies(started);
|
|
971
|
+
expect(startCookies.some((cookie) => cookie.includes(".state="))).toBe(true);
|
|
972
|
+
// Reading the session must not sign the browser in with a cookie on the API's origin.
|
|
973
|
+
expect(startCookies.some((cookie) => cookie.includes("session_token="))).toBe(false);
|
|
974
|
+
expect(startCookies.some((cookie) => cookie.includes("session_data="))).toBe(false);
|
|
975
|
+
expect(started.headers["set-auth-token"]).toBeUndefined();
|
|
976
|
+
|
|
977
|
+
const approval = await fetch(started.headers.location as string, {redirect: "manual"});
|
|
978
|
+
const callback = new URL(approval.headers.get("location") ?? "");
|
|
979
|
+
expect(callback.origin).toBe(API_ORIGIN);
|
|
980
|
+
const returned = await supertest(app)
|
|
981
|
+
.get(`${callback.pathname}${callback.search}`)
|
|
982
|
+
.set("cookie", cookieHeaderFrom(started));
|
|
983
|
+
|
|
984
|
+
expect(returned.status).toBe(302);
|
|
985
|
+
// Linking creates no session, so the return carries no one-time token.
|
|
986
|
+
expect(returned.headers.location).toBe(`${WEB_ORIGIN}/after?tab=security`);
|
|
987
|
+
expect(await verificationRows().countDocuments(HANDOFF_TOKEN_ROWS)).toBe(0);
|
|
988
|
+
const accounts = await betterAuthRows("account")
|
|
989
|
+
.find({userId: new mongoose.Types.ObjectId(user.userId)})
|
|
990
|
+
.toArray();
|
|
991
|
+
expect(accounts.map((account) => account.providerId).sort()).toEqual([
|
|
992
|
+
"credential",
|
|
993
|
+
PROVIDER_ID,
|
|
994
|
+
]);
|
|
995
|
+
expect(await betterAuthRows("session").countDocuments({})).toBe(sessionsBefore);
|
|
996
|
+
expect(setCookies(returned).some((cookie) => cookie.includes("session_token="))).toBe(false);
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
it("accepts a cookie-authenticated intent from a listed web origin", async () => {
|
|
1000
|
+
const {app} = buildApp();
|
|
1001
|
+
const user = await signUpUser(app);
|
|
1002
|
+
|
|
1003
|
+
const intent = await supertest(app)
|
|
1004
|
+
.post("/api/auth/oauth-handoff/link-intent")
|
|
1005
|
+
.set("origin", WEB_ORIGIN)
|
|
1006
|
+
.set("cookie", user.cookie)
|
|
1007
|
+
.set("content-type", "application/json")
|
|
1008
|
+
.send({callbackURL: `${WEB_ORIGIN}/after`, provider: PROVIDER_ID});
|
|
1009
|
+
|
|
1010
|
+
expect(intent.status).toBe(200);
|
|
1011
|
+
expect(intent.body.token).toBeTruthy();
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
it("stores the link token hashed under its own identifier, expiring in three minutes", async () => {
|
|
1015
|
+
const {app} = buildApp();
|
|
1016
|
+
const user = await signUpUser(app);
|
|
1017
|
+
|
|
1018
|
+
const token = await mintToken(app, user.bearer);
|
|
1019
|
+
|
|
1020
|
+
const rows = await verificationRows().find(LINK_TOKEN_ROWS).toArray();
|
|
1021
|
+
expect(rows).toHaveLength(1);
|
|
1022
|
+
expect(rows[0].identifier).not.toContain(token);
|
|
1023
|
+
expect(rows[0].value).not.toContain(token);
|
|
1024
|
+
const minutes = DateTime.fromJSDate(rows[0].expiresAt).diffNow("minutes").minutes;
|
|
1025
|
+
expect(minutes).toBeGreaterThan(2.5);
|
|
1026
|
+
expect(minutes).toBeLessThanOrEqual(3);
|
|
1027
|
+
// A link token is not a sign-in token.
|
|
1028
|
+
expect((await verify(app, token)).status).toBe(400);
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
it("refuses a reused token with 400 and no redirect", async () => {
|
|
1032
|
+
const {app} = buildApp();
|
|
1033
|
+
const user = await signUpUser(app);
|
|
1034
|
+
const token = await mintToken(app, user.bearer);
|
|
1035
|
+
expect((await linkStart(app, token)).status).toBe(302);
|
|
1036
|
+
|
|
1037
|
+
const reused = await linkStart(app, token);
|
|
1038
|
+
|
|
1039
|
+
expect(reused.status).toBe(400);
|
|
1040
|
+
expect(reused.headers.location).toBeUndefined();
|
|
1041
|
+
expect(setCookies(reused)).toEqual([]);
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
it("refuses an expired token with 400 and no redirect", async () => {
|
|
1045
|
+
const {app} = buildApp();
|
|
1046
|
+
const user = await signUpUser(app);
|
|
1047
|
+
const token = await mintToken(app, user.bearer);
|
|
1048
|
+
await verificationRows().updateMany(LINK_TOKEN_ROWS, {
|
|
1049
|
+
$set: {expiresAt: DateTime.now().minus({seconds: 1}).toJSDate()},
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
const expired = await linkStart(app, token);
|
|
1053
|
+
|
|
1054
|
+
expect(expired.status).toBe(400);
|
|
1055
|
+
expect(expired.headers.location).toBeUndefined();
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
it("refuses an unknown or missing token with 400", async () => {
|
|
1059
|
+
const {app} = buildApp();
|
|
1060
|
+
|
|
1061
|
+
const unknown = await linkStart(app, "not-a-real-token");
|
|
1062
|
+
const missing = await supertest(app)
|
|
1063
|
+
.post("/api/auth/oauth-handoff/link-start")
|
|
1064
|
+
.set("origin", WEB_ORIGIN)
|
|
1065
|
+
.type("form")
|
|
1066
|
+
.send({});
|
|
1067
|
+
|
|
1068
|
+
expect(unknown.status).toBe(400);
|
|
1069
|
+
expect(unknown.headers.location).toBeUndefined();
|
|
1070
|
+
expect(missing.status).toBe(400);
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
it("lets only one of two concurrent starts use the token", async () => {
|
|
1074
|
+
const {app} = buildApp();
|
|
1075
|
+
const user = await signUpUser(app);
|
|
1076
|
+
const token = await mintToken(app, user.bearer);
|
|
1077
|
+
|
|
1078
|
+
const results = await Promise.all([linkStart(app, token), linkStart(app, token)]);
|
|
1079
|
+
|
|
1080
|
+
expect(results.map((res) => res.status).sort()).toEqual([302, 400]);
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
// Better Auth's own origin check refuses an untrusted URL in a POST body first, with 403; the
|
|
1084
|
+
// handoff refuses every other URL that is not absolute on a listed web origin with 400.
|
|
1085
|
+
const refusedURLs: {body: Record<string, string>; name: string; status: number}[] = [
|
|
1086
|
+
{
|
|
1087
|
+
body: {callbackURL: "https://other.example.com/after"},
|
|
1088
|
+
name: "a trusted but unlisted origin",
|
|
1089
|
+
status: 400,
|
|
1090
|
+
},
|
|
1091
|
+
{body: {callbackURL: "/after"}, name: "a relative path", status: 400},
|
|
1092
|
+
{body: {callbackURL: `${API_ORIGIN}/after`}, name: "the API's own origin", status: 400},
|
|
1093
|
+
{body: {callbackURL: "exp://127.0.0.1:8081/--/home"}, name: "an app scheme", status: 400},
|
|
1094
|
+
{
|
|
1095
|
+
body: {callbackURL: `${WEB_ORIGIN}/after`, errorCallbackURL: "https://other.example.com/e"},
|
|
1096
|
+
name: "a trusted but unlisted errorCallbackURL",
|
|
1097
|
+
status: 400,
|
|
1098
|
+
},
|
|
1099
|
+
{body: {callbackURL: "https://evil.example/after"}, name: "an untrusted origin", status: 403},
|
|
1100
|
+
{
|
|
1101
|
+
body: {callbackURL: `${WEB_ORIGIN}/after`, errorCallbackURL: "https://evil.example/error"},
|
|
1102
|
+
name: "an untrusted errorCallbackURL",
|
|
1103
|
+
status: 403,
|
|
1104
|
+
},
|
|
1105
|
+
];
|
|
1106
|
+
|
|
1107
|
+
for (const {body, name, status} of refusedURLs) {
|
|
1108
|
+
it(`refuses an intent for ${name} with ${status} and mints nothing`, async () => {
|
|
1109
|
+
const {app} = buildApp();
|
|
1110
|
+
const user = await signUpUser(app);
|
|
1111
|
+
|
|
1112
|
+
const intent = await linkIntent(app, user.bearer, {provider: PROVIDER_ID, ...body});
|
|
1113
|
+
|
|
1114
|
+
expect(intent.status).toBe(status);
|
|
1115
|
+
expect(intent.body.token).toBeUndefined();
|
|
1116
|
+
expect(await verificationRows().countDocuments(LINK_TOKEN_ROWS)).toBe(0);
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
it("accepts any absolute path on a web origin as the link return, outside callbackPaths", async () => {
|
|
1121
|
+
const {app} = buildApp();
|
|
1122
|
+
const user = await signUpUser(app);
|
|
1123
|
+
const token = await mintToken(app, user.bearer, {
|
|
1124
|
+
callbackURL: `${WEB_ORIGIN}/settings/security`,
|
|
1125
|
+
errorCallbackURL: `${WEB_ORIGIN}/settings/security?failed=1`,
|
|
1126
|
+
provider: PROVIDER_ID,
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
const started = await linkStart(app, token);
|
|
1130
|
+
const approval = await fetch(started.headers.location as string, {redirect: "manual"});
|
|
1131
|
+
const callback = new URL(approval.headers.get("location") ?? "");
|
|
1132
|
+
const returned = await supertest(app)
|
|
1133
|
+
.get(`${callback.pathname}${callback.search}`)
|
|
1134
|
+
.set("cookie", cookieHeaderFrom(started));
|
|
1135
|
+
|
|
1136
|
+
expect(started.status).toBe(302);
|
|
1137
|
+
expect(returned.status).toBe(302);
|
|
1138
|
+
expect(returned.headers.location).toBe(`${WEB_ORIGIN}/settings/security`);
|
|
1139
|
+
expect(
|
|
1140
|
+
await betterAuthRows("account").countDocuments({
|
|
1141
|
+
providerId: PROVIDER_ID,
|
|
1142
|
+
userId: new mongoose.Types.ObjectId(user.userId),
|
|
1143
|
+
})
|
|
1144
|
+
).toBe(1);
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
it("links the token's user even when the browser sends another user's cookies", async () => {
|
|
1148
|
+
const {app} = buildApp();
|
|
1149
|
+
const other = await signUpUser(app);
|
|
1150
|
+
const user = await signUpUser(app);
|
|
1151
|
+
const token = await mintToken(app, user.bearer);
|
|
1152
|
+
|
|
1153
|
+
const started = await linkStart(app, token).set("cookie", other.cookie);
|
|
1154
|
+
const approval = await fetch(started.headers.location as string, {redirect: "manual"});
|
|
1155
|
+
const callback = new URL(approval.headers.get("location") ?? "");
|
|
1156
|
+
const returned = await supertest(app)
|
|
1157
|
+
.get(`${callback.pathname}${callback.search}`)
|
|
1158
|
+
.set("cookie", `${other.cookie}; ${cookieHeaderFrom(started)}`);
|
|
1159
|
+
|
|
1160
|
+
expect(started.status).toBe(302);
|
|
1161
|
+
const startCookies = setCookies(started);
|
|
1162
|
+
expect(startCookies.some((cookie) => cookie.includes(".state="))).toBe(true);
|
|
1163
|
+
for (const name of ["session_token=", "session_data=", "dont_remember="]) {
|
|
1164
|
+
expect({name, set: startCookies.some((cookie) => cookie.includes(name))}).toEqual({
|
|
1165
|
+
name,
|
|
1166
|
+
set: false,
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
expect(returned.headers.location).toBe(`${WEB_ORIGIN}/after`);
|
|
1170
|
+
const linkedTo = await betterAuthRows("account")
|
|
1171
|
+
.find({providerId: PROVIDER_ID})
|
|
1172
|
+
.map((account) => String(account.userId))
|
|
1173
|
+
.toArray();
|
|
1174
|
+
expect(linkedTo).toEqual([user.userId]);
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
it("limits link intents to three per ten seconds when Better Auth's limiter is on", async () => {
|
|
1178
|
+
const {app} = buildApp({
|
|
1179
|
+
options: {advanced: {disableOriginCheck: false}, rateLimit: {enabled: true}},
|
|
1180
|
+
});
|
|
1181
|
+
const user = await signUpUser(app);
|
|
1182
|
+
|
|
1183
|
+
const statuses: number[] = [];
|
|
1184
|
+
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
1185
|
+
statuses.push(
|
|
1186
|
+
(
|
|
1187
|
+
await linkIntent(app, user.bearer, {
|
|
1188
|
+
callbackURL: `${WEB_ORIGIN}/after`,
|
|
1189
|
+
provider: PROVIDER_ID,
|
|
1190
|
+
}).set("x-forwarded-for", "203.0.113.11")
|
|
1191
|
+
).status
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
expect(statuses).toEqual([200, 200, 200, 429]);
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
it("refuses an intent without a session with 401", async () => {
|
|
1199
|
+
const {app} = buildApp();
|
|
1200
|
+
|
|
1201
|
+
const intent = await supertest(app)
|
|
1202
|
+
.post("/api/auth/oauth-handoff/link-intent")
|
|
1203
|
+
.set("origin", WEB_ORIGIN)
|
|
1204
|
+
.set("content-type", "application/json")
|
|
1205
|
+
.send({callbackURL: `${WEB_ORIGIN}/after`, provider: PROVIDER_ID});
|
|
1206
|
+
|
|
1207
|
+
expect(intent.status).toBe(401);
|
|
1208
|
+
expect(await verificationRows().countDocuments(LINK_TOKEN_ROWS)).toBe(0);
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
it("refuses the intent with 403 when accountLinking.mode is never", async () => {
|
|
1212
|
+
const {app} = buildApp({accountLinking: {mode: "never"}});
|
|
1213
|
+
const user = await signUpUser(app);
|
|
1214
|
+
|
|
1215
|
+
const intent = await linkIntent(app, user.bearer, {
|
|
1216
|
+
callbackURL: `${WEB_ORIGIN}/after`,
|
|
1217
|
+
provider: PROVIDER_ID,
|
|
1218
|
+
});
|
|
1219
|
+
|
|
1220
|
+
expect(intent.status).toBe(403);
|
|
1221
|
+
expect(intent.body.code).toBe("LINKING_DISABLED");
|
|
1222
|
+
expect(await verificationRows().countDocuments(LINK_TOKEN_ROWS)).toBe(0);
|
|
1223
|
+
});
|
|
1224
|
+
|
|
1225
|
+
it("sends a session revoked after the intent back with error=session_expired", async () => {
|
|
1226
|
+
const {app} = buildApp();
|
|
1227
|
+
const user = await signUpUser(app);
|
|
1228
|
+
const token = await mintToken(app, user.bearer, {
|
|
1229
|
+
callbackURL: `${WEB_ORIGIN}/after`,
|
|
1230
|
+
errorCallbackURL: `${WEB_ORIGIN}/settings/security`,
|
|
1231
|
+
provider: PROVIDER_ID,
|
|
1232
|
+
});
|
|
1233
|
+
const signOut = await supertest(app)
|
|
1234
|
+
.post("/api/auth/sign-out")
|
|
1235
|
+
.set("origin", WEB_ORIGIN)
|
|
1236
|
+
.set("authorization", `Bearer ${user.bearer}`)
|
|
1237
|
+
.set("content-type", "application/json")
|
|
1238
|
+
.send({});
|
|
1239
|
+
expect(signOut.status).toBe(200);
|
|
1240
|
+
|
|
1241
|
+
const started = await linkStart(app, token);
|
|
1242
|
+
|
|
1243
|
+
expect(started.status).toBe(302);
|
|
1244
|
+
expect(started.headers.location).toBe(`${WEB_ORIGIN}/settings/security?error=session_expired`);
|
|
1245
|
+
expect(setCookies(started).some((cookie) => cookie.includes(".state="))).toBe(false);
|
|
1246
|
+
expect(await verificationRows().countDocuments(LINK_TOKEN_ROWS)).toBe(0);
|
|
1247
|
+
expect(
|
|
1248
|
+
await betterAuthRows("account").countDocuments({
|
|
1249
|
+
providerId: PROVIDER_ID,
|
|
1250
|
+
userId: new mongoose.Types.ObjectId(user.userId),
|
|
1251
|
+
})
|
|
1252
|
+
).toBe(0);
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
it("sends an unknown provider back to callbackURL with error=provider_not_found", async () => {
|
|
1256
|
+
const {app} = buildApp();
|
|
1257
|
+
const user = await signUpUser(app);
|
|
1258
|
+
const token = await mintToken(app, user.bearer, {
|
|
1259
|
+
callbackURL: `${WEB_ORIGIN}/after?tab=security`,
|
|
1260
|
+
provider: "nope",
|
|
1261
|
+
});
|
|
1262
|
+
|
|
1263
|
+
const started = await linkStart(app, token);
|
|
1264
|
+
|
|
1265
|
+
expect(started.status).toBe(302);
|
|
1266
|
+
expect(started.headers.location).toBe(
|
|
1267
|
+
`${WEB_ORIGIN}/after?tab=security&error=provider_not_found`
|
|
1268
|
+
);
|
|
1269
|
+
});
|
|
1270
|
+
|
|
1271
|
+
it("refuses a start that is not a page navigation and keeps the token", async () => {
|
|
1272
|
+
const {app} = buildApp();
|
|
1273
|
+
const user = await signUpUser(app);
|
|
1274
|
+
const token = await mintToken(app, user.bearer);
|
|
1275
|
+
|
|
1276
|
+
const refused = await linkStart(app, token).set("sec-fetch-dest", "iframe");
|
|
1277
|
+
const retried = await linkStart(app, token);
|
|
1278
|
+
|
|
1279
|
+
expect(refused.status).toBe(400);
|
|
1280
|
+
expect(refused.headers.location).toBeUndefined();
|
|
1281
|
+
expect(retried.status).toBe(302);
|
|
1282
|
+
});
|
|
1283
|
+
|
|
1284
|
+
it("refuses a start posted from another origin and keeps the token", async () => {
|
|
1285
|
+
const {app} = buildApp();
|
|
1286
|
+
const user = await signUpUser(app);
|
|
1287
|
+
const token = await mintToken(app, user.bearer);
|
|
1288
|
+
|
|
1289
|
+
const fromOther = await linkStart(app, token).set("origin", "https://other.example.com");
|
|
1290
|
+
const fromNull = await linkStart(app, token).set("origin", "null");
|
|
1291
|
+
const retried = await linkStart(app, token);
|
|
1292
|
+
|
|
1293
|
+
expect(fromOther.status).toBe(403);
|
|
1294
|
+
expect(fromOther.headers.location).toBeUndefined();
|
|
1295
|
+
expect(fromNull.status).toBe(403);
|
|
1296
|
+
expect(retried.status).toBe(302);
|
|
1297
|
+
});
|
|
1298
|
+
|
|
1299
|
+
it("refuses a start without an Origin header and keeps the token", async () => {
|
|
1300
|
+
const {app} = buildApp();
|
|
1301
|
+
const user = await signUpUser(app);
|
|
1302
|
+
const token = await mintToken(app, user.bearer);
|
|
1303
|
+
|
|
1304
|
+
const withoutOrigin = await supertest(app)
|
|
1305
|
+
.post("/api/auth/oauth-handoff/link-start")
|
|
1306
|
+
.set("sec-fetch-dest", "document")
|
|
1307
|
+
.type("form")
|
|
1308
|
+
.send({token});
|
|
1309
|
+
const retried = await linkStart(app, token);
|
|
1310
|
+
|
|
1311
|
+
expect(withoutOrigin.status).toBe(403);
|
|
1312
|
+
expect(withoutOrigin.headers.location).toBeUndefined();
|
|
1313
|
+
expect(retried.status).toBe(302);
|
|
1314
|
+
});
|
|
1315
|
+
|
|
1316
|
+
it("refuses a JSON start, so the token only travels in a form body", async () => {
|
|
1317
|
+
const {app} = buildApp();
|
|
1318
|
+
const user = await signUpUser(app);
|
|
1319
|
+
const token = await mintToken(app, user.bearer);
|
|
1320
|
+
|
|
1321
|
+
const json = await supertest(app)
|
|
1322
|
+
.post("/api/auth/oauth-handoff/link-start")
|
|
1323
|
+
.set("origin", WEB_ORIGIN)
|
|
1324
|
+
.set("content-type", "application/json")
|
|
1325
|
+
.send({token});
|
|
1326
|
+
|
|
1327
|
+
expect(json.status).toBe(415);
|
|
1328
|
+
expect(await verificationRows().countDocuments(LINK_TOKEN_ROWS)).toBe(1);
|
|
1329
|
+
});
|
|
1330
|
+
|
|
1331
|
+
it("limits link starts to three per ten seconds when Better Auth's limiter is on", async () => {
|
|
1332
|
+
const {app} = buildApp({
|
|
1333
|
+
options: {advanced: {disableOriginCheck: false}, rateLimit: {enabled: true}},
|
|
1334
|
+
});
|
|
1335
|
+
|
|
1336
|
+
const statuses: number[] = [];
|
|
1337
|
+
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
1338
|
+
statuses.push(
|
|
1339
|
+
(await linkStart(app, `token-${attempt}`).set("x-forwarded-for", "203.0.113.10")).status
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
expect(statuses).toEqual([400, 400, 400, 429]);
|
|
1344
|
+
});
|
|
1345
|
+
|
|
1346
|
+
it("has no link routes when oauthHandoff is unset", async () => {
|
|
1347
|
+
const {app} = buildApp({oauthHandoff: undefined});
|
|
1348
|
+
const user = await signUpUser(app);
|
|
1349
|
+
|
|
1350
|
+
const intent = await linkIntent(app, user.bearer, {
|
|
1351
|
+
callbackURL: `${WEB_ORIGIN}/after`,
|
|
1352
|
+
provider: PROVIDER_ID,
|
|
1353
|
+
});
|
|
1354
|
+
const started = await linkStart(app, "anything");
|
|
1355
|
+
|
|
1356
|
+
expect(intent.status).toBe(404);
|
|
1357
|
+
expect(started.status).toBe(404);
|
|
1358
|
+
});
|
|
1359
|
+
});
|