@terreno/api 57.19.2 → 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 +393 -0
- 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
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {genericOAuth} from "better-auth/plugins";
|
|
3
|
+
import express from "express";
|
|
4
|
+
import supertest from "supertest";
|
|
5
|
+
import type {BetterAuthConfig} from "./betterAuth";
|
|
6
|
+
import {
|
|
7
|
+
createBetterAuth,
|
|
8
|
+
getMongoClientFromMongoose,
|
|
9
|
+
mountBetterAuthRoutes,
|
|
10
|
+
} from "./betterAuthSetup";
|
|
11
|
+
|
|
12
|
+
const SECRET = "test-secret-at-least-32-characters-long";
|
|
13
|
+
const OAUTH_APP = {clientId: "client-id", clientSecret: "client-secret"};
|
|
14
|
+
|
|
15
|
+
const buildApp = (overrides: Partial<BetterAuthConfig> = {}): express.Application => {
|
|
16
|
+
const config: BetterAuthConfig = {
|
|
17
|
+
baseURL: "http://localhost:3000",
|
|
18
|
+
enabled: true,
|
|
19
|
+
secret: SECRET,
|
|
20
|
+
...overrides,
|
|
21
|
+
};
|
|
22
|
+
const auth = createBetterAuth({config, mongoClient: getMongoClientFromMongoose()});
|
|
23
|
+
const app = express();
|
|
24
|
+
mountBetterAuthRoutes(app, auth, config.basePath);
|
|
25
|
+
return app;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Registered with explicit endpoints, so init needs no discovery fetch.
|
|
29
|
+
const mockOidcPlugin = genericOAuth({
|
|
30
|
+
config: [
|
|
31
|
+
{
|
|
32
|
+
authorizationUrl: "https://oidc.example.com/authorize",
|
|
33
|
+
clientId: "mock-client",
|
|
34
|
+
clientSecret: "mock-secret",
|
|
35
|
+
providerId: "mock-oidc",
|
|
36
|
+
tokenUrl: "https://oidc.example.com/token",
|
|
37
|
+
userInfoUrl: "https://oidc.example.com/userinfo",
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("GET {basePath}/terreno/methods", () => {
|
|
43
|
+
it("lists only configured providers", async () => {
|
|
44
|
+
const res = await supertest(buildApp({githubOAuth: OAUTH_APP})).get(
|
|
45
|
+
"/api/auth/terreno/methods"
|
|
46
|
+
);
|
|
47
|
+
expect(res.status).toBe(200);
|
|
48
|
+
expect(res.body.providers).toEqual(["github"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("answers without a session and reports the defaults", async () => {
|
|
52
|
+
const res = await supertest(buildApp()).get("/api/auth/terreno/methods");
|
|
53
|
+
expect(res.status).toBe(200);
|
|
54
|
+
expect(res.body).toEqual({
|
|
55
|
+
accountLinking: {mode: "verified-email"},
|
|
56
|
+
emailPassword: true,
|
|
57
|
+
multiSession: false,
|
|
58
|
+
passkey: false,
|
|
59
|
+
providers: [],
|
|
60
|
+
signUp: true,
|
|
61
|
+
twoFactor: false,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("lists social providers in a stable order", async () => {
|
|
66
|
+
const res = await supertest(
|
|
67
|
+
buildApp({appleOAuth: OAUTH_APP, githubOAuth: OAUTH_APP, googleOAuth: OAUTH_APP})
|
|
68
|
+
).get("/api/auth/terreno/methods");
|
|
69
|
+
expect(res.body.providers).toEqual(["apple", "github", "google"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("lists generic OAuth provider ids from config.plugins", async () => {
|
|
73
|
+
const res = await supertest(buildApp({googleOAuth: OAUTH_APP, plugins: [mockOidcPlugin]})).get(
|
|
74
|
+
"/api/auth/terreno/methods"
|
|
75
|
+
);
|
|
76
|
+
expect(res.body.providers).toEqual(["google", "mock-oidc"]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("omits a generic OAuth provider whose discovery failed", async () => {
|
|
80
|
+
const unreachable = genericOAuth({
|
|
81
|
+
config: [
|
|
82
|
+
{
|
|
83
|
+
clientId: "broken-client",
|
|
84
|
+
clientSecret: "broken-secret",
|
|
85
|
+
discoveryUrl: "http://127.0.0.1:1/.well-known/openid-configuration",
|
|
86
|
+
providerId: "broken",
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
const res = await supertest(buildApp({plugins: [unreachable]})).get(
|
|
91
|
+
"/api/auth/terreno/methods"
|
|
92
|
+
);
|
|
93
|
+
expect(res.body.providers).toEqual([]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("reports sign-up closed when email sign-up is disabled", async () => {
|
|
97
|
+
const res = await supertest(buildApp({options: {emailAndPassword: {disableSignUp: true}}})).get(
|
|
98
|
+
"/api/auth/terreno/methods"
|
|
99
|
+
);
|
|
100
|
+
expect(res.body.emailPassword).toBe(true);
|
|
101
|
+
expect(res.body.signUp).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("reports email and password off when it is disabled", async () => {
|
|
105
|
+
const res = await supertest(buildApp({options: {emailAndPassword: {enabled: false}}})).get(
|
|
106
|
+
"/api/auth/terreno/methods"
|
|
107
|
+
);
|
|
108
|
+
expect(res.body.emailPassword).toBe(false);
|
|
109
|
+
expect(res.body.signUp).toBe(false);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("reports account linking from Better Auth's account options", async () => {
|
|
113
|
+
const never = await supertest(
|
|
114
|
+
buildApp({options: {account: {accountLinking: {enabled: false}}}})
|
|
115
|
+
).get("/api/auth/terreno/methods");
|
|
116
|
+
expect(never.body.accountLinking).toEqual({mode: "never"});
|
|
117
|
+
|
|
118
|
+
const explicit = await supertest(
|
|
119
|
+
buildApp({options: {account: {accountLinking: {disableImplicitLinking: true}}}})
|
|
120
|
+
).get("/api/auth/terreno/methods");
|
|
121
|
+
expect(explicit.body.accountLinking).toEqual({mode: "explicit"});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("mounts under a custom basePath", async () => {
|
|
125
|
+
const app = buildApp({basePath: "/auth/v2", githubOAuth: OAUTH_APP});
|
|
126
|
+
const res = await supertest(app).get("/auth/v2/terreno/methods");
|
|
127
|
+
expect(res.status).toBe(200);
|
|
128
|
+
expect(res.body.providers).toEqual(["github"]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("refuses methods other than GET", async () => {
|
|
132
|
+
const res = await supertest(buildApp())
|
|
133
|
+
.post("/api/auth/terreno/methods")
|
|
134
|
+
.set("content-type", "application/json")
|
|
135
|
+
.send({});
|
|
136
|
+
expect(res.status).toBe(404);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in methods discovery for Better Auth.
|
|
3
|
+
*
|
|
4
|
+
* Login screens call `GET {basePath}/terreno/methods` before anyone signs in, so they render
|
|
5
|
+
* only the methods this server has configured. The answer is read from Better Auth's final
|
|
6
|
+
* context on each request, which already includes `config.options` overrides and the providers
|
|
7
|
+
* that plugins such as `genericOAuth` register.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {BetterAuthPlugin} from "better-auth";
|
|
11
|
+
import {createAuthEndpoint} from "better-auth/api";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* How a sign-in with a new method relates to an existing account with the same email.
|
|
15
|
+
* - `verified-email`: Better Auth links it when the provider reports a verified email (the
|
|
16
|
+
* Better Auth default).
|
|
17
|
+
* - `explicit`: no implicit linking at sign-in; a signed-in user links the method themselves.
|
|
18
|
+
* - `never`: account linking is off.
|
|
19
|
+
*/
|
|
20
|
+
export type BetterAuthAccountLinkingMode = "verified-email" | "explicit" | "never";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The body of `GET {basePath}/terreno/methods`.
|
|
24
|
+
*/
|
|
25
|
+
export interface BetterAuthMethods {
|
|
26
|
+
/** Linking behavior for same-email sign-ins with another method. */
|
|
27
|
+
accountLinking: {mode: BetterAuthAccountLinkingMode};
|
|
28
|
+
/** Email and password sign-in is enabled. */
|
|
29
|
+
emailPassword: boolean;
|
|
30
|
+
/** Several signed-in accounts per device (Better Auth's `multiSession` plugin is on). */
|
|
31
|
+
multiSession: boolean;
|
|
32
|
+
/** Passkey sign-in (the `passkey` plugin is on). */
|
|
33
|
+
passkey: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Ids of every OAuth provider that can start a sign-in, sorted: social providers (`google`,
|
|
36
|
+
* `github`, `apple`) and generic OAuth providers (such as `mock-oidc`). A generic provider
|
|
37
|
+
* whose discovery failed at startup is left out.
|
|
38
|
+
*/
|
|
39
|
+
providers: string[];
|
|
40
|
+
/** New users can sign up with email and password (`false` under `signUp: {mode: "closed"}`). */
|
|
41
|
+
signUp: boolean;
|
|
42
|
+
/** Two-factor authentication (Better Auth's `twoFactor` plugin is on). */
|
|
43
|
+
twoFactor: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The parts of Better Auth's endpoint context the methods endpoint reads. */
|
|
47
|
+
interface MethodsContext {
|
|
48
|
+
options: {
|
|
49
|
+
account?: {accountLinking?: {disableImplicitLinking?: boolean; enabled?: boolean}};
|
|
50
|
+
emailAndPassword?: {disableSignUp?: boolean; enabled?: boolean};
|
|
51
|
+
plugins?: {id: string}[];
|
|
52
|
+
};
|
|
53
|
+
socialProviders: {id: string}[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const accountLinkingMode = (options: MethodsContext["options"]): BetterAuthAccountLinkingMode => {
|
|
57
|
+
const accountLinking = options.account?.accountLinking;
|
|
58
|
+
if (accountLinking?.enabled === false) {
|
|
59
|
+
return "never";
|
|
60
|
+
}
|
|
61
|
+
if (accountLinking?.disableImplicitLinking === true) {
|
|
62
|
+
return "explicit";
|
|
63
|
+
}
|
|
64
|
+
return "verified-email";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reads the enabled sign-in methods from a Better Auth context.
|
|
69
|
+
*/
|
|
70
|
+
export const readBetterAuthMethods = (context: MethodsContext): BetterAuthMethods => {
|
|
71
|
+
const {options, socialProviders} = context;
|
|
72
|
+
const emailPassword = options.emailAndPassword?.enabled === true;
|
|
73
|
+
// Read from the final plugin list, so a plugin added through `config.plugins` counts too.
|
|
74
|
+
const pluginIds = new Set((options.plugins ?? []).map((plugin) => plugin.id));
|
|
75
|
+
return {
|
|
76
|
+
accountLinking: {mode: accountLinkingMode(options)},
|
|
77
|
+
emailPassword,
|
|
78
|
+
multiSession: pluginIds.has("multi-session"),
|
|
79
|
+
passkey: pluginIds.has("passkey"),
|
|
80
|
+
providers: [...new Set(socialProviders.map((provider) => provider.id))].sort(),
|
|
81
|
+
signUp: emailPassword && options.emailAndPassword?.disableSignUp !== true,
|
|
82
|
+
twoFactor: pluginIds.has("two-factor"),
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The Better Auth plugin `createBetterAuth` always adds for `GET {basePath}/terreno/methods`.
|
|
88
|
+
* The endpoint is public: it needs no session and reveals only what the login screen shows.
|
|
89
|
+
*/
|
|
90
|
+
export const createMethodsPlugin = (): BetterAuthPlugin => {
|
|
91
|
+
const terrenoMethods = createAuthEndpoint("/terreno/methods", {method: "GET"}, async (ctx) => {
|
|
92
|
+
return ctx.json(readBetterAuthMethods(ctx.context as unknown as MethodsContext));
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
endpoints: {terrenoMethods},
|
|
96
|
+
id: "terreno-methods",
|
|
97
|
+
} satisfies BetterAuthPlugin;
|
|
98
|
+
};
|