@hammadj/better-auth-sso 1.5.0-beta.9

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 (42) hide show
  1. package/.turbo/turbo-build.log +116 -0
  2. package/LICENSE.md +20 -0
  3. package/dist/client.d.mts +10 -0
  4. package/dist/client.mjs +15 -0
  5. package/dist/client.mjs.map +1 -0
  6. package/dist/index.d.mts +738 -0
  7. package/dist/index.mjs +2953 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +87 -0
  10. package/src/client.ts +29 -0
  11. package/src/constants.ts +58 -0
  12. package/src/domain-verification.test.ts +551 -0
  13. package/src/index.ts +265 -0
  14. package/src/linking/index.ts +2 -0
  15. package/src/linking/org-assignment.test.ts +325 -0
  16. package/src/linking/org-assignment.ts +176 -0
  17. package/src/linking/types.ts +10 -0
  18. package/src/oidc/discovery.test.ts +1157 -0
  19. package/src/oidc/discovery.ts +494 -0
  20. package/src/oidc/errors.ts +92 -0
  21. package/src/oidc/index.ts +31 -0
  22. package/src/oidc/types.ts +219 -0
  23. package/src/oidc.test.ts +688 -0
  24. package/src/providers.test.ts +1326 -0
  25. package/src/routes/domain-verification.ts +275 -0
  26. package/src/routes/providers.ts +565 -0
  27. package/src/routes/schemas.ts +96 -0
  28. package/src/routes/sso.ts +2750 -0
  29. package/src/saml/algorithms.test.ts +449 -0
  30. package/src/saml/algorithms.ts +338 -0
  31. package/src/saml/assertions.test.ts +239 -0
  32. package/src/saml/assertions.ts +62 -0
  33. package/src/saml/index.ts +13 -0
  34. package/src/saml/parser.ts +56 -0
  35. package/src/saml-state.ts +78 -0
  36. package/src/saml.test.ts +4319 -0
  37. package/src/types.ts +365 -0
  38. package/src/utils.test.ts +103 -0
  39. package/src/utils.ts +81 -0
  40. package/tsconfig.json +14 -0
  41. package/tsdown.config.ts +9 -0
  42. package/vitest.config.ts +3 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * OIDC Discovery Types
