@aura-stack/auth 0.4.0 → 0.6.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 (179) hide show
  1. package/README.md +36 -1
  2. package/dist/@types/index.cjs +0 -18
  3. package/dist/@types/index.d.ts +2 -8
  4. package/dist/@types/index.js +0 -1
  5. package/dist/assert-B3iQSYlK.js +3 -0
  6. package/dist/assert-NJGroSJd.cjs +3 -0
  7. package/dist/client/index.cjs +1 -0
  8. package/dist/client/index.d.ts +11 -0
  9. package/dist/client/index.js +1 -0
  10. package/dist/crypto-Bz8nIciY.js +1 -0
  11. package/dist/crypto-CoXA5w_4.cjs +1 -0
  12. package/dist/env-bq387KyP.cjs +1 -0
  13. package/dist/env-nvh8QBNz.js +1 -0
  14. package/dist/errors-CCYPHuBO.cjs +1 -0
  15. package/dist/errors-DFWHOho6.js +1 -0
  16. package/dist/index-BkpwQ0l4.d.cts +2279 -0
  17. package/dist/index-nqLV2t91.d.ts +2279 -0
  18. package/dist/index.cjs +1 -1839
  19. package/dist/index.d.cts +2 -0
  20. package/dist/index.d.ts +2 -35
  21. package/dist/index.js +1 -132
  22. package/dist/logger-C59_CDMk.js +1 -0
  23. package/dist/logger-UnUhYL2V.cjs +1 -0
  24. package/dist/oauth/atlassian.cjs +1 -0
  25. package/dist/oauth/atlassian.d.ts +2 -0
  26. package/dist/oauth/atlassian.js +1 -0
  27. package/dist/oauth/bitbucket.cjs +1 -49
  28. package/dist/oauth/bitbucket.d.ts +2 -8
  29. package/dist/oauth/bitbucket.js +1 -6
  30. package/dist/oauth/discord.cjs +1 -57
  31. package/dist/oauth/discord.d.ts +2 -8
  32. package/dist/oauth/discord.js +1 -6
  33. package/dist/oauth/dropbox.cjs +1 -0
  34. package/dist/oauth/dropbox.d.ts +2 -0
  35. package/dist/oauth/dropbox.js +1 -0
  36. package/dist/oauth/figma.cjs +1 -49
  37. package/dist/oauth/figma.d.ts +2 -8
  38. package/dist/oauth/figma.js +1 -6
  39. package/dist/oauth/github.cjs +1 -49
  40. package/dist/oauth/github.d.ts +2 -8
  41. package/dist/oauth/github.js +1 -6
  42. package/dist/oauth/gitlab.cjs +1 -49
  43. package/dist/oauth/gitlab.d.ts +2 -8
  44. package/dist/oauth/gitlab.js +1 -6
  45. package/dist/oauth/index.cjs +1 -483
  46. package/dist/oauth/index.d.ts +2 -8
  47. package/dist/oauth/index.js +1 -52
  48. package/dist/oauth/mailchimp.cjs +1 -49
  49. package/dist/oauth/mailchimp.d.ts +2 -8
  50. package/dist/oauth/mailchimp.js +1 -6
  51. package/dist/oauth/notion.cjs +1 -0
  52. package/dist/oauth/notion.d.ts +2 -0
  53. package/dist/oauth/notion.js +1 -0
  54. package/dist/oauth/pinterest.cjs +1 -49
  55. package/dist/oauth/pinterest.d.ts +2 -8
  56. package/dist/oauth/pinterest.js +1 -6
  57. package/dist/oauth/spotify.cjs +1 -49
  58. package/dist/oauth/spotify.d.ts +2 -8
  59. package/dist/oauth/spotify.js +1 -6
  60. package/dist/oauth/strava.cjs +1 -49
  61. package/dist/oauth/strava.d.ts +2 -8
  62. package/dist/oauth/strava.js +1 -6
  63. package/dist/oauth/twitch.cjs +1 -0
  64. package/dist/oauth/twitch.d.ts +2 -0
  65. package/dist/oauth/twitch.js +1 -0
  66. package/dist/oauth/x.cjs +1 -49
  67. package/dist/oauth/x.d.ts +2 -8
  68. package/dist/oauth/x.js +1 -6
  69. package/dist/oauth-BntNm6aE.cjs +1 -0
  70. package/dist/oauth-DmHy9VrB.js +1 -0
  71. package/dist/shared/crypto.cjs +1 -0
  72. package/dist/shared/crypto.d.ts +47 -0
  73. package/dist/shared/crypto.js +1 -0
  74. package/dist/shared/identity.cjs +1 -0
  75. package/dist/shared/identity.d.ts +2 -0
  76. package/dist/shared/identity.js +1 -0
  77. package/dist/shared/index.cjs +1 -0
  78. package/dist/shared/index.d.ts +5 -0
  79. package/dist/shared/index.js +1 -0
  80. package/package.json +39 -12
  81. package/dist/@types/router.d.cjs +0 -1
  82. package/dist/@types/router.d.d.ts +0 -12
  83. package/dist/@types/router.d.js +0 -0
  84. package/dist/@types/utility.cjs +0 -18
  85. package/dist/@types/utility.d.ts +0 -6
  86. package/dist/@types/utility.js +0 -1
  87. package/dist/actions/callback/access-token.cjs +0 -206
  88. package/dist/actions/callback/access-token.d.ts +0 -29
  89. package/dist/actions/callback/access-token.js +0 -9
  90. package/dist/actions/callback/callback.cjs +0 -649
  91. package/dist/actions/callback/callback.d.ts +0 -13
  92. package/dist/actions/callback/callback.js +0 -19
  93. package/dist/actions/callback/userinfo.cjs +0 -250
  94. package/dist/actions/callback/userinfo.d.ts +0 -21
  95. package/dist/actions/callback/userinfo.js +0 -14
  96. package/dist/actions/csrfToken/csrfToken.cjs +0 -197
  97. package/dist/actions/csrfToken/csrfToken.d.ts +0 -5
  98. package/dist/actions/csrfToken/csrfToken.js +0 -14
  99. package/dist/actions/index.cjs +0 -954
  100. package/dist/actions/index.d.ts +0 -14
  101. package/dist/actions/index.js +0 -36
  102. package/dist/actions/session/session.cjs +0 -136
  103. package/dist/actions/session/session.d.ts +0 -5
  104. package/dist/actions/session/session.js +0 -10
  105. package/dist/actions/signIn/authorization.cjs +0 -322
  106. package/dist/actions/signIn/authorization.d.ts +0 -53
  107. package/dist/actions/signIn/authorization.js +0 -18
  108. package/dist/actions/signIn/signIn.cjs +0 -467
  109. package/dist/actions/signIn/signIn.d.ts +0 -13
  110. package/dist/actions/signIn/signIn.js +0 -15
  111. package/dist/actions/signOut/signOut.cjs +0 -493
  112. package/dist/actions/signOut/signOut.d.ts +0 -8
  113. package/dist/actions/signOut/signOut.js +0 -16
  114. package/dist/assert.cjs +0 -161
  115. package/dist/assert.d.ts +0 -33
  116. package/dist/assert.js +0 -26
  117. package/dist/chunk-4EKY7655.js +0 -123
  118. package/dist/chunk-4MYWAOLG.js +0 -31
  119. package/dist/chunk-4YHJ4IEQ.js +0 -25
  120. package/dist/chunk-54CZPKR4.js +0 -25
  121. package/dist/chunk-5LZ7TOM3.js +0 -25
  122. package/dist/chunk-5W4BRQYG.js +0 -201
  123. package/dist/chunk-6MXFPFR3.js +0 -143
  124. package/dist/chunk-7QF22LHP.js +0 -67
  125. package/dist/chunk-ALG3GIV4.js +0 -95
  126. package/dist/chunk-E6G5YCI6.js +0 -25
  127. package/dist/chunk-EBAMFRB7.js +0 -34
  128. package/dist/chunk-EEE7UM5T.js +0 -25
  129. package/dist/chunk-FRJFWTOY.js +0 -70
  130. package/dist/chunk-FW4W3REU.js +0 -25
  131. package/dist/chunk-ICAZ4OVS.js +0 -37
  132. package/dist/chunk-IPKO6UQN.js +0 -25
  133. package/dist/chunk-ITQ7352M.js +0 -0
  134. package/dist/chunk-KJBAQZX2.js +0 -92
  135. package/dist/chunk-KMMAZFSJ.js +0 -25
  136. package/dist/chunk-LDU7A2JE.js +0 -25
  137. package/dist/chunk-NUDITUKX.js +0 -73
  138. package/dist/chunk-OVHNRULD.js +0 -33
  139. package/dist/chunk-PG7UYFG5.js +0 -0
  140. package/dist/chunk-PHFH2MGS.js +0 -36
  141. package/dist/chunk-QQVSRXGX.js +0 -149
  142. package/dist/chunk-RRLIF4PQ.js +0 -55
  143. package/dist/chunk-TM5IPSNF.js +0 -113
  144. package/dist/chunk-TZB6MUXN.js +0 -78
  145. package/dist/chunk-VNCNJKS2.js +0 -267
  146. package/dist/chunk-XGLBNXL4.js +0 -75
  147. package/dist/chunk-XUP6KKNG.js +0 -106
  148. package/dist/chunk-ZNCZVF6U.js +0 -14
  149. package/dist/cookie.cjs +0 -246
  150. package/dist/cookie.d.ts +0 -85
  151. package/dist/cookie.js +0 -29
  152. package/dist/env.cjs +0 -56
  153. package/dist/env.d.ts +0 -7
  154. package/dist/env.js +0 -6
  155. package/dist/errors.cjs +0 -85
  156. package/dist/errors.d.ts +0 -50
  157. package/dist/errors.js +0 -18
  158. package/dist/headers.cjs +0 -61
  159. package/dist/headers.d.ts +0 -33
  160. package/dist/headers.js +0 -12
  161. package/dist/index-CSyIJmCM.d.ts +0 -1007
  162. package/dist/jose.cjs +0 -128
  163. package/dist/jose.d.ts +0 -25
  164. package/dist/jose.js +0 -12
  165. package/dist/logger.cjs +0 -292
  166. package/dist/logger.d.ts +0 -8
  167. package/dist/logger.js +0 -8
  168. package/dist/request.cjs +0 -38
  169. package/dist/request.d.ts +0 -13
  170. package/dist/request.js +0 -6
  171. package/dist/schemas.cjs +0 -125
  172. package/dist/schemas.d.ts +0 -149
  173. package/dist/schemas.js +0 -24
  174. package/dist/secure.cjs +0 -170
  175. package/dist/secure.d.ts +0 -35
  176. package/dist/secure.js +0 -19
  177. package/dist/utils.cjs +0 -223
  178. package/dist/utils.d.ts +0 -24
  179. package/dist/utils.js +0 -29
