@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,56 @@
1
+ import { XMLParser } from "fast-xml-parser";
2
+
3
+ export const xmlParser = new XMLParser({
4
+ ignoreAttributes: false,
5
+ attributeNamePrefix: "@_",
6
+ removeNSPrefix: true,
7
+ processEntities: false,
8
+ });
9
+
10
+ export function findNode(obj: unknown, nodeName: string): unknown {
11
+ if (!obj || typeof obj !== "object") return null;
12
+
13
+ const record = obj as Record<string, unknown>;
14
+
15
+ if (nodeName in record) {
16
+ return record[nodeName];
17
+ }
18
+
19
+ for (const value of Object.values(record)) {
20
+ if (Array.isArray(value)) {
21
+ for (const item of value) {
22
+ const found = findNode(item, nodeName);
23
+ if (found) return found;
24
+ }
25
+ } else if (typeof value === "object" && value !== null) {
26
+ const found = findNode(value, nodeName);
27
+ if (found) return found;
28
+ }
29
+ }
30
+
31
+ return null;
32
+ }
33
+
34
+ export function countAllNodes(obj: unknown, nodeName: string): number {
35
+ if (!obj || typeof obj !== "object") return 0;
36
+
37
+ let count = 0;
38
+ const record = obj as Record<string, unknown>;
39
+
40
+ if (nodeName in record) {
41
+ const node = record[nodeName];
42
+ count += Array.isArray(node) ? node.length : 1;
43
+ }
44
+
45
+ for (const value of Object.values(record)) {
46
+ if (Array.isArray(value)) {
47
+ for (const item of value) {
48
+ count += countAllNodes(item, nodeName);
49
+ }
50
+ } else if (typeof value === "object" && value !== null) {
51
+ count += countAllNodes(value, nodeName);
52
+ }
53
+ }
54
+
55
+ return count;
56
+ }
@@ -0,0 +1,78 @@
1
+ import type { GenericEndpointContext, StateData } from "better-auth";
2
+ import { generateGenericState, parseGenericState } from "better-auth";
3
+ import { generateRandomString } from "better-auth/crypto";
4
+ import { APIError } from "better-call";
5
+
6
+ export async function generateRelayState(
7
+ c: GenericEndpointContext,
8
+ link:
9
+ | {
10
+ email: string;
11
+ userId: string;
12
+ }
13
+ | undefined,
14
+ additionalData: Record<string, any> | false | undefined,
15
+ ) {
16
+ const callbackURL = c.body.callbackURL;
17
+ if (!callbackURL) {
18
+ throw new APIError("BAD_REQUEST", {
19
+ message: "callbackURL is required",
20
+ });
21
+ }
22
+
23
+ const codeVerifier = generateRandomString(128);
24
+ const stateData: StateData = {
25
+ ...(additionalData ? additionalData : {}),
26
+ callbackURL,
27
+ codeVerifier,
28
+ errorURL: c.body.errorCallbackURL,
29
+ newUserURL: c.body.newUserCallbackURL,
30
+ link,
31
+ /**
32
+ * This is the actual expiry time of the state
33
+ */
34
+ expiresAt: Date.now() + 10 * 60 * 1000,
35
+ requestSignUp: c.body.requestSignUp,
36
+ };
37
+
38
+ try {
39
+ return generateGenericState(c, stateData, {
40
+ cookieName: "relay_state",
41
+ });
42
+ } catch (error) {
43
+ c.context.logger.error(
44
+ "Failed to create verification for relay state",
45
+ error,
46
+ );
47
+ throw new APIError("INTERNAL_SERVER_ERROR", {
48
+ message: "State error: Unable to create verification for relay state",
49
+ cause: error,
50
+ });
51
+ }
52
+ }
53
+
54
+ export async function parseRelayState(c: GenericEndpointContext) {
55
+ const state = c.body.RelayState;
56
+ const errorURL =
57
+ c.context.options.onAPIError?.errorURL || `${c.context.baseURL}/error`;
58
+
59
+ let parsedData: StateData;
60
+
61
+ try {
62
+ parsedData = await parseGenericState(c, state, {
63
+ cookieName: "relay_state",
64
+ });
65
+ } catch (error) {
66
+ c.context.logger.error("Failed to parse relay state", error);
67
+ throw new APIError("BAD_REQUEST", {
68
+ message: "State error: failed to validate relay state",
69
+ cause: error,
70
+ });
71
+ }
72
+
73
+ if (!parsedData.errorURL) {
74
+ parsedData.errorURL = errorURL;
75
+ }
76
+
77
+ return parsedData;
78
+ }