@aura-stack/auth 0.4.0-rc.4 → 0.4.0

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 (143) hide show
  1. package/dist/@types/index.d.ts +4 -3
  2. package/dist/@types/router.d.cjs +0 -17
  3. package/dist/@types/router.d.d.ts +3 -2
  4. package/dist/@types/router.d.js +0 -1
  5. package/dist/actions/callback/access-token.cjs +40 -25
  6. package/dist/actions/callback/access-token.d.ts +4 -3
  7. package/dist/actions/callback/access-token.js +3 -4
  8. package/dist/actions/callback/callback.cjs +287 -77
  9. package/dist/actions/callback/callback.d.ts +5 -26
  10. package/dist/actions/callback/callback.js +13 -10
  11. package/dist/actions/callback/userinfo.cjs +68 -7
  12. package/dist/actions/callback/userinfo.d.ts +4 -3
  13. package/dist/actions/callback/userinfo.js +8 -6
  14. package/dist/actions/csrfToken/csrfToken.cjs +63 -4
  15. package/dist/actions/csrfToken/csrfToken.d.ts +1 -3
  16. package/dist/actions/csrfToken/csrfToken.js +8 -6
  17. package/dist/actions/index.cjs +400 -175
  18. package/dist/actions/index.d.ts +3 -2
  19. package/dist/actions/index.js +21 -19
  20. package/dist/actions/session/session.cjs +40 -11
  21. package/dist/actions/session/session.d.ts +1 -3
  22. package/dist/actions/session/session.js +4 -4
  23. package/dist/actions/signIn/authorization.cjs +171 -132
  24. package/dist/actions/signIn/authorization.d.ts +21 -11
  25. package/dist/actions/signIn/authorization.js +8 -6
  26. package/dist/actions/signIn/signIn.cjs +220 -113
  27. package/dist/actions/signIn/signIn.d.ts +5 -25
  28. package/dist/actions/signIn/signIn.js +9 -7
  29. package/dist/actions/signOut/signOut.cjs +268 -119
  30. package/dist/actions/signOut/signOut.d.ts +1 -9
  31. package/dist/actions/signOut/signOut.js +10 -8
  32. package/dist/assert.cjs +117 -5
  33. package/dist/assert.d.ts +22 -3
  34. package/dist/assert.js +17 -3
  35. package/dist/chunk-4EKY7655.js +123 -0
  36. package/dist/chunk-4MYWAOLG.js +31 -0
  37. package/dist/chunk-4YHJ4IEQ.js +25 -0
  38. package/dist/chunk-54CZPKR4.js +25 -0
  39. package/dist/chunk-5LZ7TOM3.js +25 -0
  40. package/dist/{chunk-W6LG7BFW.js → chunk-5W4BRQYG.js} +24 -20
  41. package/dist/chunk-6MXFPFR3.js +143 -0
  42. package/dist/{chunk-3EUWD5BB.js → chunk-7QF22LHP.js} +13 -9
  43. package/dist/chunk-ALG3GIV4.js +95 -0
  44. package/dist/chunk-E6G5YCI6.js +25 -0
  45. package/dist/chunk-EBAMFRB7.js +34 -0
  46. package/dist/chunk-EEE7UM5T.js +25 -0
  47. package/dist/{chunk-TLE4PXY3.js → chunk-FRJFWTOY.js} +38 -7
  48. package/dist/chunk-FW4W3REU.js +25 -0
  49. package/dist/{chunk-HT4YLL7N.js → chunk-ICAZ4OVS.js} +10 -8
  50. package/dist/chunk-IPKO6UQN.js +25 -0
  51. package/dist/{chunk-YRCB5FLE.js → chunk-KJBAQZX2.js} +13 -0
  52. package/dist/chunk-KMMAZFSJ.js +25 -0
  53. package/dist/chunk-LDU7A2JE.js +25 -0
  54. package/dist/{chunk-N2APGLXA.js → chunk-NUDITUKX.js} +18 -16
  55. package/dist/chunk-OVHNRULD.js +33 -0
  56. package/dist/{chunk-JVFTCTTE.js → chunk-PHFH2MGS.js} +12 -9
  57. package/dist/chunk-QQVSRXGX.js +149 -0
  58. package/dist/chunk-TM5IPSNF.js +113 -0
  59. package/dist/{chunk-GA2SMTJO.js → chunk-TZB6MUXN.js} +33 -13
  60. package/dist/chunk-VNCNJKS2.js +267 -0
  61. package/dist/{chunk-IVET23KF.js → chunk-XGLBNXL4.js} +31 -14
  62. package/dist/chunk-XUP6KKNG.js +106 -0
  63. package/dist/cookie.cjs +24 -20
  64. package/dist/cookie.d.ts +4 -3
  65. package/dist/cookie.js +1 -1
  66. package/dist/env.cjs +56 -0
  67. package/dist/env.d.ts +7 -0
  68. package/dist/env.js +6 -0
  69. package/dist/errors.d.ts +4 -3
  70. package/dist/headers.cjs +28 -2
  71. package/dist/headers.d.ts +25 -1
  72. package/dist/headers.js +9 -3
  73. package/dist/{index-DkaLJFn8.d.ts → index-CSyIJmCM.d.ts} +373 -45
  74. package/dist/index.cjs +1128 -483
  75. package/dist/index.d.ts +6 -10
  76. package/dist/index.js +83 -42
  77. package/dist/jose.cjs +62 -25
  78. package/dist/jose.d.ts +7 -5
  79. package/dist/jose.js +8 -6
  80. package/dist/logger.cjs +292 -0
  81. package/dist/logger.d.ts +8 -0
  82. package/dist/logger.js +8 -0
  83. package/dist/oauth/bitbucket.cjs +19 -15
  84. package/dist/oauth/bitbucket.d.ts +3 -2
  85. package/dist/oauth/bitbucket.js +1 -1
  86. package/dist/oauth/discord.cjs +27 -24
  87. package/dist/oauth/discord.d.ts +3 -2
  88. package/dist/oauth/discord.js +1 -1
  89. package/dist/oauth/figma.cjs +19 -16
  90. package/dist/oauth/figma.d.ts +3 -2
  91. package/dist/oauth/figma.js +1 -1
  92. package/dist/oauth/github.cjs +19 -8
  93. package/dist/oauth/github.d.ts +3 -2
  94. package/dist/oauth/github.js +1 -1
  95. package/dist/oauth/gitlab.cjs +19 -16
  96. package/dist/oauth/gitlab.d.ts +3 -2
  97. package/dist/oauth/gitlab.js +1 -1
  98. package/dist/oauth/index.cjs +266 -166
  99. package/dist/oauth/index.d.ts +3 -2
  100. package/dist/oauth/index.js +22 -21
  101. package/dist/oauth/mailchimp.cjs +19 -16
  102. package/dist/oauth/mailchimp.d.ts +3 -2
  103. package/dist/oauth/mailchimp.js +1 -1
  104. package/dist/oauth/pinterest.cjs +19 -16
  105. package/dist/oauth/pinterest.d.ts +3 -2
  106. package/dist/oauth/pinterest.js +1 -1
  107. package/dist/oauth/spotify.cjs +19 -16
  108. package/dist/oauth/spotify.d.ts +3 -2
  109. package/dist/oauth/spotify.js +1 -1
  110. package/dist/oauth/strava.cjs +19 -16
  111. package/dist/oauth/strava.d.ts +3 -2
  112. package/dist/oauth/strava.js +1 -1
  113. package/dist/oauth/x.cjs +19 -16
  114. package/dist/oauth/x.d.ts +3 -2
  115. package/dist/oauth/x.js +1 -1
  116. package/dist/schemas.cjs +16 -2
  117. package/dist/schemas.d.ts +17 -1
  118. package/dist/schemas.js +5 -3
  119. package/dist/secure.cjs +58 -16
  120. package/dist/secure.d.ts +4 -10
  121. package/dist/secure.js +5 -5
  122. package/dist/utils.cjs +94 -87
  123. package/dist/utils.d.ts +9 -39
  124. package/dist/utils.js +11 -9
  125. package/package.json +3 -4
  126. package/dist/chunk-42XB3YCW.js +0 -22
  127. package/dist/chunk-6R2YZ4AC.js +0 -22
  128. package/dist/chunk-A3N4PVAT.js +0 -70
  129. package/dist/chunk-B737EUJV.js +0 -22
  130. package/dist/chunk-CXLATHS5.js +0 -143
  131. package/dist/chunk-DIVDFNAP.js +0 -0
  132. package/dist/chunk-E3OXBRYF.js +0 -22
  133. package/dist/chunk-EIL2FPSS.js +0 -22
  134. package/dist/chunk-EMKJA2GJ.js +0 -89
  135. package/dist/chunk-FIPU4MLT.js +0 -21
  136. package/dist/chunk-FKRDCWBF.js +0 -22
  137. package/dist/chunk-HP34YGGJ.js +0 -22
  138. package/dist/chunk-IKHPGFCW.js +0 -14
  139. package/dist/chunk-IUYZQTJV.js +0 -30
  140. package/dist/chunk-KRNOMBXQ.js +0 -22
  141. package/dist/chunk-KSWLO5ZU.js +0 -102
  142. package/dist/chunk-N4SX7TZT.js +0 -96
  143. package/dist/chunk-STHEPPUZ.js +0 -11
