@better-auth/core 1.7.0-beta.4 → 1.7.0-beta.5

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.
Files changed (150) hide show
  1. package/dist/api/index.d.mts +3 -3
  2. package/dist/context/global.mjs +1 -1
  3. package/dist/db/adapter/factory.mjs +2 -2
  4. package/dist/db/get-tables.mjs +3 -3
  5. package/dist/db/schema/account.d.mts +1 -1
  6. package/dist/db/schema/account.mjs +1 -1
  7. package/dist/env/env-impl.mjs +1 -1
  8. package/dist/error/codes.d.mts +5 -0
  9. package/dist/error/codes.mjs +5 -0
  10. package/dist/index.d.mts +2 -2
  11. package/dist/instrumentation/tracer.mjs +1 -1
  12. package/dist/oauth2/create-authorization-url.d.mts +4 -1
  13. package/dist/oauth2/create-authorization-url.mjs +5 -2
  14. package/dist/oauth2/index.d.mts +4 -2
  15. package/dist/oauth2/index.mjs +3 -1
  16. package/dist/oauth2/oauth-provider.d.mts +128 -9
  17. package/dist/oauth2/refresh-access-token.mjs +1 -1
  18. package/dist/oauth2/scopes.d.mts +76 -0
  19. package/dist/oauth2/scopes.mjs +96 -0
  20. package/dist/oauth2/utils.mjs +2 -1
  21. package/dist/oauth2/verify-id-token.d.mts +26 -0
  22. package/dist/oauth2/verify-id-token.mjs +62 -0
  23. package/dist/oauth2/verify.d.mts +14 -0
  24. package/dist/oauth2/verify.mjs +23 -7
  25. package/dist/social-providers/apple.d.mts +14 -2
  26. package/dist/social-providers/apple.mjs +12 -36
  27. package/dist/social-providers/atlassian.d.mts +5 -1
  28. package/dist/social-providers/atlassian.mjs +4 -4
  29. package/dist/social-providers/cognito.d.mts +13 -2
  30. package/dist/social-providers/cognito.mjs +24 -32
  31. package/dist/social-providers/discord.d.mts +5 -1
  32. package/dist/social-providers/discord.mjs +7 -6
  33. package/dist/social-providers/dropbox.d.mts +5 -1
  34. package/dist/social-providers/dropbox.mjs +5 -5
  35. package/dist/social-providers/facebook.d.mts +21 -2
  36. package/dist/social-providers/facebook.mjs +46 -22
  37. package/dist/social-providers/figma.d.mts +5 -1
  38. package/dist/social-providers/figma.mjs +5 -5
  39. package/dist/social-providers/github.d.mts +5 -1
  40. package/dist/social-providers/github.mjs +4 -4
  41. package/dist/social-providers/gitlab.d.mts +5 -1
  42. package/dist/social-providers/gitlab.mjs +6 -6
  43. package/dist/social-providers/google.d.mts +29 -3
  44. package/dist/social-providers/google.mjs +24 -30
  45. package/dist/social-providers/huggingface.d.mts +5 -1
  46. package/dist/social-providers/huggingface.mjs +8 -8
  47. package/dist/social-providers/index.d.mts +221 -42
  48. package/dist/social-providers/kakao.d.mts +5 -1
  49. package/dist/social-providers/kakao.mjs +8 -8
  50. package/dist/social-providers/kick.d.mts +5 -1
  51. package/dist/social-providers/kick.mjs +4 -4
  52. package/dist/social-providers/line.d.mts +8 -2
  53. package/dist/social-providers/line.mjs +12 -14
  54. package/dist/social-providers/linear.d.mts +5 -1
  55. package/dist/social-providers/linear.mjs +4 -4
  56. package/dist/social-providers/linkedin.d.mts +5 -1
  57. package/dist/social-providers/linkedin.mjs +10 -10
  58. package/dist/social-providers/microsoft-entra-id.d.mts +31 -6
  59. package/dist/social-providers/microsoft-entra-id.mjs +26 -37
  60. package/dist/social-providers/naver.d.mts +5 -1
  61. package/dist/social-providers/naver.mjs +4 -4
  62. package/dist/social-providers/notion.d.mts +5 -1
  63. package/dist/social-providers/notion.mjs +4 -4
  64. package/dist/social-providers/paybin.d.mts +5 -1
  65. package/dist/social-providers/paybin.mjs +10 -10
  66. package/dist/social-providers/paypal.d.mts +5 -2
  67. package/dist/social-providers/paypal.mjs +8 -13
  68. package/dist/social-providers/polar.d.mts +5 -1
  69. package/dist/social-providers/polar.mjs +8 -8
  70. package/dist/social-providers/railway.d.mts +5 -1
  71. package/dist/social-providers/railway.mjs +9 -9
  72. package/dist/social-providers/reddit.d.mts +5 -1
  73. package/dist/social-providers/reddit.mjs +9 -8
  74. package/dist/social-providers/roblox.d.mts +5 -1
  75. package/dist/social-providers/roblox.mjs +5 -5
  76. package/dist/social-providers/salesforce.d.mts +5 -1
  77. package/dist/social-providers/salesforce.mjs +8 -8
  78. package/dist/social-providers/slack.d.mts +5 -1
  79. package/dist/social-providers/slack.mjs +9 -9
  80. package/dist/social-providers/spotify.d.mts +5 -1
  81. package/dist/social-providers/spotify.mjs +5 -5
  82. package/dist/social-providers/tiktok.d.mts +5 -1
  83. package/dist/social-providers/tiktok.mjs +9 -5
  84. package/dist/social-providers/twitch.d.mts +5 -1
  85. package/dist/social-providers/twitch.mjs +4 -4
  86. package/dist/social-providers/twitter.d.mts +6 -4
  87. package/dist/social-providers/twitter.mjs +9 -9
  88. package/dist/social-providers/vercel.d.mts +5 -1
  89. package/dist/social-providers/vercel.mjs +4 -7
  90. package/dist/social-providers/vk.d.mts +5 -1
  91. package/dist/social-providers/vk.mjs +5 -5
  92. package/dist/social-providers/wechat.d.mts +5 -1
  93. package/dist/social-providers/wechat.mjs +9 -5
  94. package/dist/social-providers/zoom.d.mts +6 -1
  95. package/dist/social-providers/zoom.mjs +15 -9
  96. package/dist/types/context.d.mts +10 -8
  97. package/dist/types/index.d.mts +1 -1
  98. package/dist/types/init-options.d.mts +92 -1
  99. package/package.json +5 -5
  100. package/src/db/adapter/factory.ts +10 -2
  101. package/src/db/get-tables.ts +8 -3
  102. package/src/db/schema/account.ts +14 -2
  103. package/src/env/env-impl.ts +1 -2
  104. package/src/error/codes.ts +5 -0
  105. package/src/oauth2/create-authorization-url.ts +2 -2
  106. package/src/oauth2/index.ts +17 -1
  107. package/src/oauth2/oauth-provider.ts +140 -10
  108. package/src/oauth2/refresh-access-token.ts +2 -2
  109. package/src/oauth2/scopes.ts +118 -0
  110. package/src/oauth2/utils.ts +2 -5
  111. package/src/oauth2/verify-id-token.ts +111 -0
  112. package/src/oauth2/verify.ts +62 -11
  113. package/src/social-providers/apple.ts +24 -61
  114. package/src/social-providers/atlassian.ts +12 -8
  115. package/src/social-providers/cognito.ts +25 -47
  116. package/src/social-providers/discord.ts +19 -8
  117. package/src/social-providers/dropbox.ts +13 -7
  118. package/src/social-providers/facebook.ts +97 -51
  119. package/src/social-providers/figma.ts +13 -9
  120. package/src/social-providers/github.ts +12 -8
  121. package/src/social-providers/gitlab.ts +14 -8
  122. package/src/social-providers/google.ts +66 -47
  123. package/src/social-providers/huggingface.ts +12 -8
  124. package/src/social-providers/kakao.ts +16 -8
  125. package/src/social-providers/kick.ts +12 -7
  126. package/src/social-providers/line.ts +37 -37
  127. package/src/social-providers/linear.ts +12 -6
  128. package/src/social-providers/linkedin.ts +14 -10
  129. package/src/social-providers/microsoft-entra-id.ts +65 -64
  130. package/src/social-providers/naver.ts +12 -6
  131. package/src/social-providers/notion.ts +12 -6
  132. package/src/social-providers/paybin.ts +14 -11
  133. package/src/social-providers/paypal.ts +6 -25
  134. package/src/social-providers/polar.ts +12 -8
  135. package/src/social-providers/railway.ts +13 -9
  136. package/src/social-providers/reddit.ts +21 -10
  137. package/src/social-providers/roblox.ts +18 -7
  138. package/src/social-providers/salesforce.ts +12 -8
  139. package/src/social-providers/slack.ts +18 -9
  140. package/src/social-providers/spotify.ts +13 -7
  141. package/src/social-providers/tiktok.ts +13 -7
  142. package/src/social-providers/twitch.ts +12 -8
  143. package/src/social-providers/twitter.ts +17 -8
  144. package/src/social-providers/vercel.ts +16 -10
  145. package/src/social-providers/vk.ts +13 -7
  146. package/src/social-providers/wechat.ts +20 -8
  147. package/src/social-providers/zoom.ts +19 -6
  148. package/src/types/context.ts +8 -8
  149. package/src/types/index.ts +7 -0
  150. package/src/types/init-options.ts +119 -0
