@aura-stack/auth 0.1.0-rc.9 → 0.2.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 (153) hide show
  1. package/dist/@types/index.cjs +15 -15
  2. package/dist/@types/index.d.ts +7 -31
  3. package/dist/@types/index.js +1 -1
  4. package/dist/@types/router.d.cjs +1 -1
  5. package/dist/@types/router.d.d.ts +7 -7
  6. package/dist/@types/utility.cjs +15 -15
  7. package/dist/@types/utility.d.ts +4 -8
  8. package/dist/@types/utility.js +1 -1
  9. package/dist/actions/callback/access-token.cjs +176 -155
  10. package/dist/actions/callback/access-token.d.ts +15 -20
  11. package/dist/actions/callback/access-token.js +10 -4
  12. package/dist/actions/callback/callback.cjs +394 -490
  13. package/dist/actions/callback/callback.d.ts +10 -12
  14. package/dist/actions/callback/callback.js +16 -12
  15. package/dist/actions/callback/userinfo.cjs +166 -145
  16. package/dist/actions/callback/userinfo.d.ts +9 -9
  17. package/dist/actions/callback/userinfo.js +12 -6
  18. package/dist/actions/csrfToken/csrfToken.cjs +112 -190
  19. package/dist/actions/csrfToken/csrfToken.d.ts +3 -3
  20. package/dist/actions/csrfToken/csrfToken.js +12 -8
  21. package/dist/actions/index.cjs +652 -814
  22. package/dist/actions/index.d.ts +13 -13
  23. package/dist/actions/index.js +34 -18
  24. package/dist/actions/session/session.cjs +86 -176
  25. package/dist/actions/session/session.d.ts +3 -3
  26. package/dist/actions/session/session.js +10 -7
  27. package/dist/actions/signIn/authorization.cjs +254 -263
  28. package/dist/actions/signIn/authorization.d.ts +12 -18
  29. package/dist/actions/signIn/authorization.js +16 -6
  30. package/dist/actions/signIn/signIn.cjs +320 -456
  31. package/dist/actions/signIn/signIn.d.ts +10 -10
  32. package/dist/actions/signIn/signIn.js +13 -10
  33. package/dist/actions/signOut/signOut.cjs +304 -473
  34. package/dist/actions/signOut/signOut.d.ts +3 -3
  35. package/dist/actions/signOut/signOut.js +14 -11
  36. package/dist/assert.cjs +40 -36
  37. package/dist/assert.d.ts +13 -4
  38. package/dist/assert.js +12 -2
  39. package/dist/chunk-3EUWD5BB.js +63 -0
  40. package/dist/chunk-42XB3YCW.js +19 -17
  41. package/dist/chunk-6R2YZ4AC.js +22 -0
  42. package/dist/chunk-A3N4PVAT.js +70 -0
  43. package/dist/chunk-B737EUJV.js +22 -0
  44. package/dist/chunk-CXLATHS5.js +143 -0
  45. package/dist/chunk-E3OXBRYF.js +19 -17
  46. package/dist/chunk-EIL2FPSS.js +22 -0
  47. package/dist/chunk-EMKJA2GJ.js +89 -0
  48. package/dist/chunk-FIPU4MLT.js +18 -16
  49. package/dist/chunk-FKRDCWBF.js +19 -17
  50. package/dist/chunk-GA2SMTJO.js +58 -0
  51. package/dist/chunk-HP34YGGJ.js +22 -0
  52. package/dist/chunk-HT4YLL7N.js +35 -0
  53. package/dist/chunk-IKHPGFCW.js +11 -9
  54. package/dist/chunk-IUYZQTJV.js +30 -0
  55. package/dist/chunk-IVET23KF.js +58 -0
  56. package/dist/chunk-JVFTCTTE.js +33 -0
  57. package/dist/chunk-KRNOMBXQ.js +19 -17
  58. package/dist/chunk-KSWLO5ZU.js +102 -0
  59. package/dist/chunk-N2APGLXA.js +71 -0
  60. package/dist/chunk-N4SX7TZT.js +96 -0
  61. package/dist/chunk-RRLIF4PQ.js +55 -0
  62. package/dist/chunk-STHEPPUZ.js +8 -6
  63. package/dist/chunk-TLE4PXY3.js +39 -0
  64. package/dist/chunk-W6LG7BFW.js +197 -0
  65. package/dist/chunk-YRCB5FLE.js +79 -0
  66. package/dist/chunk-ZNCZVF6U.js +14 -0
  67. package/dist/cookie.cjs +222 -187
  68. package/dist/cookie.d.ts +39 -76
  69. package/dist/cookie.js +27 -34
  70. package/dist/errors.cjs +85 -0
  71. package/dist/errors.d.ts +49 -0
  72. package/dist/errors.js +18 -0
  73. package/dist/headers.cjs +28 -28
  74. package/dist/headers.d.ts +2 -2
  75. package/dist/headers.js +6 -2
  76. package/dist/index-DkaLJFn8.d.ts +679 -0
  77. package/dist/index.cjs +1074 -1007
  78. package/dist/index.d.ts +10 -31
  79. package/dist/index.js +85 -51
  80. package/dist/jose.cjs +74 -66
  81. package/dist/jose.d.ts +11 -8
  82. package/dist/jose.js +10 -5
  83. package/dist/oauth/bitbucket.cjs +38 -38
  84. package/dist/oauth/bitbucket.d.ts +7 -7
  85. package/dist/oauth/bitbucket.js +6 -2
  86. package/dist/oauth/discord.cjs +47 -48
  87. package/dist/oauth/discord.d.ts +7 -7
  88. package/dist/oauth/discord.js +6 -2
  89. package/dist/oauth/figma.cjs +39 -39
  90. package/dist/oauth/figma.d.ts +7 -7
  91. package/dist/oauth/figma.js +6 -2
  92. package/dist/oauth/github.cjs +31 -31
  93. package/dist/oauth/github.d.ts +7 -7
  94. package/dist/oauth/github.js +6 -2
  95. package/dist/oauth/gitlab.cjs +39 -39
  96. package/dist/oauth/gitlab.d.ts +7 -7
  97. package/dist/oauth/gitlab.js +6 -2
  98. package/dist/oauth/index.cjs +350 -180
  99. package/dist/oauth/index.d.ts +7 -26
  100. package/dist/oauth/index.js +51 -9
  101. package/dist/oauth/mailchimp.cjs +46 -0
  102. package/dist/oauth/mailchimp.d.ts +7 -0
  103. package/dist/oauth/mailchimp.js +6 -0
  104. package/dist/oauth/pinterest.cjs +46 -0
  105. package/dist/oauth/pinterest.d.ts +7 -0
  106. package/dist/oauth/pinterest.js +6 -0
  107. package/dist/oauth/spotify.cjs +39 -39
  108. package/dist/oauth/spotify.d.ts +7 -7
  109. package/dist/oauth/spotify.js +6 -2
  110. package/dist/oauth/strava.cjs +46 -0
  111. package/dist/oauth/strava.d.ts +7 -0
  112. package/dist/oauth/strava.js +6 -0
  113. package/dist/oauth/x.cjs +39 -39
  114. package/dist/oauth/x.d.ts +7 -7
  115. package/dist/oauth/x.js +6 -2
  116. package/dist/request.cjs +38 -0
  117. package/dist/request.d.ts +13 -0
  118. package/dist/request.js +6 -0
  119. package/dist/schemas.cjs +103 -97
  120. package/dist/schemas.d.ts +96 -126
  121. package/dist/schemas.js +20 -18
  122. package/dist/secure.cjs +106 -101
  123. package/dist/secure.d.ts +17 -17
  124. package/dist/secure.js +19 -4
  125. package/dist/utils.cjs +197 -135
  126. package/dist/utils.d.ts +20 -26
  127. package/dist/utils.js +25 -21
  128. package/package.json +8 -7
  129. package/dist/chunk-256KIVJL.js +0 -110
  130. package/dist/chunk-6SM22VVJ.js +0 -15
  131. package/dist/chunk-CAKJT3KS.js +0 -77
  132. package/dist/chunk-EBPE35JT.js +0 -29
  133. package/dist/chunk-FJUDBLCP.js +0 -52
  134. package/dist/chunk-GZU3RBTB.js +0 -51
  135. package/dist/chunk-HGJ4TXY4.js +0 -105
  136. package/dist/chunk-HMRKN75I.js +0 -74
  137. package/dist/chunk-JAPMIE6S.js +0 -8
  138. package/dist/chunk-LLR722CL.js +0 -75
  139. package/dist/chunk-RLT4RFKV.js +0 -36
  140. package/dist/chunk-SJPDVKUS.js +0 -93
  141. package/dist/chunk-SMQO5WD7.js +0 -20
  142. package/dist/chunk-UJJ7R56J.js +0 -42
  143. package/dist/chunk-UTDLUEEG.js +0 -25
  144. package/dist/chunk-VFTYH33W.js +0 -44
  145. package/dist/chunk-XXJKNKGQ.js +0 -27
  146. package/dist/chunk-ZV4BH47P.js +0 -156
  147. package/dist/error.cjs +0 -88
  148. package/dist/error.d.ts +0 -62
  149. package/dist/error.js +0 -9
  150. package/dist/index-DpfbvTZ_.d.ts +0 -597
  151. package/dist/response.cjs +0 -34
  152. package/dist/response.d.ts +0 -10
  153. package/dist/response.js +0 -2