@@ -1,36 +0,0 @@
1
- import {
2
- getErrorName,
3
- toISOString
4
- } from "./chunk-QQVSRXGX.js";
5
- import {
6
- expiredCookieAttributes,
7
- getCookie
8
- } from "./chunk-5W4BRQYG.js";
9
- import {
10
- secureApiHeaders
11
- } from "./chunk-EBAMFRB7.js";
12
-
13
- // src/actions/session/session.ts
14
- import { createEndpoint, HeadersBuilder } from "@aura-stack/router";
15
- var sessionAction = createEndpoint("GET", "/session", async (ctx) => {
16
- const {
17
- request,
18
- context: { jose, cookies, logger }
19
- } = ctx;
20
- try {
21
- const session = getCookie(request, cookies.sessionToken.name);
22
- const decoded = await jose.decodeJWT(session);
23
- logger?.log("AUTH_SESSION_VALID");
24
- const { exp, iat, jti, nbf, ...user } = decoded;
25
- const headers = new Headers(secureApiHeaders);
26
- return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
27
- } catch (error) {
28
- logger?.log("AUTH_SESSION_INVALID", { structuredData: { error_type: getErrorName(error) } });
29
- const headers = new HeadersBuilder(secureApiHeaders).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
30
- return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
31
- }
32
- });
33
-
34
- export {
35
- sessionAction
36
- };
@@ -1,149 +0,0 @@
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
- };
@@ -1,55 +0,0 @@
1
- // src/errors.ts
2
- var OAuthProtocolError = class extends Error {
3
- type = "OAUTH_PROTOCOL_ERROR";
4
- error;
5
- errorURI;
6
- constructor(error, description, errorURI, options) {
7
- super(description, options);
8
- this.error = error;
9
- this.errorURI = errorURI;
10
- this.name = new.target.name;
11
- Error.captureStackTrace(this, new.target);
12
- }
13
- };
14
- var AuthInternalError = class extends Error {
15
- type = "AUTH_INTERNAL_ERROR";
16
- code;
17
- constructor(code, message, options) {
18
- super(message, options);
19
- this.code = code;
20
- this.name = new.target.name;
21
- Error.captureStackTrace(this, new.target);
22
- }
23
- };
24
- var AuthSecurityError = class extends Error {
25
- type = "AUTH_SECURITY_ERROR";
26
- code;
27
- constructor(code, message, options) {
28
- super(message, options);
29
- this.code = code;
30
- this.name = new.target.name;
31
- Error.captureStackTrace(this, new.target);
32
- }
33
- };
34
- var isNativeError = (error) => {
35
- return error instanceof Error;
36
- };
37
- var isOAuthProtocolError = (error) => {
38
- return error instanceof OAuthProtocolError;
39
- };
40
- var isAuthInternalError = (error) => {
41
- return error instanceof AuthInternalError;
42
- };
43
- var isAuthSecurityError = (error) => {
44
- return error instanceof AuthSecurityError;
45
- };
46
-
47
- export {
48
- OAuthProtocolError,
49
- AuthInternalError,
50
- AuthSecurityError,
51
- isNativeError,
52
- isOAuthProtocolError,
53
- isAuthInternalError,
54
- isAuthSecurityError
55
- };
@@ -1,113 +0,0 @@
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,78 +0,0 @@
1
- import {
2
- fetchAsync
3
- } from "./chunk-ZNCZVF6U.js";
4
- import {
5
- OAuthAccessToken,
6
- OAuthAccessTokenErrorResponse,
7
- OAuthAccessTokenResponse
8
- } from "./chunk-KJBAQZX2.js";
9
- import {
10
- AuthInternalError,
11
- OAuthProtocolError
12
- } from "./chunk-RRLIF4PQ.js";
13
-
14
- // src/actions/callback/access-token.ts
15
- var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier, logger) => {
16
- const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
17
- if (!parsed.success) {
18
- logger?.log("INVALID_OAUTH_CONFIGURATION");
19
- throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
20
- }
21
- const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
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
- });
30
- const response = await fetchAsync(accessToken, {
31
- method: "POST",
32
- headers: {
33
- Accept: "application/json",
34
- "Content-Type": "application/x-www-form-urlencoded"
35
- },
36
- body: new URLSearchParams({
37
- client_id: clientId,
38
- client_secret: clientSecret,
39
- code: codeParsed,
40
- redirect_uri: redirectParsed,
41
- grant_type: "authorization_code",
42
- code_verifier: codeVerifier
43
- }).toString()
44
- });
45
- if (!response.ok) {
46
- logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
47
- throw new OAuthProtocolError("invalid_request", "Invalid access token response");
48
- }
49
- const json = await response.json();
50
- const token = OAuthAccessTokenResponse.safeParse(json);
51
- if (!token.success) {
52
- const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
53
- if (!success) {
54
- logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
55
- throw new OAuthProtocolError("invalid_request", "Invalid access token response format");
56
- }
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");
64
- }
65
- logger?.log("OAUTH_ACCESS_TOKEN_SUCCESS");
66
- return token.data;
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
- }
72
- throw error;
73
- }
74
- };
75
-
76
- export {
77
- createAccessToken
78
- };
@@ -1,267 +0,0 @@
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
- };