@@ -0,0 +1,33 @@
1
+ // src/oauth/discord.ts
2
+ var discord = (options) => {
3
+ return {
4
+ id: "discord",
5
+ name: "Discord",
6
+ authorizeURL: "https://discord.com/oauth2/authorize",
7
+ accessToken: "https://discord.com/api/oauth2/token",
8
+ userInfo: "https://discord.com/api/users/@me",
9
+ scope: "identify email",
10
+ responseType: "code",
11
+ profile(profile) {
12
+ let image = "";
13
+ if (profile.avatar === null) {
14
+ const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
15
+ image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
16
+ } else {
17
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
18
+ image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
19
+ }
20
+ return {
21
+ sub: profile.id,
22
+ name: profile.global_name ?? profile.username,
23
+ email: profile.email ?? "",
24
+ image
25
+ };
26
+ },
27
+ ...options
28
+ };
29
+ };
30
+
31
+ export {
32
+ discord
33
+ };
@@ -1,29 +1,32 @@
1
+ import {
2
+ getErrorName,
3
+ toISOString
4
+ } from "./chunk-QQVSRXGX.js";
1
5
  import {
2
6
  expiredCookieAttributes,
3
7
  getCookie
4
- } from "./chunk-W6LG7BFW.js";
8
+ } from "./chunk-5W4BRQYG.js";
5
9
  import {
6
- cacheControl
7
- } from "./chunk-STHEPPUZ.js";
8
- import {
9
- toISOString
10
- } from "./chunk-CXLATHS5.js";
10
+ secureApiHeaders
11
+ } from "./chunk-EBAMFRB7.js";
11
12
 