@@ -1,77 +0,0 @@
1
- import { isValidURL } from "./chunk-6SM22VVJ.js"
2
- import { equals, getNormalizedOriginPath, sanitizeURL, toCastCase } from "./chunk-256KIVJL.js"
3
- import { AuthError, ERROR_RESPONSE, InvalidRedirectToError, isAuthError } from "./chunk-FJUDBLCP.js"
4
- import { OAuthAuthorization } from "./chunk-HMRKN75I.js"
5
-
6
- // src/actions/signIn/authorization.ts
7
- var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod) => {
8
- const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod })
9
- if (!parsed.success) {
10
- throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.SERVER_ERROR, "Invalid OAuth configuration")
11
- }
12
- const { authorizeURL, ...options } = parsed.data
13
- const { userInfo, accessToken, clientSecret, ...required } = options
14
- const searchParams = new URLSearchParams(toCastCase(required))
15
- return `${authorizeURL}?${searchParams}`
16
- }
17
- var getOriginURL = (request, trustedProxyHeaders) => {
18
- const headers = request.headers
19
- if (trustedProxyHeaders) {
20
- const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http"
21
- const host =
22
- headers.get("X-Forwarded-Host") ??
23
- headers.get("Host") ??
24
- headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ??
25
- null
26
- return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`)
27
- } else {
28
- return new URL(getNormalizedOriginPath(request.url))
29
- }
30
- }
31
- var createRedirectURI = (request, oauth, basePath, trustedProxyHeaders) => {
32
- const url = getOriginURL(request, trustedProxyHeaders)
33
- return `${url.origin}${basePath}/callback/${oauth}`
34
- }
35
- var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
36
- try {
37
- const headers = request.headers
38
- const origin = headers.get("Origin")
39
- const referer = headers.get("Referer")
40
- let hostedURL = getOriginURL(request, trustedProxyHeaders)
41
- if (redirectTo) {
42
- if (redirectTo.startsWith("/")) {
43
- return sanitizeURL(redirectTo)
44
- }
45
- const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)))
46
- if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
47
- throw new InvalidRedirectToError()
48
- }
49
- return sanitizeURL(redirectToURL.pathname)
50
- }
51
- if (referer) {
52
- const refererURL = new URL(sanitizeURL(referer))
53
- if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
54
- throw new AuthError(
55
- ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST,
56
- "The referer of the request does not match the hosted origin."
57
- )
58
- }
59
- return sanitizeURL(refererURL.pathname)
60
- }
61
- if (origin) {
62
- const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)))
63
- if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
64
- throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).")
65
- }
66
- return sanitizeURL(originURL.pathname)
67
- }
68
- return "/"
69
- } catch (error) {
70
- if (isAuthError(error)) {
71
- throw error
72
- }
73
- throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).")
74
- }
75
- }
76
-
77
- export { createAuthorizationURL, getOriginURL, createRedirectURI, createRedirectTo }
@@ -1,29 +0,0 @@
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 { discord }
@@ -1,52 +0,0 @@
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 { AuthError, InvalidCsrfTokenError, InvalidRedirectToError, isAuthError, throwAuthError, ERROR_RESPONSE }
@@ -1,51 +0,0 @@
1
- import { equals } from "./chunk-256KIVJL.js"
2
- import { InvalidCsrfTokenError } from "./chunk-FJUDBLCP.js"
3
-
4
- // src/secure.ts
5
- import crypto from "crypto"
6
- var generateSecure = (length = 32) => {
7
- return crypto.randomBytes(length).toString("base64url")
8
- }
9
- var createHash = (data, base = "hex") => {
10
- return crypto.createHash("sha256").update(data).digest().toString(base)
11
- }
12
- var createPKCE = async (verifier) => {
13
- const codeVerifier = verifier ?? generateSecure(86)
14
- const codeChallenge = createHash(codeVerifier, "base64url")
15
- return { codeVerifier, codeChallenge, method: "S256" }
16
- }
17
- var createCSRF = async (jose, csrfCookie) => {
18
- try {
19
- const token = generateSecure(32)
20
- if (csrfCookie) {
21
- await jose.verifyJWS(csrfCookie)
22
- return csrfCookie
23
- }
24
- return jose.signJWS({ token })
25
- } catch {
26
- const token = generateSecure(32)
27
- return jose.signJWS({ token })
28
- }
29
- }
30
- var verifyCSRF = async (jose, cookie, header) => {
31
- try {
32
- const { token: cookieToken } = await jose.verifyJWS(cookie)
33
- const { token: headerToken } = await jose.verifyJWS(header)
34
- const cookieBuffer = Buffer.from(cookieToken)
35
- const headerBuffer = Buffer.from(headerToken)
36
- if (!equals(headerBuffer.length, cookieBuffer.length)) {
37
- throw new InvalidCsrfTokenError()
38
- }
39
- if (!crypto.timingSafeEqual(cookieBuffer, headerBuffer)) {
40
- throw new InvalidCsrfTokenError()
41
- }
42
- return true
43
- } catch {
44
- throw new InvalidCsrfTokenError()
45
- }
46
- }
47
- var createDerivedSalt = (secret) => {
48
- return crypto.createHash("sha256").update(secret).update("aura-auth-salt").digest("hex")
49
- }
50
-
51
- export { generateSecure, createHash, createPKCE, createCSRF, verifyCSRF, createDerivedSalt }
@@ -1,105 +0,0 @@
1
- import { getUserInfo } from "./chunk-RLT4RFKV.js"
2
- import { createAccessToken } from "./chunk-UJJ7R56J.js"
3
- import { createSessionCookie, expireCookie, getCookie, secureCookieOptions, setCookie } from "./chunk-ZV4BH47P.js"
4
- import { cacheControl } from "./chunk-STHEPPUZ.js"
5
- import { createCSRF } from "./chunk-GZU3RBTB.js"
6
- import { equals, isValidRelativePath, sanitizeURL } from "./chunk-256KIVJL.js"
7
- import { AuthError, ERROR_RESPONSE, isAuthError } from "./chunk-FJUDBLCP.js"
8
- import { AuraResponse } from "./chunk-JAPMIE6S.js"
9
- import { OAuthAuthorizationErrorResponse, OAuthAuthorizationResponse } from "./chunk-HMRKN75I.js"
10
-
11
- // src/actions/callback/callback.ts
12
- import z from "zod"
13
- import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router"
14
- var callbackConfig = (oauth) => {
15
- return createEndpointConfig("/callback/:oauth", {
16
- schemas: {
17
- searchParams: OAuthAuthorizationResponse,
18
- params: z.object({
19
- oauth: z.enum(Object.keys(oauth)),
20
- }),
21
- },
22
- middlewares: [
23
- (ctx) => {
24
- const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams)
25
- if (response.success) {
26
- const { error, error_description } = response.data
27
- throw new AuthError(error, error_description ?? "OAuth Authorization Error")
28
- }
29
- return ctx
30
- },
31
- ],
32
- })
33
- }
34
- var callbackAction = (oauth) => {
35
- return createEndpoint(
36
- "GET",
37
- "/callback/:oauth",
38
- async (ctx) => {
39
- const {
40
- request,
41
- params: { oauth: oauth2 },
42
- searchParams: { code, state },
43
- context: { oauth: providers, cookies, jose, trustedProxyHeaders },
44
- } = ctx
45
- try {
46
- const oauthConfig = providers[oauth2]
47
- const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders)
48
- const cookieState = getCookie(request, "state", cookieOptions)
49
- const cookieRedirectTo = getCookie(request, "redirect_to", cookieOptions)
50
- const cookieRedirectURI = getCookie(request, "redirect_uri", cookieOptions)
51
- const codeVerifier = getCookie(request, "code_verifier", cookieOptions)
52
- if (!equals(cookieState, state)) {
53
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Mismatching state")
54
- }
55
- const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier)
56
- const sanitized = sanitizeURL(cookieRedirectTo)
57
- if (!isValidRelativePath(sanitized)) {
58
- throw new AuthError(
59
- ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST,
60
- "Invalid redirect path. Potential open redirect attack detected."
61
- )
62
- }
63
- const headers = new Headers(cacheControl)
64
- headers.set("Location", sanitized)
65
- const userInfo = await getUserInfo(oauthConfig, accessToken.access_token)
66
- const sessionCookie = await createSessionCookie(userInfo, cookieOptions, jose)
67
- const csrfToken = await createCSRF(jose)
68
- const csrfCookie = setCookie(
69
- "csrfToken",
70
- csrfToken,
71
- secureCookieOptions(
72
- request,
73
- {
74
- ...cookies,
75
- strategy: "host",
76
- },
77
- trustedProxyHeaders
78
- )
79
- )
80
- headers.set("Set-Cookie", sessionCookie)
81
- headers.append("Set-Cookie", expireCookie("state", cookieOptions))
82
- headers.append("Set-Cookie", expireCookie("redirect_uri", cookieOptions))
83
- headers.append("Set-Cookie", expireCookie("redirect_to", cookieOptions))
84
- headers.append("Set-Cookie", expireCookie("code_verifier", cookieOptions))
85
- headers.append("Set-Cookie", csrfCookie)
86
- return Response.json({ oauth: oauth2 }, { status: 302, headers })
87
- } catch (error) {
88
- if (isAuthError(error)) {
89
- const { type, message } = error
90
- return AuraResponse.json({ error: type, error_description: message }, { status: statusCode.BAD_REQUEST })
91
- }
92
- return AuraResponse.json(
93
- {
94
- error: ERROR_RESPONSE.ACCESS_TOKEN.INVALID_CLIENT,
95
- error_description: "An unexpected error occurred",
96
- },
97
- { status: statusCode.INTERNAL_SERVER_ERROR }
98
- )
99
- }
100
- },
101
- callbackConfig(oauth)
102
- )
103
- }
104
-
105
- export { callbackAction }
@@ -1,74 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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 { AuraResponse }
@@ -1,75 +0,0 @@
1
- import { createAuthorizationURL, createRedirectTo, createRedirectURI } from "./chunk-CAKJT3KS.js"
2
- import { oauthCookie, secureCookieOptions, setCookie } from "./chunk-ZV4BH47P.js"
3
- import { createPKCE, generateSecure } from "./chunk-GZU3RBTB.js"
4
- import { ERROR_RESPONSE, isAuthError } from "./chunk-FJUDBLCP.js"
5
- import { AuraResponse } from "./chunk-JAPMIE6S.js"
6
-
7
- // src/actions/signIn/signIn.ts
8
- import z from "zod"
9
- import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router"
10
- var signInConfig = (oauth) => {
11
- return createEndpointConfig("/signIn/:oauth", {
12
- schemas: {
13
- params: z.object({
14
- oauth: z.enum(Object.keys(oauth)),
15
- redirectTo: z.string().optional(),
16
- }),
17
- },
18
- })
19
- }
20
- var signInAction = (oauth) => {
21
- return createEndpoint(
22
- "GET",
23
- "/signIn/:oauth",
24
- async (ctx) => {
25
- const {
26
- request,
27
- params: { oauth: oauth2, redirectTo },
28
- context: { oauth: providers, cookies, trustedProxyHeaders, basePath },
29
- } = ctx
30
- try {
31
- const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders)
32
- const state = generateSecure()
33
- const redirectURI = createRedirectURI(request, oauth2, basePath, trustedProxyHeaders)
34
- const stateCookie = setCookie("state", state, oauthCookie(cookieOptions))
35
- const redirectURICookie = setCookie("redirect_uri", redirectURI, oauthCookie(cookieOptions))
36
- const redirectToCookie = setCookie(
37
- "redirect_to",
38
- createRedirectTo(request, redirectTo, trustedProxyHeaders),
39
- oauthCookie(cookieOptions)
40
- )
41
- const { codeVerifier, codeChallenge, method } = await createPKCE()
42
- const codeVerifierCookie = setCookie("code_verifier", codeVerifier, oauthCookie(cookieOptions))
43
- const authorization = createAuthorizationURL(providers[oauth2], redirectURI, state, codeChallenge, method)
44
- const headers = new Headers()
45
- headers.set("Location", authorization)
46
- headers.append("Set-Cookie", stateCookie)
47
- headers.append("Set-Cookie", redirectURICookie)
48
- headers.append("Set-Cookie", redirectToCookie)
49
- headers.append("Set-Cookie", codeVerifierCookie)
50
- return Response.json(
51
- { oauth: oauth2 },
52
- {
53
- status: 302,
54
- headers,
55
- }
56
- )
57
- } catch (error) {
58
- if (isAuthError(error)) {
59
- const { type, message } = error
60
- return AuraResponse.json({ error: type, error_description: message }, { status: statusCode.BAD_REQUEST })
61
- }
62
- return AuraResponse.json(
63
- {
64
- error: ERROR_RESPONSE.AUTHORIZATION.SERVER_ERROR,
65
- error_description: "An unexpected error occurred",
66
- },
67
- { status: statusCode.INTERNAL_SERVER_ERROR }
68
- )
69
- }
70
- },
71
- signInConfig(oauth)
72
- )
73
- }
74
-
75
- export { signInAction }
@@ -1,36 +0,0 @@
1
- import { generateSecure } from "./chunk-GZU3RBTB.js"
2
- import { AuthError, throwAuthError } from "./chunk-FJUDBLCP.js"
3
- import { OAuthErrorResponse } from "./chunk-HMRKN75I.js"
4
-
5
- // src/actions/callback/userinfo.ts
6
- var getDefaultUserInfo = (profile) => {
7
- const sub = generateSecure(16)
8
- return {
9
- sub: profile?.id ?? profile?.sub ?? sub,
10
- email: profile?.email,
11
- name: profile?.name ?? profile?.username ?? profile?.nickname,
12
- image: profile?.image ?? profile?.picture,
13
- }
14
- }
15
- var getUserInfo = async (oauthConfig, accessToken) => {
16
- const userinfoEndpoint = oauthConfig.userInfo
17
- try {
18
- const response = await fetch(userinfoEndpoint, {
19
- method: "GET",
20
- headers: {
21
- Accept: "application/json",
22
- Authorization: `Bearer ${accessToken}`,
23
- },
24
- })
25
- const json = await response.json()
26
- const { success, data } = OAuthErrorResponse.safeParse(json)
27
- if (success) {
28
- throw new AuthError(data.error, data?.error_description ?? "An error occurred while fetching user information.")
29
- }
30
- return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json)
31
- } catch (error) {
32
- throw throwAuthError(error, "Failed to retrieve userinfo")
33
- }
34
- }
35
-
36
- export { getUserInfo }
@@ -1,93 +0,0 @@
1
- import { createRedirectTo } from "./chunk-CAKJT3KS.js"
2
- import { expireCookie, getCookie, secureCookieOptions } from "./chunk-ZV4BH47P.js"
3
- import { cacheControl } from "./chunk-STHEPPUZ.js"
4
- import { verifyCSRF } from "./chunk-GZU3RBTB.js"
5
- import { getNormalizedOriginPath } from "./chunk-256KIVJL.js"
6
- import { InvalidCsrfTokenError, InvalidRedirectToError } from "./chunk-FJUDBLCP.js"
7
- import { AuraResponse } from "./chunk-JAPMIE6S.js"
8
-
9
- // src/actions/signOut/signOut.ts
10
- import z from "zod"
11
- import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router"
12
- var config = createEndpointConfig({
13
- schemas: {
14
- searchParams: z.object({
15
- token_type_hint: z.literal("session_token"),
16
- redirectTo: z.string().optional(),
17
- }),
18
- },
19
- })
20
- var signOutAction = createEndpoint(
21
- "POST",
22
- "/signOut",
23
- async (ctx) => {
24
- const {
25
- request,
26
- headers,
27
- searchParams: { redirectTo },
28
- context: { cookies, jose, trustedProxyHeaders },
29
- } = ctx
30
- try {
31
- const cookiesOptions = secureCookieOptions(request, cookies, trustedProxyHeaders)
32
- const session = getCookie(request, "sessionToken", cookiesOptions)
33
- const csrfToken = getCookie(request, "csrfToken", {
34
- ...cookiesOptions,
35
- prefix: cookiesOptions.secure ? "__Host-" : "",
36
- })
37
- const header = headers.get("X-CSRF-Token")
38
- if (!header || !session || !csrfToken) {
39
- throw new Error("Missing CSRF token or session token")
40
- }
41
- await verifyCSRF(jose, csrfToken, header)
42
- await jose.decodeJWT(session)
43
- const normalizedOriginPath = getNormalizedOriginPath(request.url)
44
- const location = createRedirectTo(
45
- new Request(normalizedOriginPath, {
46
- headers,
47
- }),
48
- redirectTo
49
- )
50
- const responseHeaders = new Headers(cacheControl)
51
- responseHeaders.append("Set-Cookie", expireCookie("sessionToken", cookiesOptions))
52
- responseHeaders.append(
53
- "Set-Cookie",
54
- expireCookie("csrfToken", { ...cookiesOptions, prefix: cookiesOptions.secure ? "__Host-" : "" })
55
- )
56
- responseHeaders.append("Location", location)
57
- return Response.json(
58
- { message: "Signed out successfully" },
59
- { status: statusCode.ACCEPTED, headers: responseHeaders }
60
- )
61
- } catch (error) {
62
- if (error instanceof InvalidCsrfTokenError) {
63
- return AuraResponse.json(
64
- {
65
- error: "invalid_csrf_token",
66
- error_description: "The provided CSRF token is invalid or has expired",
67
- },
68
- { status: statusCode.UNAUTHORIZED }
69
- )
70
- }
71
- if (error instanceof InvalidRedirectToError) {
72
- const { type, message } = error
73
- return AuraResponse.json(
74
- {
75
- error: type,
76
- error_description: message,
77
- },
78
- { status: statusCode.BAD_REQUEST }
79
- )
80
- }
81
- return AuraResponse.json(
82
- {
83
- error: "invalid_session_token",
84
- error_description: "The provided sessionToken is invalid or has already expired",
85
- },
86
- { status: statusCode.UNAUTHORIZED }
87
- )
88
- }
89
- },
90
- config
91
- )
92
-
93
- export { signOutAction }
@@ -1,20 +0,0 @@
1
- import { getCookie, secureCookieOptions, setCookie } from "./chunk-ZV4BH47P.js"
2
- import { cacheControl } from "./chunk-STHEPPUZ.js"
3
- import { createCSRF } from "./chunk-GZU3RBTB.js"
4
-
5
- // src/actions/csrfToken/csrfToken.ts
6
- import { createEndpoint } from "@aura-stack/router"
7
- var csrfTokenAction = createEndpoint("GET", "/csrfToken", async (ctx) => {
8
- const {
9
- request,
10
- context: { cookies, jose, trustedProxyHeaders },
11
- } = ctx
12
- const cookieOptions = secureCookieOptions(request, { ...cookies, strategy: "host" }, trustedProxyHeaders)
13
- const existingCSRFToken = getCookie(request, "csrfToken", cookieOptions, true)
14
- const csrfToken = await createCSRF(jose, existingCSRFToken)
15
- const headers = new Headers(cacheControl)
16
- headers.set("Set-Cookie", setCookie("csrfToken", csrfToken, cookieOptions))
17
- return Response.json({ csrfToken }, { headers })
18
- })
19
-
20
- export { csrfTokenAction }
@@ -1,42 +0,0 @@
1
- import { AuthError, ERROR_RESPONSE, throwAuthError } from "./chunk-FJUDBLCP.js"
2
- import { OAuthAccessToken, OAuthAccessTokenErrorResponse, OAuthAccessTokenResponse } from "./chunk-HMRKN75I.js"
3
-
4
- // src/actions/callback/access-token.ts
5
- var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) => {
6
- const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier })
7
- if (!parsed.success) {
8
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Invalid OAuth configuration")
9
- }
10
- const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data
11
- try {
12
- const response = await fetch(accessToken, {
13
- method: "POST",
14
- headers: {
15
- Accept: "application/json",
16
- "Content-Type": "application/x-www-form-urlencoded",
17
- },
18
- body: new URLSearchParams({
19
- client_id: clientId,
20
- client_secret: clientSecret,
21
- code: codeParsed,
22
- redirect_uri: redirectParsed,
23
- grant_type: "authorization_code",
24
- code_verifier: codeVerifier,
25
- }).toString(),
26
- })
27
- const json = await response.json()
28
- const token = OAuthAccessTokenResponse.safeParse(json)
29
- if (!token.success) {
30
- const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json)
31
- if (!success) {
32
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_GRANT, "Invalid access token response format")
33
- }
34
- throw new AuthError(data.error, data?.error_description ?? "Failed to retrieve access token")
35
- }
36
- return token.data
37
- } catch (error) {
38
- throw throwAuthError(error, "Failed to create access token")
39
- }
40
- }
41
-
42
- export { createAccessToken }