@aura-stack/auth 0.1.0-rc.1

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 (141) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/dist/@types/index.cjs +18 -0
  4. package/dist/@types/index.d.cts +9 -0
  5. package/dist/@types/index.d.ts +9 -0
  6. package/dist/@types/index.js +1 -0
  7. package/dist/@types/router.d.cjs +1 -0
  8. package/dist/@types/router.d.d.cts +16 -0
  9. package/dist/@types/router.d.d.ts +16 -0
  10. package/dist/@types/router.d.js +0 -0
  11. package/dist/@types/utility.cjs +18 -0
  12. package/dist/@types/utility.d.cts +6 -0
  13. package/dist/@types/utility.d.ts +6 -0
  14. package/dist/@types/utility.js +1 -0
  15. package/dist/actions/callback/access-token.cjs +170 -0
  16. package/dist/actions/callback/access-token.d.cts +30 -0
  17. package/dist/actions/callback/access-token.d.ts +30 -0
  18. package/dist/actions/callback/access-token.js +8 -0
  19. package/dist/actions/callback/callback.cjs +517 -0
  20. package/dist/actions/callback/callback.d.cts +13 -0
  21. package/dist/actions/callback/callback.d.ts +13 -0
  22. package/dist/actions/callback/callback.js +16 -0
  23. package/dist/actions/callback/userinfo.cjs +165 -0
  24. package/dist/actions/callback/userinfo.d.cts +22 -0
  25. package/dist/actions/callback/userinfo.d.ts +22 -0
  26. package/dist/actions/callback/userinfo.js +10 -0
  27. package/dist/actions/csrfToken/csrfToken.cjs +207 -0
  28. package/dist/actions/csrfToken/csrfToken.d.cts +5 -0
  29. package/dist/actions/csrfToken/csrfToken.d.ts +5 -0
  30. package/dist/actions/csrfToken/csrfToken.js +12 -0
  31. package/dist/actions/index.cjs +864 -0
  32. package/dist/actions/index.d.cts +14 -0
  33. package/dist/actions/index.d.ts +14 -0
  34. package/dist/actions/index.js +34 -0
  35. package/dist/actions/session/session.cjs +191 -0
  36. package/dist/actions/session/session.d.cts +5 -0
  37. package/dist/actions/session/session.d.ts +5 -0
  38. package/dist/actions/session/session.js +11 -0
  39. package/dist/actions/signIn/authorization.cjs +274 -0
  40. package/dist/actions/signIn/authorization.d.cts +45 -0
  41. package/dist/actions/signIn/authorization.d.ts +45 -0
  42. package/dist/actions/signIn/authorization.js +16 -0
  43. package/dist/actions/signIn/signIn.cjs +469 -0
  44. package/dist/actions/signIn/signIn.d.cts +13 -0
  45. package/dist/actions/signIn/signIn.d.ts +13 -0
  46. package/dist/actions/signIn/signIn.js +14 -0
  47. package/dist/actions/signOut/signOut.cjs +490 -0
  48. package/dist/actions/signOut/signOut.d.cts +8 -0
  49. package/dist/actions/signOut/signOut.d.ts +8 -0
  50. package/dist/actions/signOut/signOut.js +15 -0
  51. package/dist/assert.cjs +44 -0
  52. package/dist/assert.d.ts +5 -0
  53. package/dist/assert.js +10 -0
  54. package/dist/chunk-256KIVJL.js +99 -0
  55. package/dist/chunk-42XB3YCW.js +22 -0
  56. package/dist/chunk-6SM22VVJ.js +18 -0
  57. package/dist/chunk-CAKJT3KS.js +92 -0
  58. package/dist/chunk-E3OXBRYF.js +22 -0
  59. package/dist/chunk-EBPE35JT.js +31 -0
  60. package/dist/chunk-FIPU4MLT.js +21 -0
  61. package/dist/chunk-FJUDBLCP.js +59 -0
  62. package/dist/chunk-FKRDCWBF.js +22 -0
  63. package/dist/chunk-GZU3RBTB.js +62 -0
  64. package/dist/chunk-HGJ4TXY4.js +137 -0
  65. package/dist/chunk-HMRKN75I.js +74 -0
  66. package/dist/chunk-IKHPGFCW.js +14 -0
  67. package/dist/chunk-ITQ7352M.js +0 -0
  68. package/dist/chunk-JAPMIE6S.js +10 -0
  69. package/dist/chunk-KRNOMBXQ.js +22 -0
  70. package/dist/chunk-LLR722CL.js +96 -0
  71. package/dist/chunk-PG7UYFG5.js +0 -0
  72. package/dist/chunk-RLT4RFKV.js +45 -0
  73. package/dist/chunk-SJPDVKUS.js +112 -0
  74. package/dist/chunk-SMQO5WD7.js +30 -0
  75. package/dist/chunk-STHEPPUZ.js +11 -0
  76. package/dist/chunk-UJJ7R56J.js +52 -0
  77. package/dist/chunk-VFTYH33W.js +61 -0
  78. package/dist/chunk-X7M4CQTN.js +25 -0
  79. package/dist/chunk-XXJKNKGQ.js +37 -0
  80. package/dist/chunk-ZV4BH47P.js +154 -0
  81. package/dist/cookie.cjs +201 -0
  82. package/dist/cookie.d.ts +95 -0
  83. package/dist/cookie.js +36 -0
  84. package/dist/error.cjs +88 -0
  85. package/dist/error.d.ts +64 -0
  86. package/dist/error.js +16 -0
  87. package/dist/headers.cjs +35 -0
  88. package/dist/headers.d.ts +9 -0
  89. package/dist/headers.js +6 -0
  90. package/dist/index-CGRZ0wrw.d.ts +530 -0
  91. package/dist/index.cjs +1089 -0
  92. package/dist/index.d.ts +43 -0
  93. package/dist/index.js +75 -0
  94. package/dist/jose.cjs +68 -0
  95. package/dist/jose.d.ts +20 -0
  96. package/dist/jose.js +9 -0
  97. package/dist/oauth/bitbucket.cjs +45 -0
  98. package/dist/oauth/bitbucket.d.cts +9 -0
  99. package/dist/oauth/bitbucket.d.ts +9 -0
  100. package/dist/oauth/bitbucket.js +6 -0
  101. package/dist/oauth/discord.cjs +55 -0
  102. package/dist/oauth/discord.d.cts +9 -0
  103. package/dist/oauth/discord.d.ts +9 -0
  104. package/dist/oauth/discord.js +6 -0
  105. package/dist/oauth/figma.cjs +46 -0
  106. package/dist/oauth/figma.d.cts +9 -0
  107. package/dist/oauth/figma.d.ts +9 -0
  108. package/dist/oauth/figma.js +6 -0
  109. package/dist/oauth/github.cjs +38 -0
  110. package/dist/oauth/github.d.cts +9 -0
  111. package/dist/oauth/github.d.ts +9 -0
  112. package/dist/oauth/github.js +6 -0
  113. package/dist/oauth/gitlab.cjs +46 -0
  114. package/dist/oauth/gitlab.d.cts +9 -0
  115. package/dist/oauth/gitlab.d.ts +9 -0
  116. package/dist/oauth/gitlab.js +6 -0
  117. package/dist/oauth/index.cjs +213 -0
  118. package/dist/oauth/index.d.cts +9 -0
  119. package/dist/oauth/index.d.ts +9 -0
  120. package/dist/oauth/index.js +36 -0
  121. package/dist/oauth/spotify.cjs +46 -0
  122. package/dist/oauth/spotify.d.cts +9 -0
  123. package/dist/oauth/spotify.d.ts +9 -0
  124. package/dist/oauth/spotify.js +6 -0
  125. package/dist/oauth/x.cjs +46 -0
  126. package/dist/oauth/x.d.cts +9 -0
  127. package/dist/oauth/x.d.ts +9 -0
  128. package/dist/oauth/x.js +6 -0
  129. package/dist/response.cjs +34 -0
  130. package/dist/response.d.ts +10 -0
  131. package/dist/response.js +6 -0
  132. package/dist/schemas.cjs +105 -0
  133. package/dist/schemas.d.ts +130 -0
  134. package/dist/schemas.js +20 -0
  135. package/dist/secure.cjs +120 -0
  136. package/dist/secure.d.ts +43 -0
  137. package/dist/secure.js +18 -0
  138. package/dist/utils.cjs +141 -0
  139. package/dist/utils.d.ts +45 -0
  140. package/dist/utils.js +23 -0
  141. package/package.json +70 -0