12
13
  // src/actions/session/session.ts
13
14
  import { createEndpoint, HeadersBuilder } from "@aura-stack/router";
14
15
  var sessionAction = createEndpoint("GET", "/session", async (ctx) => {
15
16
  const {
16
17
  request,
17
- context: { jose, cookies }
18
+ context: { jose, cookies, logger }
18
19
  } = ctx;
19
20
  try {
20
21
  const session = getCookie(request, cookies.sessionToken.name);
21
22
  const decoded = await jose.decodeJWT(session);
23
+ logger?.log("AUTH_SESSION_VALID");
22
24
  const { exp, iat, jti, nbf, ...user } = decoded;
23
- const headers = new Headers(cacheControl);
25
+ const headers = new Headers(secureApiHeaders);
24
26
  return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
25
27
  } catch (error) {
26
- const headers = new HeadersBuilder(cacheControl).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
28
+ logger?.log("AUTH_SESSION_INVALID", { structuredData: { error_type: getErrorName(error) } });
29
+ const headers = new HeadersBuilder(secureApiHeaders).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
27
30
  return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
28
31
  }
29
32
  });
@@ -0,0 +1,149 @@
1
+ import {
2
+ isAuthInternalError,
3
+ isAuthSecurityError,
4
+ isOAuthProtocolError
5
+ } from "./chunk-RRLIF4PQ.js";
6
+
7
+ // src/utils.ts
8
+ import { isInvalidZodSchemaError, isRouterError } from "@aura-stack/router";
9
+ var toSnakeCase = (str) => {
10
+ return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase().replace(/^_+/, "");
11
+ };
12
+ var toUpperCase = (str) => {
13
+ return str.toUpperCase();
14
+ };
15
+ var toCastCase = (obj, type = "snake") => {
16
+ return Object.entries(obj).reduce((previous, [key, value]) => {
17
+ const newKey = type === "snake" ? toSnakeCase(key) : toUpperCase(key);
18
+ return { ...previous, [newKey]: value };
19
+ }, {});
20
+ };
21
+ var equals = (a, b) => {
22
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
23
+ return a === b;
24
+ };
25
+ var createErrorHandler = (logger) => {
26
+ return (error) => {
27
+ if (isRouterError(error)) {
28
+ const { message, status, statusText } = error;
29
+ logger?.log("ROUTER_INTERNAL_ERROR");
30
+ return Response.json({ type: "ROUTER_ERROR", code: "ROUTER_INTERNAL_ERROR", message }, { status, statusText });
31
+ }
32
+ if (isInvalidZodSchemaError(error)) {
33
+ logger?.log("INVALID_REQUEST");
34
+ return Response.json({ type: "ROUTER_ERROR", code: "INVALID_REQUEST", message: error.errors }, { status: 422 });
35
+ }
36
+ if (isOAuthProtocolError(error)) {
37
+ const { error: errorCode, message, type, errorURI } = error;
38
+ logger?.log("OAUTH_PROTOCOL_ERROR", {
39
+ structuredData: {
40
+ error: errorCode,
41
+ error_description: message,
42
+ error_uri: errorURI ?? ""
43
+ }
44
+ });
45
+ return Response.json(
46
+ {
47
+ type,
48
+ message
49
+ },
50
+ { status: 400 }
51
+ );
52
+ }
53
+ if (isAuthInternalError(error)) {
54
+ const { type, code, message } = error;
55
+ logger?.log("INVALID_OAUTH_CONFIGURATION", {
56
+ structuredData: {
57
+ error: code,
58
+ error_description: message
59
+ }
60
+ });
61
+ return Response.json(
62
+ {
63
+ type,
64
+ message
65
+ },
66
+ { status: 400 }
67
+ );
68
+ }
69
+ if (isAuthSecurityError(error)) {
70
+ const { type, code, message } = error;
71
+ logger?.log("INVALID_OAUTH_CONFIGURATION", {
72
+ structuredData: {
73
+ error: code,
74
+ error_description: message
75
+ }
76
+ });
77
+ return Response.json(
78
+ {
79
+ type,
80
+ code,
81
+ message
82
+ },
83
+ { status: 400 }
84
+ );
85
+ }
86
+ logger?.log("SERVER_ERROR");
87
+ return Response.json(
88
+ { type: "SERVER_ERROR", code: "SERVER_ERROR", message: "An unexpected error occurred" },
89
+ { status: 500 }
90
+ );
91
+ };
92
+ };
93
+ var getBaseURL = (request) => {
94
+ const url = new URL(request.url);
95
+ return `${url.origin}${url.pathname}`;
96
+ };
97
+ var toISOString = (date) => {
98
+ return new Date(date).toISOString();
99
+ };
100
+ var useSecureCookies = (request, trustedProxyHeaders) => {
101
+ return trustedProxyHeaders ? request.url.startsWith("https://") || request.headers.get("X-Forwarded-Proto") === "https" || (request.headers.get("Forwarded")?.includes("proto=https") ?? false) : request.url.startsWith("https://");
102
+ };
103
+ var formatZodError = (error) => {
104
+ if (!error.issues || error.issues.length === 0) {
105
+ return {};
106
+ }
107
+ return error.issues.reduce((previous, issue) => {
108
+ const key = issue.path.join(".");
109
+ return {
110
+ ...previous,
111
+ [key]: {
112
+ code: issue.code,
113
+ message: issue.message
114
+ }
115
+ };
116
+ }, {});
117
+ };
118
+ var extractPath = (url) => {
119
+ const pathRegex = /^https?:\/\/[a-zA-Z0-9_\-\.]+(:\d+)?(\/.*)$/;
120
+ const match = url.match(pathRegex);
121
+ return match && match[2] ? match[2] : "/";
122
+ };
123
+ var createStructuredData = (data, sdID = "metadata") => {
124
+ const entries = Object.entries(data);
125
+ if (entries.length === 0) return `[${sdID}]`;
126
+ const values = entries.map(([key, value]) => `${key}="${String(value).replace(/(["\\\]])/g, "\\$1")}"`).join(" ");
127
+ return `[${sdID} ${values}]`;
128
+ };
129
+ var getErrorName = (error) => {
130
+ if (error instanceof Error) {
131
+ return error.name;
132
+ }
133
+ return typeof error === "string" ? error : "UnknownError";
134
+ };
135
+
136
+ export {
137
+ toSnakeCase,
138
+ toUpperCase,
139
+ toCastCase,
140
+ equals,
141
+ createErrorHandler,
142
+ getBaseURL,
143
+ toISOString,
144
+ useSecureCookies,
145
+ formatZodError,
146
+ extractPath,
147
+ createStructuredData,
148
+ getErrorName
149
+ };
@@ -0,0 +1,113 @@
1
+ import {
2
+ spotify
3
+ } from "./chunk-IPKO6UQN.js";
4
+ import {
5
+ strava
6
+ } from "./chunk-54CZPKR4.js";
7
+ import {
8
+ x
9
+ } from "./chunk-EEE7UM5T.js";
10
+ import {
11
+ bitbucket
12
+ } from "./chunk-4YHJ4IEQ.js";
13
+ import {
14
+ discord
15
+ } from "./chunk-OVHNRULD.js";
16
+ import {
17
+ figma
18
+ } from "./chunk-KMMAZFSJ.js";
19
+ import {
20
+ github
21
+ } from "./chunk-FW4W3REU.js";
22
+ import {
23
+ gitlab
24
+ } from "./chunk-5LZ7TOM3.js";
25
+ import {
26
+ mailchimp
27
+ } from "./chunk-LDU7A2JE.js";
28
+ import {
29
+ pinterest
30
+ } from "./chunk-E6G5YCI6.js";
31
+ import {
32
+ OAuthEnvSchema,
33
+ OAuthProviderCredentialsSchema
34
+ } from "./chunk-KJBAQZX2.js";
35
+ import {
36
+ formatZodError
37
+ } from "./chunk-QQVSRXGX.js";
38
+ import {
39
+ env
40
+ } from "./chunk-4MYWAOLG.js";
41
+ import {
42
+ AuthInternalError
43
+ } from "./chunk-RRLIF4PQ.js";
44
+
45
+ // src/oauth/index.ts
46
+ var builtInOAuthProviders = {
47
+ github,
48
+ bitbucket,
49
+ figma,
50
+ discord,
51
+ gitlab,
52
+ spotify,
53
+ x,
54
+ strava,
55
+ mailchimp,
56
+ pinterest
57
+ };
58
+ var defineOAuthEnvironment = (oauth) => {
59
+ const clientIdSuffix = `${oauth.toUpperCase()}_CLIENT_ID`;
60
+ const clientSecretSuffix = `${oauth.toUpperCase()}_CLIENT_SECRET`;
61
+ const loadEnvs = OAuthEnvSchema.safeParse({
62
+ clientId: env[`AURA_AUTH_${clientIdSuffix}`] ?? env[`AUTH_${clientIdSuffix}`] ?? env[`${clientIdSuffix}`],
63
+ clientSecret: env[`AURA_AUTH_${clientSecretSuffix}`] ?? env[`AUTH_${clientSecretSuffix}`] ?? env[`${clientSecretSuffix}`]
64
+ });
65
+ if (!loadEnvs.success) {
66
+ const msg = JSON.stringify(formatZodError(loadEnvs.error), null, 2);
67
+ throw new AuthInternalError("INVALID_ENVIRONMENT_CONFIGURATION", msg);
68
+ }
69
+ return loadEnvs.data;
70
+ };
71
+ var defineOAuthProviderConfig = (config) => {
72
+ if (typeof config === "string") {
73
+ const definition = defineOAuthEnvironment(config);
74
+ const oauthConfig = builtInOAuthProviders[config]();
75
+ const parsed2 = OAuthProviderCredentialsSchema.safeParse({ ...oauthConfig, ...definition });
76
+ if (!parsed2.success) {
77
+ const details = JSON.stringify(formatZodError(parsed2.error), null, 2);
78
+ throw new AuthInternalError(
79
+ "INVALID_OAUTH_PROVIDER_CONFIGURATION",
80
+ `Invalid configuration for OAuth provider "${config}": ${details}`
81
+ );
82
+ }
83
+ return parsed2.data;
84
+ }
85
+ const hasCredentials = config.clientId && config.clientSecret;
86
+ const envConfig = hasCredentials ? {} : defineOAuthEnvironment(config.id);
87
+ const parsed = OAuthProviderCredentialsSchema.safeParse({ ...envConfig, ...config });
88
+ if (!parsed.success) {
89
+ const details = JSON.stringify(formatZodError(parsed.error), null, 2);
90
+ throw new AuthInternalError(
91
+ "INVALID_OAUTH_PROVIDER_CONFIGURATION",
92
+ `Invalid configuration for OAuth provider "${config.id}": ${details}`
93
+ );
94
+ }
95
+ return parsed.data;
96
+ };
97
+ var createBuiltInOAuthProviders = (oauth = []) => {
98
+ return oauth.reduce((previous, config) => {
99
+ const oauthConfig = defineOAuthProviderConfig(config);
100
+ if (oauthConfig.id in previous) {
101
+ throw new AuthInternalError(
102
+ "DUPLICATED_OAUTH_PROVIDER_ID",
103
+ `Duplicate OAuth provider id "${oauthConfig.id}" found. Each provider must have a unique id.`
104
+ );
105
+ }
106
+ return { ...previous, [oauthConfig.id]: oauthConfig };
107
+ }, {});
108
+ };
109
+
110
+ export {
111
+ builtInOAuthProviders,
112
+ createBuiltInOAuthProviders
113
+ };
@@ -1,10 +1,3 @@
1
- import {
2
- formatZodError
3
- } from "./chunk-CXLATHS5.js";
4
- import {
5
- AuthInternalError,
6
- OAuthProtocolError
7
- } from "./chunk-RRLIF4PQ.js";
8
1
  import {
9
2
  fetchAsync
10
3
  } from "./chunk-ZNCZVF6U.js";