@@ -1,8 +1,9 @@
1
1
  import { betterFetch } from "@better-fetch/fetch";
2
- import type { OAuthProvider, ProviderOptions } from "../oauth2";
2
+ import type { ProviderOptions, UpstreamProvider } from "../oauth2";
3
3
  import {
4
4
  createAuthorizationURL,
5
5
  refreshAccessToken,
6
+ resolveRequestedScopes,
6
7
  validateAuthorizationCode,
7
8
  } from "../oauth2";
8
9
 
@@ -143,6 +144,8 @@ export interface ZoomOptions extends ProviderOptions<ZoomProfile> {
143
144
  pkce?: boolean | undefined;
144
145
  }
145
146
 
147
+ const ZOOM_DEFAULT_SCOPES: string[] = [];
148
+
146
149
  export const zoom = (userOptions: ZoomOptions) => {
147
150
  const options = {
148
151
  pkce: true,
@@ -152,21 +155,31 @@ export const zoom = (userOptions: ZoomOptions) => {
152
155
  return {
153
156
  id: "zoom",
154
157
  name: "Zoom",
155
- createAuthorizationURL: async ({
158
+ callbackPath: "/callback/zoom",
159
+ createAuthorizationURL: ({
156
160
  state,
161
+ scopes,
157
162
  redirectURI,
158
163
  codeVerifier,
159
164
  additionalParams,
160
- }) =>
161
- createAuthorizationURL({
165
+ }) => {
166
+ const requestedScopes = resolveRequestedScopes(
167
+ options,
168
+ ZOOM_DEFAULT_SCOPES,
169
+ scopes,
170
+ );
171
+
172
+ return createAuthorizationURL({
162
173
  id: "zoom",
163
174
  options,
164
175
  authorizationEndpoint: "https://zoom.us/oauth/authorize",
176
+ scopes: requestedScopes,
165
177
  state,
166
178
  redirectURI,
167
179
  codeVerifier: options.pkce ? codeVerifier : undefined,
168
180
  additionalParams,
169
- }),
181
+ });
182
+ },
170
183
  validateAuthorizationCode: async ({ code, redirectURI, codeVerifier }) => {
171
184
  return validateAuthorizationCode({
172
185
  code,
@@ -222,5 +235,5 @@ export const zoom = (userOptions: ZoomOptions) => {
222
235
  },
223
236
  };
224
237
  },
225
- } satisfies OAuthProvider<ZoomProfile>;
238
+ } satisfies UpstreamProvider<ZoomProfile>;
226
239
  };
@@ -10,12 +10,13 @@ import type {
10
10
  } from "../db";
11
11
  import type { DBAdapter, Where } from "../db/adapter";
12
12
  import type { createLogger } from "../env";
13
- import type { OAuthProvider } from "../oauth2";
13
+ import type { UpstreamProvider } from "../oauth2";
14
14
  import type { BetterAuthCookie, BetterAuthCookies } from "./cookie";
15
15
  import type { Awaitable, LiteralString } from "./helper";
16
16
  import type {
17
17
  BetterAuthOptions,
18
18
  BetterAuthRateLimitOptions,
19
+ UserProvisioningSource,
19
20
  } from "./init-options";
20
21
  import type { BetterAuthPlugin } from "./plugin";
21
22
  import type { SecretConfig } from "./secret";
@@ -87,16 +88,15 @@ export type GenericEndpointContext<
87
88
  export interface InternalAdapter<
88
89
  _Options extends BetterAuthOptions = BetterAuthOptions,
89
90
  > {
90
- createOAuthUser(
91
- user: Omit<User, "id" | "createdAt" | "updatedAt">,
92
- account: Omit<Account, "userId" | "id" | "createdAt" | "updatedAt"> &
93
- Partial<Account>,
94
- ): Promise<{ user: User; account: Account }>;
95
-
96
91
  createUser<T extends Record<string, any>>(
97
92
  user: Omit<User, "id" | "createdAt" | "updatedAt" | "emailVerified"> &
98
93
  Partial<User> &
99
94
  Record<string, any>,
95
+ /**
96
+ * Provisioning source. The creation seam adds `action: "create-user"` and
97
+ * runs the `user.validateUserInfo` gate.
98
+ */
99
+ source: UserProvisioningSource,
100
100
  ): Promise<T & User>;
101
101
 
102
102
  createAccount<T extends Record<string, any>>(
@@ -351,7 +351,7 @@ export type AuthContext<Options extends BetterAuthOptions = BetterAuthOptions> =
351
351
  user: User & Record<string, any>;
352
352
  } | null,
353
353
  ) => void;
354
- socialProviders: OAuthProvider[];
354
+ socialProviders: UpstreamProvider[];
355
355
  authCookies: BetterAuthCookies;
356
356
  logger: ReturnType<typeof createLogger>;
357
357
  rateLimit: {
@@ -24,6 +24,13 @@ export type {
24
24
  DynamicBaseURLConfig,
25
25
  GenerateIdFn,
26
26
  StoreIdentifierOption,
27
+ UserProvisioningSource,
28
+ ValidateUserInfoAction,
29
+ ValidateUserInfoMethod,
30
+ ValidateUserInfoOAuthInfo,
31
+ ValidateUserInfoResult,
32
+ ValidateUserInfoSource,
33
+ ValidateUserInfoSSOInfo,
27
34
  } from "./init-options";
28
35
  export type {
29
36
  BetterAuthPlugin,
@@ -47,6 +47,98 @@ export type GenerateIdFn = (options: {
47
47
  size?: number | undefined;
48
48
  }) => string | false;
49
49
 
50
+ /**
51
+ * What Better Auth is about to do with an incoming identity when
52
+ * {@link BetterAuthOptions.user}'s `validateUserInfo` runs.
53
+ *
54
+ * - `create-user`: a brand-new user record is about to be created.
55
+ * - `link-account`: a new provider account is about to be linked to an
56
+ * already-existing user.
57
+ * - `sign-in`: an existing OAuth or SSO user is signing in again. This is the
58
+ * one case where the provider can assert *changed* data, so the hook receives
59
+ * the fresh provider email and profile (not the stored row), letting a domain
60
+ * or org policy reject a user whose provider identity moved out of bounds.
61
+ *
62
+ * Non-provider returning sign-ins are not re-validated: they carry only the
63
+ * stored row, which has not changed since `create-user` gated it. Use the admin
64
+ * plugin's ban controls or a `databaseHooks.session.create.before` hook to
65
+ * block those.
66
+ */
67
+ export type ValidateUserInfoAction = "create-user" | "link-account" | "sign-in";
68
+
69
+ /**
70
+ * The authentication method that produced the incoming user info. The named
71
+ * methods cover Better Auth's built-ins; the open `string` keeps it extensible
72
+ * for plugins (for example `"scim"`).
73
+ */
74
+ export type ValidateUserInfoMethod =
75
+ | "oauth"
76
+ | "sso-oidc"
77
+ | "sso-saml"
78
+ | "email-password"
79
+ | "magic-link"
80
+ | "email-otp"
81
+ | "anonymous"
82
+ | "siwe"
83
+ | "phone-number"
84
+ | "admin"
85
+ | (string & {});
86
+
87
+ /** OAuth-specific provisioning context; present only when `method` is `"oauth"`. */
88
+ export type ValidateUserInfoOAuthInfo = {
89
+ /** The social or generic OAuth provider id (e.g. `"google"`). */
90
+ providerId: string;
91
+ /** The raw provider profile (userinfo or id-token claims), unmapped. */
92
+ profile?: Record<string, unknown> | undefined;
93
+ };
94
+
95
+ /** SSO-specific provisioning context; present for OIDC and SAML SSO methods. */
96
+ export type ValidateUserInfoSSOInfo = {
97
+ /** The configured SSO provider id. */
98
+ providerId: string;
99
+ /** The raw OIDC claims or SAML assertion attributes, unmapped. */
100
+ profile?: Record<string, unknown> | undefined;
101
+ };
102
+
103
+ /** Provisioning origin passed to `createUser`; the creation seam adds `action: "create-user"` to build {@link ValidateUserInfoSource}. */
104
+ export type UserProvisioningSource = {
105
+ method: ValidateUserInfoMethod;
106
+ /** Provider id and raw profile; present iff `method` is `"oauth"`. */
107
+ oauth?: ValidateUserInfoOAuthInfo | undefined;
108
+ /** Provider id and raw profile; present iff `method` is `"sso-oidc"` or `"sso-saml"`. */
109
+ sso?: ValidateUserInfoSSOInfo | undefined;
110
+ };
111
+
112
+ /**
113
+ * The context passed to `validateUserInfo`: the lifecycle
114
+ * {@link ValidateUserInfoAction}, the {@link ValidateUserInfoMethod}, and (for
115
+ * OAuth/SSO provider methods) protocol-specific provider metadata.
116
+ *
117
+ * ```ts
118
+ * // Scope to one OAuth provider:
119
+ * if (source.oauth?.providerId !== "google") return;
120
+ * // Branch on the method:
121
+ * if (source.method === "anonymous") return { error: "no_anonymous" };
122
+ * // Inspect SSO claims:
123
+ * if (source.method === "sso-saml" && source.sso?.profile?.department !== "eng") {
124
+ * return { error: "invalid_department" };
125
+ * }
126
+ * ```
127
+ */
128
+ export type ValidateUserInfoSource = UserProvisioningSource & {
129
+ action: ValidateUserInfoAction;
130
+ };
131
+
132
+ export type ValidateUserInfoResult = {
133
+ /** A short, machine-readable rejection code, surfaced to the client. */
134
+ error: string;
135
+ /**
136
+ * A human-readable reason, surfaced to the client. Do not put sensitive
137
+ * details here.
138
+ */
139
+ errorDescription?: string | undefined;
140
+ };
141
+
50
142
  /**
51
143
  * Configuration for dynamic base URL resolution.
52
144
  * Allows Better Auth to work with multiple domains (e.g., Vercel preview deployments).
@@ -777,6 +869,33 @@ export type BetterAuthOptions = {
777
869
  */
778
870
  user?:
779
871
  | (BetterAuthDBOptions<"user", keyof BaseUser> & {
872
+ /**
873
+ * Gate which identities Better Auth admits. Called just before
874
+ * `create-user`, `link-account`, and (for OAuth) `sign-in`, across
875
+ * every authentication method, including stateless setups with no
876
+ * persistent database. On `sign-in` the hook receives the *fresh*
877
+ * provider email and profile, so a domain policy can reject a user
878
+ * whose provider identity moved out of bounds.
879
+ *
880
+ * Non-provider returning sign-ins are not re-validated; use the admin
881
+ * plugin's ban controls or a `databaseHooks.session.create.before`
882
+ * hook for those.
883
+ *
884
+ * Return nothing to allow; return `{ error }` to reject. Browser flows
885
+ * redirect to the configured error URL; programmatic flows surface a
886
+ * `403`.
887
+ *
888
+ * TODO: rename to `validateUser` (and the `ValidateUserInfo*` types).
889
+ * "UserInfo" is the OIDC term and misleads for the email/password,
890
+ * SIWE, phone, and admin methods.
891
+ */
892
+ validateUserInfo?: (
893
+ data: {
894
+ user: Partial<User> & Record<string, unknown>;
895
+ source: ValidateUserInfoSource;
896
+ },
897
+ context: GenericEndpointContext,
898
+ ) => Awaitable<void | ValidateUserInfoResult>;
780
899
  /**
781
900
  * Changing email configuration
782
901
  */