@@ -0,0 +1,22 @@
1
+ // src/oauth/x.ts
2
+ var x = {
3
+ id: "x",
4
+ name: "X",
5
+ authorizeURL: "https://x.com/i/oauth2/authorize",
6
+ accessToken: "https://api.x.com/2/oauth2/token",
7
+ userInfo: "https://api.x.com/2/users/me?user.fields=profile_image_url",
8
+ scope: "users.read users.email tweet.read offline.access",
9
+ responseType: "code",
10
+ profile({ data }) {
11
+ return {
12
+ sub: data.id,
13
+ name: data.name,
14
+ image: data.profile_image_url,
15
+ email: ""
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ x
22
+ };
@@ -0,0 +1,18 @@
1
+ // src/assert.ts
2
+ var isFalsy = (value) => {
3
+ return value === false || value === 0 || value === "" || value === null || value === void 0 || Number.isNaN(value);
4
+ };
5
+ var isRequest = (value) => {
6
+ return typeof Request !== "undefined" && value instanceof Request;
7
+ };
8
+ var isValidURL = (value) => {
9
+ if (value.includes("\r\n") || value.includes("\n") || value.includes("\r")) return false;
10
+ const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()*+,;=:@-]*)*\/?$/;
11
+ return regex.test(value);
12
+ };
13
+
14
+ export {
15
+ isFalsy,
16
+ isRequest,
17
+ isValidURL
18
+ };
@@ -0,0 +1,92 @@
1
+ import {
2
+ isValidURL
3
+ } from "./chunk-6SM22VVJ.js";
4
+ import {
5
+ equals,
6
+ getNormalizedOriginPath,
7
+ sanitizeURL,
8
+ toCastCase
9
+ } from "./chunk-256KIVJL.js";
10
+ import {
11
+ AuthError,
12
+ ERROR_RESPONSE,
13
+ InvalidRedirectToError,
14
+ isAuthError
15
+ } from "./chunk-FJUDBLCP.js";
16
+ import {
17
+ OAuthAuthorization
18
+ } from "./chunk-HMRKN75I.js";
19
+
20
+ // src/actions/signIn/authorization.ts
21
+ var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod) => {
22
+ const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod });
23
+ if (!parsed.success) {
24
+ throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.SERVER_ERROR, "Invalid OAuth configuration");
25
+ }
26
+ const { authorizeURL, ...options } = parsed.data;
27
+ const { userInfo, accessToken, clientSecret, ...required } = options;
28
+ const searchParams = new URLSearchParams(toCastCase(required));
29
+ return `${authorizeURL}?${searchParams}`;
30
+ };
31
+ var getOriginURL = (request, trustedProxyHeaders) => {
32
+ const headers = request.headers;
33
+ if (trustedProxyHeaders) {
34
+ const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http";
35
+ const host = headers.get("X-Forwarded-Host") ?? headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? null;
36
+ return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`);
37
+ } else {
38
+ return new URL(getNormalizedOriginPath(request.url));
39
+ }
40
+ };
41
+ var createRedirectURI = (request, oauth, basePath, trustedProxyHeaders) => {
42
+ const url = getOriginURL(request, trustedProxyHeaders);
43
+ return `${url.origin}${basePath}/callback/${oauth}`;
44
+ };
45
+ var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
46
+ try {
47
+ const headers = request.headers;
48
+ const origin = headers.get("Origin");
49
+ const referer = headers.get("Referer");
50
+ let hostedURL = getOriginURL(request, trustedProxyHeaders);
51
+ if (redirectTo) {
52
+ if (redirectTo.startsWith("/")) {
53
+ return sanitizeURL(redirectTo);
54
+ }
55
+ const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)));
56
+ if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
57
+ throw new InvalidRedirectToError();
58
+ }
59
+ return sanitizeURL(redirectToURL.pathname);
60
+ }
61
+ if (referer) {
62
+ const refererURL = new URL(sanitizeURL(referer));
63
+ if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
64
+ throw new AuthError(
65
+ ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST,
66
+ "The referer of the request does not match the hosted origin."
67
+ );
68
+ }
69
+ return sanitizeURL(refererURL.pathname);
70
+ }
71
+ if (origin) {
72
+ const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)));
73
+ if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
74
+ throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).");
75
+ }
76
+ return sanitizeURL(originURL.pathname);
77
+ }
78
+ return "/";
79
+ } catch (error) {
80
+ if (isAuthError(error)) {
81
+ throw error;
82
+ }
83
+ throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).");
84
+ }
85
+ };
86
+
87
+ export {
88
+ createAuthorizationURL,
89
+ getOriginURL,
90
+ createRedirectURI,
91
+ createRedirectTo
92
+ };
@@ -0,0 +1,22 @@
1
+ // src/oauth/spotify.ts
2
+ var spotify = {
3
+ id: "spotify",
4
+ name: "Spotify",
5
+ authorizeURL: "https://accounts.spotify.com/authorize",
6
+ accessToken: "https://accounts.spotify.com/api/token",
7
+ userInfo: "https://api.spotify.com/v1/me",
8
+ scope: "user-read-email user-read-private",
9
+ responseType: "token",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id,
13
+ name: profile.display_name,
14
+ email: profile.email,
15
+ image: profile.images?.[0]?.url
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ spotify
22
+ };
@@ -0,0 +1,31 @@
1
+ // src/oauth/discord.ts
2
+ var discord = {
3
+ id: "discord",
4
+ name: "Discord",
5
+ authorizeURL: "https://discord.com/oauth2/authorize",
6
+ accessToken: "https://discord.com/api/oauth2/token",
7
+ userInfo: "https://discord.com/api/users/@me",
8
+ scope: "identify email",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ let image = "";
12
+ if (profile.avatar === null) {
13
+ const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
14
+ image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
15
+ } else {
16
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
17
+ image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
18
+ }
19
+ return {
20
+ sub: profile.id,
21
+ // https://discord.com/developers/docs/change-log#display-names
22
+ name: profile.global_name ?? profile.username,
23
+ email: profile.email ?? "",
24
+ image
25
+ };
26
+ }
27
+ };
28
+
29
+ export {
30
+ discord
31
+ };
@@ -0,0 +1,21 @@
1
+ // src/oauth/bitbucket.ts
2
+ var bitbucket = {
3
+ id: "bitbucket",
4
+ name: "Bitbucket",
5
+ authorizeURL: "https://bitbucket.org/site/oauth2/authorize",
6
+ accessToken: "https://bitbucket.org/site/oauth2/access_token",
7
+ userInfo: "https://api.bitbucket.org/2.0/user",
8
+ scope: "account email",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.uuid ?? profile.account_id,
13
+ name: profile.display_name ?? profile.nickname,
14
+ image: profile.links.avatar.href
15
+ };
16
+ }
17
+ };
18
+
19
+ export {
20
+ bitbucket
21
+ };
@@ -0,0 +1,59 @@
1
+ // src/error.ts
2
+ var AuthError = class extends Error {
3
+ constructor(type, message) {
4
+ super(message);
5
+ this.type = type;
6
+ this.name = "AuthError";
7
+ }
8
+ };
9
+ var InvalidCsrfTokenError = class extends AuthError {
10
+ constructor(message = "The provided CSRF token is invalid or has expired") {
11
+ super("invalid_csrf_token", message);
12
+ this.name = "InvalidCsrfTokenError";
13
+ }
14
+ };
15
+ var InvalidRedirectToError = class extends AuthError {
16
+ constructor(message = "The redirectTo parameter does not match the hosted origin.") {
17
+ super("invalid_redirect_to", message);
18
+ this.name = "InvalidRedirectToError";
19
+ }
20
+ };
21
+ var isAuthError = (error) => {
22
+ return error instanceof AuthError;
23
+ };
24
+ var throwAuthError = (error, message) => {
25
+ if (error instanceof Error) {
26
+ if (isAuthError(error)) {
27
+ throw error;
28
+ }
29
+ throw new AuthError("invalid_request", error.message ?? message);
30
+ }
31
+ };
32
+ var ERROR_RESPONSE = {
33
+ AUTHORIZATION: {
34
+ INVALID_REQUEST: "invalid_request",
35
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
36
+ ACCESS_DENIED: "access_denied",
37
+ UNSUPPORTED_RESPONSE_TYPE: "unsupported_response_type",
38
+ INVALID_SCOPE: "invalid_scope",
39
+ SERVER_ERROR: "server_error",
40
+ TEMPORARILY_UNAVAILABLE: "temporarily_unavailable"
41
+ },
42
+ ACCESS_TOKEN: {
43
+ INVALID_REQUEST: "invalid_request",
44
+ INVALID_CLIENT: "invalid_client",
45
+ INVALID_GRANT: "invalid_grant",
46
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
47
+ UNSUPPORTED_GRANT_TYPE: "unsupported_grant_type",
48
+ INVALID_SCOPE: "invalid_scope"
49
+ }
50
+ };
51
+
52
+ export {
53
+ AuthError,
54
+ InvalidCsrfTokenError,
55
+ InvalidRedirectToError,
56
+ isAuthError,
57
+ throwAuthError,
58
+ ERROR_RESPONSE
59
+ };
@@ -0,0 +1,22 @@
1
+ // src/oauth/figma.ts
2
+ var figma = {
3
+ id: "figma",
4
+ name: "Figma",
5
+ authorizeURL: "https://www.figma.com/oauth",
6
+ accessToken: "https://api.figma.com/v1/oauth/token",
7
+ userInfo: "https://api.figma.com/v1/me",
8
+ scope: "current_user:read",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id,
13
+ name: profile.handle,
14
+ email: profile.email,
15
+ image: profile.img_url
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ figma
22
+ };
@@ -0,0 +1,62 @@
1
+ import {
2
+ equals
3
+ } from "./chunk-256KIVJL.js";
4
+ import {
5
+ InvalidCsrfTokenError
6
+ } from "./chunk-FJUDBLCP.js";
7
+
8
+ // src/secure.ts
9
+ import crypto from "crypto";
10
+ var generateSecure = (length = 32) => {
11
+ return crypto.randomBytes(length).toString("base64url");
12
+ };
13
+ var createHash = (data, base = "hex") => {
14
+ return crypto.createHash("sha256").update(data).digest().toString(base);
15
+ };
16
+ var createPKCE = async (verifier) => {
17
+ const codeVerifier = verifier ?? generateSecure(86);
18
+ const codeChallenge = createHash(codeVerifier, "base64url");
19
+ return { codeVerifier, codeChallenge, method: "S256" };
20
+ };
21
+ var createCSRF = async (jose, csrfCookie) => {
22
+ try {
23
+ const token = generateSecure(32);
24
+ if (csrfCookie) {
25
+ await jose.verifyJWS(csrfCookie);
26
+ return csrfCookie;
27
+ }
28
+ return jose.signJWS({ token });
29
+ } catch {
30
+ const token = generateSecure(32);
31
+ return jose.signJWS({ token });
32
+ }
33
+ };
34
+ var verifyCSRF = async (jose, cookie, header) => {
35
+ try {
36
+ const { token: cookieToken } = await jose.verifyJWS(cookie);
37
+ const { token: headerToken } = await jose.verifyJWS(header);
38
+ const cookieBuffer = Buffer.from(cookieToken);
39
+ const headerBuffer = Buffer.from(headerToken);
40
+ if (!equals(headerBuffer.length, cookieBuffer.length)) {
41
+ throw new InvalidCsrfTokenError();
42
+ }
43
+ if (!crypto.timingSafeEqual(cookieBuffer, headerBuffer)) {
44
+ throw new InvalidCsrfTokenError();
45
+ }
46
+ return true;
47
+ } catch {
48
+ throw new InvalidCsrfTokenError();
49
+ }
50
+ };
51
+ var createDerivedSalt = (secret) => {
52
+ return crypto.createHash("sha256").update(secret).update("aura-auth-salt").digest("hex");
53
+ };
54
+
55
+ export {
56
+ generateSecure,
57
+ createHash,
58
+ createPKCE,
59
+ createCSRF,
60
+ verifyCSRF,
61
+ createDerivedSalt
62
+ };
@@ -0,0 +1,137 @@
1
+ import {
2
+ getUserInfo
3
+ } from "./chunk-RLT4RFKV.js";
4
+ import {
5
+ createAccessToken
6
+ } from "./chunk-UJJ7R56J.js";
7
+ import {
8
+ createSessionCookie,
9
+ expireCookie,
10
+ getCookie,
11
+ secureCookieOptions,
12
+ setCookie
13
+ } from "./chunk-ZV4BH47P.js";
14
+ import {
15
+ cacheControl
16
+ } from "./chunk-STHEPPUZ.js";
17
+ import {
18
+ createCSRF
19
+ } from "./chunk-GZU3RBTB.js";
20
+ import {
21
+ equals,
22
+ isValidRelativePath,
23
+ sanitizeURL
24
+ } from "./chunk-256KIVJL.js";
25
+ import {
26
+ AuthError,
27
+ ERROR_RESPONSE,
28
+ isAuthError
29
+ } from "./chunk-FJUDBLCP.js";
30
+ import {
31
+ AuraResponse
32
+ } from "./chunk-JAPMIE6S.js";
33
+ import {
34
+ OAuthAuthorizationErrorResponse,
35
+ OAuthAuthorizationResponse
36
+ } from "./chunk-HMRKN75I.js";
37
+
38
+ // src/actions/callback/callback.ts
39
+ import z from "zod";
40
+ import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router";
41
+ var callbackConfig = (oauth) => {
42
+ return createEndpointConfig("/callback/:oauth", {
43
+ schemas: {
44
+ searchParams: OAuthAuthorizationResponse,
45
+ params: z.object({
46
+ oauth: z.enum(Object.keys(oauth))
47
+ })
48
+ },
49
+ middlewares: [
50
+ (ctx) => {
51
+ const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams);
52
+ if (response.success) {
53
+ const { error, error_description } = response.data;
54
+ throw new AuthError(error, error_description ?? "OAuth Authorization Error");
55
+ }
56
+ return ctx;
57
+ }
58
+ ]
59
+ });
60
+ };
61
+ var callbackAction = (oauth) => {
62
+ return createEndpoint(
63
+ "GET",
64
+ "/callback/:oauth",
65
+ async (ctx) => {
66
+ const {
67
+ request,
68
+ params: { oauth: oauth2 },
69
+ searchParams: { code, state },
70
+ context: { oauth: providers, cookies, jose, trustedProxyHeaders }
71
+ } = ctx;
72
+ try {
73
+ const oauthConfig = providers[oauth2];
74
+ const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
75
+ const cookieState = getCookie(request, "state", cookieOptions);
76
+ const cookieRedirectTo = getCookie(request, "redirect_to", cookieOptions);
77
+ const cookieRedirectURI = getCookie(request, "redirect_uri", cookieOptions);
78
+ const codeVerifier = getCookie(request, "code_verifier", cookieOptions);
79
+ if (!equals(cookieState, state)) {
80
+ throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Mismatching state");
81
+ }
82
+ const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier);
83
+ const sanitized = sanitizeURL(cookieRedirectTo);
84
+ if (!isValidRelativePath(sanitized)) {
85
+ throw new AuthError(
86
+ ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST,
87
+ "Invalid redirect path. Potential open redirect attack detected."
88
+ );
89
+ }
90
+ const headers = new Headers(cacheControl);
91
+ headers.set("Location", sanitized);
92
+ const userInfo = await getUserInfo(oauthConfig, accessToken.access_token);
93
+ const sessionCookie = await createSessionCookie(userInfo, cookieOptions, jose);
94
+ const csrfToken = await createCSRF(jose);
95
+ const csrfCookie = setCookie(
96
+ "csrfToken",
97
+ csrfToken,
98
+ secureCookieOptions(
99
+ request,
100
+ {
101
+ ...cookies,
102
+ strategy: "host"
103
+ },
104
+ trustedProxyHeaders
105
+ )
106
+ );
107
+ headers.set("Set-Cookie", sessionCookie);
108
+ headers.append("Set-Cookie", expireCookie("state", cookieOptions));
109
+ headers.append("Set-Cookie", expireCookie("redirect_uri", cookieOptions));
110
+ headers.append("Set-Cookie", expireCookie("redirect_to", cookieOptions));
111
+ headers.append("Set-Cookie", expireCookie("code_verifier", cookieOptions));
112
+ headers.append("Set-Cookie", csrfCookie);
113
+ return Response.json({ oauth: oauth2 }, { status: 302, headers });
114
+ } catch (error) {
115
+ if (isAuthError(error)) {
116
+ const { type, message } = error;
117
+ return AuraResponse.json(
118
+ { error: type, error_description: message },
119
+ { status: statusCode.BAD_REQUEST }
120
+ );
121
+ }
122
+ return AuraResponse.json(
123
+ {
124
+ error: ERROR_RESPONSE.ACCESS_TOKEN.INVALID_CLIENT,
125
+ error_description: "An unexpected error occurred"
126
+ },
127
+ { status: statusCode.INTERNAL_SERVER_ERROR }
128
+ );
129
+ }
130
+ },
131
+ callbackConfig(oauth)
132
+ );
133
+ };
134
+
135
+ export {
136
+ callbackAction
137
+ };
@@ -0,0 +1,74 @@
1
+ // src/schemas.ts
2
+ import { object, string, enum as options, number, url } from "zod/v4";
3
+ var OAuthProviderConfigSchema = object({
4
+ authorizeURL: url(),
5
+ accessToken: url(),
6
+ scope: string().optional(),
7
+ userInfo: url(),
8
+ responseType: options(["code", "token", "id_token"]),
9
+ clientId: string(),
10
+ clientSecret: string()
11
+ });
12
+ var OAuthAuthorization = OAuthProviderConfigSchema.extend({
13
+ redirectURI: string(),
14
+ state: string(),
15
+ codeChallenge: string(),
16
+ codeChallengeMethod: options(["plain", "S256"])
17
+ });
18
+ var OAuthAuthorizationResponse = object({
19
+ state: string(),
20
+ code: string()
21
+ });
22
+ var OAuthAuthorizationErrorResponse = object({
23
+ error: options([
24
+ "invalid_request",
25
+ "unauthorized_client",
26
+ "access_denied",
27
+ "unsupported_response_type",
28
+ "invalid_scope",
29
+ "server_error",
30
+ "temporarily_unavailable"
31
+ ]),
32
+ error_description: string().optional(),
33
+ error_uri: string().optional(),
34
+ state: string()
35
+ });
36
+ var OAuthAccessToken = OAuthProviderConfigSchema.extend({
37
+ redirectURI: string(),
38
+ code: string(),
39
+ codeVerifier: string().min(43).max(128)
40
+ });
41
+ var OAuthAccessTokenResponse = object({
42
+ access_token: string(),
43
+ token_type: string(),
44
+ expires_in: number().optional(),
45
+ refresh_token: string().optional(),
46
+ scope: string().optional()
47
+ });
48
+ var OAuthAccessTokenErrorResponse = object({
49
+ error: options([
50
+ "invalid_request",
51
+ "invalid_client",
52
+ "invalid_grant",
53
+ "unauthorized_client",
54
+ "unsupported_grant_type",
55
+ "invalid_scope"
56
+ ]),
57
+ error_description: string().optional(),
58
+ error_uri: string().optional()
59
+ });
60
+ var OAuthErrorResponse = object({
61
+ error: string(),
62
+ error_description: string().optional()
63
+ });
64
+
65
+ export {
66
+ OAuthProviderConfigSchema,
67
+ OAuthAuthorization,
68
+ OAuthAuthorizationResponse,
69
+ OAuthAuthorizationErrorResponse,
70
+ OAuthAccessToken,
71
+ OAuthAccessTokenResponse,
72
+ OAuthAccessTokenErrorResponse,
73
+ OAuthErrorResponse
74
+ };
@@ -0,0 +1,14 @@
1
+ // src/oauth/github.ts
2
+ var github = {
3
+ id: "github",
4
+ name: "GitHub",
5
+ authorizeURL: "https://github.com/login/oauth/authorize",
6
+ accessToken: "https://github.com/login/oauth/access_token",
7
+ userInfo: "https://api.github.com/user",
8
+ scope: "read:user user:email",
9
+ responseType: "code"
10
+ };
11
+
12
+ export {
13
+ github
14
+ };
File without changes
@@ -0,0 +1,10 @@
1
+ // src/response.ts
2
+ var AuraResponse = class extends Response {
3
+ static json(body, init) {
4
+ return Response.json(body, init);
5
+ }
6
+ };
7
+
8
+ export {
9
+ AuraResponse
10
+ };
@@ -0,0 +1,22 @@
1
+ // src/oauth/gitlab.ts
2
+ var gitlab = {
3
+ id: "gitlab",
4
+ name: "GitLab",
5
+ authorizeURL: "https://gitlab.com/oauth/authorize",
6
+ accessToken: "https://gitlab.com/oauth/token",
7
+ userInfo: "https://gitlab.com/api/v4/user",
8
+ scope: "read_user",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id.toString(),
13
+ name: profile.name ?? profile.username,
14
+ email: profile.email,
15
+ avatar: profile.avatar_url
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ gitlab
22
+ };