@@ -12,17 +5,28 @@ import {
12
5
  OAuthAccessToken,
13
6
  OAuthAccessTokenErrorResponse,
14
7
  OAuthAccessTokenResponse
15
- } from "./chunk-YRCB5FLE.js";
8
+ } from "./chunk-KJBAQZX2.js";
9
+ import {
10
+ AuthInternalError,
11
+ OAuthProtocolError
12
+ } from "./chunk-RRLIF4PQ.js";
16
13
 
17
14
  // src/actions/callback/access-token.ts
18
- var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) => {
15
+ var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier, logger) => {
19
16
  const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
20
17
  if (!parsed.success) {
21
- const msg = JSON.stringify(formatZodError(parsed.error), null, 2);
22
- throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", msg);
18
+ logger?.log("INVALID_OAUTH_CONFIGURATION");
19
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
23
20
  }
24
21
  const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
25
22
  try {
23
+ logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_INITIATED", {
24
+ structuredData: {
25
+ has_client_id: Boolean(clientId),
26
+ redirect_uri: redirectParsed,
27
+ grant_type: "authorization_code"
28
+ }
29
+ });
26
30
  const response = await fetchAsync(accessToken, {
27
31
  method: "POST",
28
32
  headers: {
@@ -38,17 +42,33 @@ var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) =>
38
42
  code_verifier: codeVerifier
39
43
  }).toString()