3
+ *
4
+ * Types for the OIDC discovery document and hydrated configuration.
5
+ * Based on OpenID Connect Discovery 1.0 specification.
6
+ *
7
+ * @see https://openid.net/specs/openid-connect-discovery-1_0.html
8
+ */
9
+
10
+ /**
11
+ * Raw OIDC Discovery Document as returned by the IdP's
12
+ * .well-known/openid-configuration endpoint.
13
+ *
14
+ * Required fields for Better Auth's OIDC support:
15
+ * - issuer
16
+ * - authorization_endpoint
17
+ * - token_endpoint
18
+ * - jwks_uri (required for ID token validation)
19
+ *
20
+ */
21
+ export interface OIDCDiscoveryDocument {
22
+ /** REQUIRED. URL using the https scheme that the OP asserts as its Issuer Identifier. */
23
+ issuer: string;
24
+
25
+ /** REQUIRED. URL of the OP's OAuth 2.0 Authorization Endpoint. */
26
+ authorization_endpoint: string;
27
+
28
+ /**
29
+ * REQUIRED (spec says "unless only implicit flow is used").
30
+ * URL of the OP's OAuth 2.0 Token Endpoint.
31
+ * We only support authorization code flow.
32
+ */
33
+ token_endpoint: string;
34
+
35
+ /** REQUIRED. URL of the OP's JSON Web Key Set document for ID token validation. */
36
+ jwks_uri: string;
37
+
38
+ /** RECOMMENDED. URL of the OP's UserInfo Endpoint. */
39
+ userinfo_endpoint?: string;
40
+
41
+ /**
42
+ * OPTIONAL. JSON array containing a list of Client Authentication methods
43
+ * supported by this Token Endpoint.
44
+ * Default: ["client_secret_basic"]
45
+ */
46
+ token_endpoint_auth_methods_supported?: string[];
47
+
48
+ /** OPTIONAL. JSON array containing a list of the OAuth 2.0 scope values that this server supports. */
49
+ scopes_supported?: string[];
50
+
51
+ /** OPTIONAL. JSON array containing a list of the OAuth 2.0 response_type values that this OP supports. */
52
+ response_types_supported?: string[];
53
+
54
+ /** OPTIONAL. JSON array containing a list of the Subject Identifier types that this OP supports. */
55
+ subject_types_supported?: string[];
56
+
57
+ /** OPTIONAL. JSON array containing a list of the JWS signing algorithms supported by the OP. */
58
+ id_token_signing_alg_values_supported?: string[];
59
+
60
+ /** OPTIONAL. JSON array containing a list of the claim names that the OP may supply values for. */
61
+ claims_supported?: string[];
62
+
63
+ /** OPTIONAL. URL of a page containing human-readable information about the OP. */
64
+ service_documentation?: string;
65
+
66
+ /** OPTIONAL. Boolean value specifying whether the OP supports use of the claims parameter. */
67
+ claims_parameter_supported?: boolean;
68
+
69
+ /** OPTIONAL. Boolean value specifying whether the OP supports use of the request parameter. */
70
+ request_parameter_supported?: boolean;
71
+
72
+ /** OPTIONAL. Boolean value specifying whether the OP supports use of the request_uri parameter. */
73
+ request_uri_parameter_supported?: boolean;
74
+
75
+ /** OPTIONAL. Boolean value specifying whether the OP requires any request_uri values to be pre-registered. */
76
+ require_request_uri_registration?: boolean;
77
+
78
+ /** OPTIONAL. URL of the OP's end session endpoint. */
79
+ end_session_endpoint?: string;
80
+
81
+ /** OPTIONAL. URL of the OP's revocation endpoint. */
82
+ revocation_endpoint?: string;
83
+
84
+ /** OPTIONAL. URL of the OP's introspection endpoint. */
85
+ introspection_endpoint?: string;
86
+
87
+ /** OPTIONAL. JSON array of PKCE code challenge methods supported (e.g., "S256", "plain"). */
88
+ code_challenge_methods_supported?: string[];
89
+
90
+ /** Allow additional fields from the discovery document */
91
+ [key: string]: unknown;
92
+ }
93
+
94
+ /**
95
+ * Error codes for OIDC discovery operations.
96
+ */
97
+ export type DiscoveryErrorCode =
98
+ /** Request to discovery endpoint timed out */
99
+ | "discovery_timeout"
100
+ /** Discovery endpoint returned 404 or similar */
101
+ | "discovery_not_found"
102
+ /** Discovery endpoint returned invalid JSON */
103
+ | "discovery_invalid_json"
104
+ /** Discovery URL is invalid or malformed */
105
+ | "discovery_invalid_url"
106
+ /** Discovery URL is not trusted by the trusted origins configuration */
107
+ | "discovery_untrusted_origin"
108
+ /** Discovery document issuer doesn't match configured issuer */
109
+ | "issuer_mismatch"
110
+ /** Discovery document is missing required fields */
111
+ | "discovery_incomplete"
112
+ /** IdP only advertises token auth methods that Better Auth doesn't currently support */
113
+ | "unsupported_token_auth_method"
114
+ /** Catch-all for unexpected errors */
115
+ | "discovery_unexpected_error";
116
+
117
+ /**
118
+ * Custom error class for OIDC discovery failures.
119
+ * Can be caught and mapped to APIError at the edge.
120
+ */
121
+ export class DiscoveryError extends Error {
122
+ public readonly code: DiscoveryErrorCode;
123
+ public readonly details?: Record<string, unknown>;
124
+
125
+ constructor(
126
+ code: DiscoveryErrorCode,
127
+ message: string,
128
+ details?: Record<string, unknown>,
129
+ options?: { cause?: unknown },
130
+ ) {
131
+ super(message, options);
132
+ this.name = "DiscoveryError";
133
+ this.code = code;
134
+ this.details = details;
135
+
136
+ // Maintains proper stack trace for where the error was thrown
137
+ if (Error.captureStackTrace) {
138
+ Error.captureStackTrace(this, DiscoveryError);
139
+ }
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Hydrated OIDC configuration after discovery.
145
+ * This is the normalized shape that gets persisted to the database
146
+ * or merged into provider config at runtime.
147
+ *
148
+ * Field names are camelCase to match Better Auth conventions.
149
+ */
150
+ export interface HydratedOIDCConfig {
151
+ /** The issuer URL (validated to match configured issuer) */
152
+ issuer: string;
153
+
154
+ /** The discovery endpoint URL */
155
+ discoveryEndpoint: string;
156
+
157
+ /** URL of the authorization endpoint */
158
+ authorizationEndpoint: string;
159
+
160
+ /** URL of the token endpoint */
161
+ tokenEndpoint: string;
162
+
163
+ /** URL of the JWKS endpoint */
164
+ jwksEndpoint: string;
165
+
166
+ /** URL of the userinfo endpoint (optional) */
167
+ userInfoEndpoint?: string;
168
+
169
+ /** Token endpoint authentication method */
170
+ tokenEndpointAuthentication?: "client_secret_basic" | "client_secret_post";
171
+
172
+ /** Scopes supported by the IdP */
173
+ scopesSupported?: string[];
174
+ }
175
+
176
+ /**
177
+ * Parameters for the discoverOIDCConfig function.
178
+ */
179
+ export interface DiscoverOIDCConfigParams {
180
+ /** The issuer URL to discover configuration from */
181
+ issuer: string;
182
+
183
+ /**
184
+ * Optional existing configuration.
185
+ * Values provided here will override discovered values.
186
+ */
187
+ existingConfig?: Partial<HydratedOIDCConfig>;
188
+
189
+ /**
190
+ * Optional custom discovery endpoint URL.
191
+ * If not provided, defaults to <issuer>/.well-known/openid-configuration
192
+ */
193
+ discoveryEndpoint?: string;
194
+
195
+ /**
196
+ * Optional timeout in milliseconds for the discovery request.
197
+ * @default 10000 (10 seconds)
198
+ */
199
+ timeout?: number;
200
+
201
+ /**
202
+ * Trusted origin predicate. See "trustedOrigins" option
203
+ * @param url the url to test
204
+ * @returns {boolean} return true for urls that belong to a trusted origin and false otherwise
205
+ */
206
+ isTrustedOrigin: (url: string) => boolean;
207
+ }
208
+
209
+ /**
210
+ * Required fields that must be present in a valid discovery document.
211
+ */
212
+ export const REQUIRED_DISCOVERY_FIELDS = [
213
+ "issuer",
214
+ "authorization_endpoint",
215
+ "token_endpoint",
216
+ "jwks_uri",
217
+ ] as const;
218
+
219
+ export type RequiredDiscoveryField = (typeof REQUIRED_DISCOVERY_FIELDS)[number];