40
44
  });
45
+ if (!response.ok) {
46
+ logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
47
+ throw new OAuthProtocolError("invalid_request", "Invalid access token response");
48
+ }
41
49
  const json = await response.json();
42
50
  const token = OAuthAccessTokenResponse.safeParse(json);
43
51
  if (!token.success) {
44
52
  const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
45
53
  if (!success) {
46
- throw new OAuthProtocolError("INVALID_REQUEST", "Invalid access token response format");
54
+ logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
55
+ throw new OAuthProtocolError("invalid_request", "Invalid access token response format");
47
56
  }
48
- throw new OAuthProtocolError(data.error, data?.error_description ?? "Failed to retrieve access token");
57
+ logger?.log("OAUTH_ACCESS_TOKEN_ERROR", {
58
+ structuredData: {
59
+ error: data.error,
60
+ error_description: data.error_description ?? ""
61
+ }
62
+ });
63
+ throw new OAuthProtocolError("INVALID_ACCESS_TOKEN", "Failed to retrieve access token");
49
64
  }
65
+ logger?.log("OAUTH_ACCESS_TOKEN_SUCCESS");
50
66
  return token.data;
51
67
  } catch (error) {
68
+ logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_FAILED");
69
+ if (error instanceof Error) {
70
+ throw new OAuthProtocolError("server_error", "Failed to communicate with OAuth provider", "", { cause: error });
71
+ }
52
72
  throw error;
53
73
  }
54
74
  };
@@ -0,0 +1,267 @@
1
+ // src/logger.ts
2
+ var logMessages = {
3
+ ROUTER_INTERNAL_ERROR: {
4
+ facility: 10,
5
+ severity: "error",
6
+ msgId: "ROUTER_INTERNAL_ERROR",
7
+ message: "Unhandled router error while processing the request"
8
+ },
9
+ INVALID_REQUEST: {
10
+ facility: 10,
11
+ severity: "warning",
12
+ msgId: "INVALID_REQUEST",
13
+ message: "Request validation failed against the expected schema"
14
+ },
15
+ SERVER_ERROR: {
16
+ facility: 10,
17
+ severity: "error",
18
+ msgId: "SERVER_ERROR",
19
+ message: "Unexpected internal server error during authentication"
20
+ },
21
+ OAUTH_PROTOCOL_ERROR: {
22
+ facility: 10,
23
+ severity: "warning",
24
+ msgId: "OAUTH_PROTOCOL_ERROR",
25
+ message: "OAuth provider returned an invalid or unexpected protocol response"
26
+ },
27
+ OAUTH_AUTHORIZATION_ERROR: {
28
+ facility: 10,
29
+ severity: "error",
30
+ msgId: "OAUTH_AUTHORIZATION_ERROR",
31
+ message: "OAuth authorization request was rejected or failed"
32
+ },
33
+ INVALID_OAUTH_CONFIGURATION: {
34
+ facility: 10,
35
+ severity: "error",
36
+ msgId: "INVALID_OAUTH_CONFIGURATION",
37
+ message: "The OAuth provider configuration is invalid or incomplete"
38
+ },
39
+ OAUTH_ACCESS_TOKEN_REQUEST_INITIATED: {
40
+ facility: 10,
41
+ severity: "debug",
42
+ msgId: "OAUTH_ACCESS_TOKEN_REQUEST_INITIATED",
43
+ message: "Starting OAuth access token request to the provider"
44
+ },
45
+ INVALID_OAUTH_ACCESS_TOKEN_RESPONSE: {
46
+ facility: 10,
47
+ severity: "error",
48
+ msgId: "INVALID_OAUTH_ACCESS_TOKEN_RESPONSE",
49
+ message: "OAuth access token endpoint returned an invalid or malformed response"
50
+ },
51
+ OAUTH_ACCESS_TOKEN_ERROR: {
52
+ facility: 10,
53
+ severity: "error",
54
+ msgId: "OAUTH_ACCESS_TOKEN_ERROR",
55
+ message: "OAuth access token endpoint returned an error response"
56
+ },
57
+ OAUTH_ACCESS_TOKEN_SUCCESS: {
58
+ facility: 10,
59
+ severity: "info",
60
+ msgId: "OAUTH_ACCESS_TOKEN_SUCCESS",
61
+ message: "Successfully retrieved OAuth access token from the provider"
62
+ },
63
+ OAUTH_ACCESS_TOKEN_REQUEST_FAILED: {
64
+ facility: 10,
65
+ severity: "error",
66
+ msgId: "OAUTH_ACCESS_TOKEN_REQUEST_FAILED",
67
+ message: "Network or server error while requesting OAuth access token"
68
+ },
69
+ OAUTH_USERINFO_REQUEST_INITIATED: {
70
+ facility: 10,
71
+ severity: "debug",
72
+ msgId: "OAUTH_USERINFO_REQUEST_INITIATED",
73
+ message: "Starting OAuth userinfo request to the provider"
74
+ },
75
+ OAUTH_USERINFO_INVALID_RESPONSE: {
76
+ facility: 10,
77
+ severity: "error",
78
+ msgId: "OAUTH_USERINFO_INVALID_RESPONSE",
79
+ message: "OAuth userinfo endpoint returned an invalid or malformed response"
80
+ },
81
+ OAUTH_USERINFO_ERROR: {
82
+ facility: 10,
83
+ severity: "error",
84
+ msgId: "OAUTH_USERINFO_ERROR",
85
+ message: "OAuth userinfo endpoint returned an error response"
86
+ },
87
+ OAUTH_USERINFO_SUCCESS: {
88
+ facility: 10,
89
+ severity: "info",
90
+ msgId: "OAUTH_USERINFO_SUCCESS",
91
+ message: "Successfully retrieved user information from the OAuth provider"
92
+ },
93
+ OAUTH_USERINFO_REQUEST_FAILED: {
94
+ facility: 10,
95
+ severity: "error",
96
+ msgId: "OAUTH_USERINFO_REQUEST_FAILED",
97
+ message: "Network or server error while requesting user information from the OAuth provider"
98
+ },
99
+ OAUTH_CALLBACK_SUCCESS: {
100
+ facility: 4,
101
+ severity: "info",
102
+ msgId: "OAUTH_CALLBACK_SUCCESS",
103
+ message: "OAuth callback completed successfully and session was created"
104
+ },
105
+ MISMATCHING_STATE: {
106
+ facility: 4,
107
+ severity: "critical",
108
+ msgId: "MISMATCHING_STATE",
109
+ message: "OAuth response state parameter does not match the stored state value"
110
+ },
111
+ POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED: {
112
+ facility: 4,
113
+ severity: "critical",
114
+ msgId: "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
115
+ message: "Blocked redirect to untrusted or external URL (potential open redirect attack)"
116
+ },
117
+ OPEN_REDIRECT_ATTACK: {
118
+ facility: 4,
119
+ severity: "warning",
120
+ msgId: "OPEN_REDIRECT_ATTACK",
121
+ message: "Detected redirect target that does not match the trusted origin"
122
+ },
123
+ SESSION_TOKEN_MISSING: {
124
+ facility: 4,
125
+ severity: "warning",
126
+ msgId: "SESSION_TOKEN_MISSING",
127
+ message: "Session cookie is missing from the request"
128
+ },
129
+ CSRF_TOKEN_MISSING: {
130
+ facility: 4,
131
+ severity: "warning",
132
+ msgId: "CSRF_TOKEN_MISSING",
133
+ message: "CSRF token cookie is missing from the request"
134
+ },
135
+ CSRF_HEADER_MISSING: {
136
+ facility: 4,
137
+ severity: "warning",
138
+ msgId: "CSRF_HEADER_MISSING",
139
+ message: "CSRF header is missing from the request"
140
+ },
141
+ CSRF_TOKEN_INVALID: {
142
+ facility: 4,
143
+ severity: "error",
144
+ msgId: "CSRF_TOKEN_INVALID",
145
+ message: "CSRF token verification failed or token is invalid"
146
+ },
147
+ SIGN_IN_INITIATED: {
148
+ facility: 4,
149
+ severity: "info",
150
+ msgId: "SIGN_IN_INITIATED",
151
+ message: "Starting OAuth sign-in flow for the selected provider"
152
+ },
153
+ SIGN_OUT_ATTEMPT: {
154
+ facility: 4,
155
+ severity: "debug",
156
+ msgId: "SIGN_OUT_ATTEMPT",
157
+ message: "Received sign-out request from client"
158
+ },
159
+ SIGN_OUT_CSRF_VERIFIED: {
160
+ facility: 4,
161
+ severity: "info",
162
+ msgId: "SIGN_OUT_CSRF_VERIFIED",
163
+ message: "CSRF token was successfully verified during sign-out"
164
+ },
165
+ SIGN_OUT_SUCCESS: {
166
+ facility: 4,
167
+ severity: "info",
168
+ msgId: "SIGN_OUT_SUCCESS",
169
+ message: "User session was cleared and sign-out completed successfully"
170
+ },
171
+ SIGN_OUT_REDIRECT: {
172
+ facility: 4,
173
+ severity: "debug",
174
+ msgId: "SIGN_OUT_REDIRECT",
175
+ message: "Redirecting client after successful sign-out"
176
+ },
177
+ AUTH_SESSION_VALID: {
178
+ facility: 4,
179
+ severity: "info",
180
+ msgId: "AUTH_SESSION_VALID",
181
+ message: "Session token is valid and user session was returned"
182
+ },
183
+ AUTH_SESSION_INVALID: {
184
+ facility: 4,
185
+ severity: "notice",
186
+ msgId: "AUTH_SESSION_INVALID",
187
+ message: "Session token is missing, expired, or invalid"
188
+ },
189
+ INVALID_JWT_TOKEN: {
190
+ facility: 4,
191
+ severity: "warning",
192
+ msgId: "INVALID_JWT_TOKEN",
193
+ message: "JWT session token failed validation during sign-out"
194
+ },
195
+ CSRF_TOKEN_REQUESTED: {
196
+ facility: 4,
197
+ severity: "debug",
198
+ msgId: "CSRF_TOKEN_REQUESTED",
199
+ message: "Client requested a CSRF token"
200
+ },
201
+ CSRF_TOKEN_ISSUED: {
202
+ facility: 4,
203
+ severity: "debug",
204
+ msgId: "CSRF_TOKEN_ISSUED",
205
+ message: "Issued a new CSRF token to the client"
206
+ },
207
+ INVALID_URL: {
208
+ facility: 10,
209
+ severity: "error",
210
+ msgId: "INVALID_URL",
211
+ message: "Derived origin URL is invalid or malformed"
212
+ },
213
+ COOKIE_HTTPONLY_DISABLED: {
214
+ facility: 10,
215
+ severity: "critical",
216
+ msgId: "COOKIE_HTTPONLY_DISABLED",
217
+ message: "Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS exposure."
218
+ },
219
+ COOKIE_WILDCARD_DOMAIN: {
220
+ facility: 10,
221
+ severity: "critical",
222
+ msgId: "COOKIE_WILDCARD_DOMAIN",
223
+ message: "Cookie 'Domain' is set to a wildcard, which is insecure and should be avoided."
224
+ },
225
+ COOKIE_SECURE_DISABLED: {
226
+ facility: 10,
227
+ severity: "warning",
228
+ msgId: "COOKIE_SECURE_DISABLED",
229
+ message: "Cookie is configured with 'Secure' but the request is not HTTPS. The 'Secure' attribute will be ignored by the browser."
230
+ },
231
+ COOKIE_SAMESITE_NONE_WITHOUT_SECURE: {
232
+ facility: 10,
233
+ severity: "warning",
234
+ msgId: "COOKIE_SAMESITE_NONE_WITHOUT_SECURE",
235
+ message: "Cookie uses SameSite=None without Secure. Falling back to SameSite=Lax for safer defaults."
236
+ },
237
+ COOKIE_INSECURE_IN_PRODUCTION: {
238
+ facility: 10,
239
+ severity: "critical",
240
+ msgId: "COOKIE_INSECURE_IN_PRODUCTION",
241
+ message: "Cookies are being served over an insecure connection in production, which is a serious security risk."
242
+ },
243
+ COOKIE_HOST_STRATEGY_INSECURE: {
244
+ facility: 10,
245
+ severity: "critical",
246
+ msgId: "COOKIE_HOST_STRATEGY_INSECURE",
247
+ message: "__Host- cookies require a secure HTTPS context. Falling back to standard cookie settings."
248
+ },
249
+ UNTRUSTED_ORIGIN: {
250
+ facility: 10,
251
+ severity: "error",
252
+ msgId: "UNTRUSTED_ORIGIN",
253
+ message: "The constructed origin URL is not trusted."
254
+ }
255
+ };
256
+ var createLogEntry = (key, overrides) => {
257
+ const message = logMessages[key];
258
+ return {
259
+ ...message,
260
+ ...overrides
261
+ };
262
+ };
263
+
264
+ export {
265
+ logMessages,
266
+ createLogEntry
267
+ };