@mindfulauth/core 1.2.1 → 2.0.0-beta.2
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.
- package/README.md +2 -14
- package/dist/auth-handler.d.ts.map +1 -1
- package/dist/auth-handler.js +9 -2
- package/dist/config.d.ts +2 -7
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +5 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +31 -13
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
|
-
# @mindful-auth/core
|
|
1
|
+
# @mindful-auth/core - ASTRO 6 Beta
|
|
2
2
|
|
|
3
|
-
Core authentication library for Mindful Auth, designed for Astro applications.
|
|
4
|
-
|
|
5
|
-
Install Mindful Auth Astro template here https://docs.mindfulauth.com/guides/frontend/astro/astro-setup/
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- 🔐 Session validation and management
|
|
10
|
-
- 🛡️ Security headers (CSP, HSTS, etc.)
|
|
11
|
-
- 🚦 Public/protected route handling
|
|
12
|
-
- 🔄 Auth proxy for login, registration, 2FA, etc.
|
|
13
|
-
- 🛠️ Path traversal protection
|
|
14
|
-
- ⚡ Built for Cloudflare Workers & Astro SSR
|
|
3
|
+
Core authentication library for Mindful Auth, designed for Astro 6 applications.
|
|
15
4
|
|
|
16
5
|
## License
|
|
17
|
-
|
|
18
6
|
MIT
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-handler.d.ts","sourceRoot":"","sources":["../src/auth-handler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth-handler.d.ts","sourceRoot":"","sources":["../src/auth-handler.ts"],"names":[],"mappings":"AAwEA,2EAA2E;AAC3E,wBAAsB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CA6BpH;AAED,gEAAgE;AAChE,wBAAsB,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAyDrH"}
|
package/dist/auth-handler.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
// Auth proxy handler for Mindful Auth
|
|
2
2
|
// Forwards authentication requests to the central Mindful Auth service
|
|
3
|
+
//
|
|
4
|
+
// ASTRO 6 MIGRATION:
|
|
5
|
+
// - Astro v6 removed context.locals.runtime.env. Env vars now import from 'cloudflare:workers'.
|
|
6
|
+
// - Note: @cloudflare/workers-types must be installed and referenced in env.d.ts.
|
|
7
|
+
import { env } from 'cloudflare:workers';
|
|
3
8
|
import { CENTRAL_AUTH_ORIGIN, ALLOWED_AUTH_METHODS, MAX_BODY_SIZE_BYTES, AUTH_PROXY_TIMEOUT_MS } from './config';
|
|
4
9
|
import { sanitizeEndpoint } from './security';
|
|
5
10
|
const JSON_HEADERS = { 'Content-Type': 'application/json' };
|
|
@@ -60,7 +65,8 @@ function buildResponse(data, status, cookie) {
|
|
|
60
65
|
}
|
|
61
66
|
/** Handle GET requests (email verification, password reset links, etc.) */
|
|
62
67
|
export async function handleAuthGet(rawEndpoint, request, url, locals) {
|
|
63
|
-
|
|
68
|
+
// ASTRO 6 CHANGE: Environment variables accessed from 'cloudflare:workers' env directly.
|
|
69
|
+
const internalApiKey = env.INTERNAL_API_KEY || import.meta.env.INTERNAL_API_KEY;
|
|
64
70
|
if (!internalApiKey)
|
|
65
71
|
console.error('[auth-handler] INTERNAL_API_KEY not configured');
|
|
66
72
|
const endpoint = sanitizeEndpoint(rawEndpoint);
|
|
@@ -94,7 +100,8 @@ export async function handleAuthPost(rawEndpoint, request, url, locals) {
|
|
|
94
100
|
const contentLength = request.headers.get('content-length');
|
|
95
101
|
if (contentLength && parseInt(contentLength) > MAX_BODY_SIZE_BYTES)
|
|
96
102
|
return jsonError('Payload too large', 413);
|
|
97
|
-
|
|
103
|
+
// ASTRO 6 CHANGE: Environment variables accessed from 'cloudflare:workers' env directly.
|
|
104
|
+
const internalApiKey = env.INTERNAL_API_KEY || import.meta.env.INTERNAL_API_KEY;
|
|
98
105
|
if (!internalApiKey)
|
|
99
106
|
console.error('[auth-handler] INTERNAL_API_KEY not configured');
|
|
100
107
|
const endpoint = sanitizeEndpoint(rawEndpoint);
|
package/dist/config.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export declare const CENTRAL_AUTH_ORIGIN = "https://api.mindfulauth.com";
|
|
2
|
-
export declare const CDN_ORIGIN = "https://cdn.mindfulauth.com";
|
|
1
|
+
export declare const CENTRAL_AUTH_ORIGIN = "https://dev-api.mindfulauth.com";
|
|
2
|
+
export declare const CDN_ORIGIN = "https://dev-cdn.mindfulauth.com";
|
|
3
3
|
export declare const ALLOWED_AUTH_METHODS: string[];
|
|
4
4
|
export declare const MAX_BODY_SIZE_BYTES = 1048576;
|
|
5
5
|
export declare const AUTH_PROXY_TIMEOUT_MS = 15000;
|
|
@@ -59,11 +59,6 @@ export declare function getMauthViteDefines(options?: {
|
|
|
59
59
|
/**
|
|
60
60
|
* Get security headers with CSP sources merged from defaults and build-time
|
|
61
61
|
* custom values injected via vite.define (from getMauthViteDefines()).
|
|
62
|
-
*
|
|
63
|
-
* Pass the per-request `nonce` generated by `generateNonce()` so the
|
|
64
|
-
* Content-Security-Policy includes `'nonce-<value>'` in `script-src`.
|
|
65
|
-
* The same nonce must be set as the `nonce` attribute on every inline
|
|
66
|
-
* `<script>` tag (available via `Astro.locals.cspNonce` in layouts).
|
|
67
62
|
*/
|
|
68
63
|
export declare function GET_SECURITY_HEADERS(nonce?: string): Record<string, string>;
|
|
69
64
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,mBAAmB,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,mBAAmB,oCAAoC,CAAC;AACrE,eAAO,MAAM,UAAU,oCAAoC,CAAC;AAG5D,eAAO,MAAM,oBAAoB,UAAkB,CAAC;AACpD,eAAO,MAAM,mBAAmB,UAAU,CAAC;AAC3C,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAC3C,eAAO,MAAM,6BAA6B,QAAQ,CAAC;AAenD;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,EAAE,CAE1C;AAID,eAAO,MAAM,aAAa,UAQzB,CAAC;AAIF,eAAO,MAAM,eAAe,UAO3B,CAAC;AA0CF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,CAAC,EAAE;IAC1C,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,GAAG,CAAC,EAAE;QACF,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;QACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;QAC1B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;QACxB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B,CAAC;CACL,GAAG,OAAO,OAAO,CAEjB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,CAAC,EAAE;IAC1C,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,GAAG,CAAC,EAAE;QACF,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;QACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;QAC1B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;QACxB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B,CAAC;CACL,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQzB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiD3E"}
|
package/dist/config.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Configuration for the Astro Portal
|
|
3
3
|
// ============================================================================
|
|
4
4
|
// API Endpoints
|
|
5
|
-
export const CENTRAL_AUTH_ORIGIN = 'https://api.mindfulauth.com';
|
|
6
|
-
export const CDN_ORIGIN = 'https://cdn.mindfulauth.com';
|
|
5
|
+
export const CENTRAL_AUTH_ORIGIN = 'https://dev-api.mindfulauth.com';
|
|
6
|
+
export const CDN_ORIGIN = 'https://dev-cdn.mindfulauth.com';
|
|
7
7
|
// Request & Timeout Configuration
|
|
8
8
|
export const ALLOWED_AUTH_METHODS = ['GET', 'POST'];
|
|
9
9
|
export const MAX_BODY_SIZE_BYTES = 1048576; // 1MB limit
|
|
@@ -55,14 +55,14 @@ const DEFAULT_SCRIPT_SOURCES = [
|
|
|
55
55
|
"'self'",
|
|
56
56
|
'https://*.cloudflare.com',
|
|
57
57
|
'https://*.cloudflareinsights.com',
|
|
58
|
-
'https://cdn.mindfulauth.com',
|
|
59
|
-
'https://api.mindfulauth.com'
|
|
58
|
+
'https://dev-cdn.mindfulauth.com',
|
|
59
|
+
'https://dev-api.mindfulauth.com'
|
|
60
60
|
];
|
|
61
61
|
// Default allowed connection sources for CSP
|
|
62
62
|
const DEFAULT_CONNECT_SOURCES = [
|
|
63
63
|
"'self'",
|
|
64
64
|
'https://*.cloudflare.com',
|
|
65
|
-
'https://api.mindfulauth.com'
|
|
65
|
+
'https://dev-api.mindfulauth.com'
|
|
66
66
|
];
|
|
67
67
|
// Default allowed frame sources for CSP
|
|
68
68
|
const DEFAULT_FRAME_SOURCES = [
|
|
@@ -116,11 +116,6 @@ export function getMauthViteDefines(options) {
|
|
|
116
116
|
/**
|
|
117
117
|
* Get security headers with CSP sources merged from defaults and build-time
|
|
118
118
|
* custom values injected via vite.define (from getMauthViteDefines()).
|
|
119
|
-
*
|
|
120
|
-
* Pass the per-request `nonce` generated by `generateNonce()` so the
|
|
121
|
-
* Content-Security-Policy includes `'nonce-<value>'` in `script-src`.
|
|
122
|
-
* The same nonce must be set as the `nonce` attribute on every inline
|
|
123
|
-
* `<script>` tag (available via `Astro.locals.cspNonce` in layouts).
|
|
124
119
|
*/
|
|
125
120
|
export function GET_SECURITY_HEADERS(nonce) {
|
|
126
121
|
// Custom sources are baked in at build time via vite.define
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,SAAS,CAAC;AAGxB,cAAc,UAAU,CAAC;AAGzB,cAAc,QAAQ,CAAC;AAGvB,cAAc,gBAAgB,CAAC;AAG/B,cAAc,YAAY,CAAC;AAG3B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,SAAS,CAAC;AAGxB,cAAc,UAAU,CAAC;AAGzB,cAAc,QAAQ,CAAC;AAGvB,cAAc,gBAAgB,CAAC;AAG/B,cAAc,YAAY,CAAC;AAG3B,cAAc,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -9,5 +9,5 @@ export * from './auth';
|
|
|
9
9
|
export * from './auth-handler';
|
|
10
10
|
// Security utilities
|
|
11
11
|
export * from './security';
|
|
12
|
-
// Middleware
|
|
13
|
-
export
|
|
12
|
+
// Middleware
|
|
13
|
+
export * from './middleware';
|
package/dist/middleware.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAoCA,eAAO,MAAM,SAAS,mCAuEpB,CAAC"}
|
package/dist/middleware.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
// Global middleware for session validation
|
|
2
2
|
// Runs before all route handlers to enforce authentication
|
|
3
|
+
//
|
|
4
|
+
// ASTRO 6 MIGRATION:
|
|
5
|
+
// - Astro v6 removed context.locals.runtime.env. Env vars now import from 'cloudflare:workers'.
|
|
6
|
+
// - cloudflare:workers is marked external in astro.config.vite.ssr to avoid esbuild scan errors.
|
|
7
|
+
// - Dev mode bypass uses import.meta.env.DEV (build-time constant: true in dev, false in prod).
|
|
3
8
|
import { defineMiddleware } from 'astro:middleware';
|
|
9
|
+
import { env } from 'cloudflare:workers';
|
|
4
10
|
import { PUBLIC_ROUTES, PUBLIC_PREFIXES, GET_SECURITY_HEADERS, GET_SKIP_ASSETS } from './config';
|
|
5
11
|
import { sanitizePath, generateNonce } from './security';
|
|
6
12
|
import { validateSession, validateMemberIdInUrl } from './auth';
|
|
@@ -9,37 +15,47 @@ function isPublicRoute(pathname) {
|
|
|
9
15
|
return PUBLIC_ROUTES.includes(pathname) ||
|
|
10
16
|
PUBLIC_PREFIXES.some(prefix => pathname.startsWith(prefix));
|
|
11
17
|
}
|
|
12
|
-
/** Add security headers
|
|
18
|
+
/** Add security headers to a response */
|
|
19
|
+
// NOTE: In Cloudflare Workers, Response.headers is immutable.
|
|
20
|
+
// We must create a new Response with a fresh Headers object instead of mutating.
|
|
13
21
|
function addSecurityHeaders(response, nonce) {
|
|
22
|
+
const newHeaders = new Headers(response.headers);
|
|
14
23
|
Object.entries(GET_SECURITY_HEADERS(nonce)).forEach(([key, value]) => {
|
|
15
|
-
|
|
24
|
+
newHeaders.set(key, value);
|
|
25
|
+
});
|
|
26
|
+
return new Response(response.body, {
|
|
27
|
+
status: response.status,
|
|
28
|
+
statusText: response.statusText,
|
|
29
|
+
headers: newHeaders,
|
|
16
30
|
});
|
|
17
|
-
return response;
|
|
18
31
|
}
|
|
19
32
|
// Main middleware function
|
|
20
33
|
export const onRequest = defineMiddleware(async (context, next) => {
|
|
21
34
|
const { request, url, redirect, locals } = context;
|
|
22
35
|
const pathname = url.pathname;
|
|
23
|
-
// Type assertion for locals (users should extend App.Locals in their project)
|
|
24
|
-
const authLocals = locals;
|
|
25
36
|
// Generate a per-request nonce for CSP. Exposed as locals.cspNonce so
|
|
26
37
|
// layouts can add nonce={Astro.locals.cspNonce} to inline <script> tags.
|
|
27
38
|
const nonce = generateNonce();
|
|
28
|
-
|
|
39
|
+
locals.cspNonce = nonce;
|
|
29
40
|
// Skip middleware for static assets FIRST (before dev mode)
|
|
30
41
|
if (GET_SKIP_ASSETS().includes(pathname)) {
|
|
31
42
|
return next();
|
|
32
43
|
}
|
|
33
|
-
// DEV MODE: Skip auth
|
|
34
|
-
|
|
44
|
+
// DEV MODE: Skip auth
|
|
45
|
+
// import.meta.env.DEV is a build-time constant replaced by Vite:
|
|
46
|
+
// - true during local dev (never included in prod build)
|
|
47
|
+
// - false in production builds (tree-shaken out entirely)
|
|
48
|
+
// Localhost auth is skipped because Mindful Auth blocks localhost requests.
|
|
49
|
+
if (import.meta.env.DEV) {
|
|
50
|
+
// Check if public route first
|
|
35
51
|
if (isPublicRoute(pathname)) {
|
|
36
|
-
|
|
52
|
+
locals.recordId = null;
|
|
37
53
|
return next();
|
|
38
54
|
}
|
|
39
55
|
// For protected routes, extract or create mock recordId
|
|
40
56
|
// Match memberid with trailing slash
|
|
41
57
|
const match = pathname.match(/^\/([a-zA-Z0-9-]+)(?:\/|$)/);
|
|
42
|
-
|
|
58
|
+
locals.recordId = match ? match[1] : 'dev-user-123';
|
|
43
59
|
return next();
|
|
44
60
|
}
|
|
45
61
|
// Sanitize path
|
|
@@ -49,11 +65,13 @@ export const onRequest = defineMiddleware(async (context, next) => {
|
|
|
49
65
|
}
|
|
50
66
|
// Public routes - no auth needed
|
|
51
67
|
if (isPublicRoute(safePath)) {
|
|
52
|
-
|
|
68
|
+
locals.recordId = null;
|
|
53
69
|
return addSecurityHeaders(await next(), nonce);
|
|
54
70
|
}
|
|
55
71
|
// Protected route - validate session
|
|
56
|
-
|
|
72
|
+
// ASTRO 6 CHANGE: Environment variables are accessed directly from 'cloudflare:workers' env,
|
|
73
|
+
// not from context.locals.runtime.env (which was removed).
|
|
74
|
+
const internalApiKey = env.INTERNAL_API_KEY || import.meta.env.INTERNAL_API_KEY;
|
|
57
75
|
if (!internalApiKey) {
|
|
58
76
|
console.error('[middleware] CRITICAL: INTERNAL_API_KEY not configured');
|
|
59
77
|
return new Response('Internal Server Error', { status: 500 });
|
|
@@ -71,6 +89,6 @@ export const onRequest = defineMiddleware(async (context, next) => {
|
|
|
71
89
|
if (!validateMemberIdInUrl(safePath, session.recordId).valid) {
|
|
72
90
|
return new Response('Forbidden: Invalid user ID in URL', { status: 403 });
|
|
73
91
|
}
|
|
74
|
-
|
|
92
|
+
locals.recordId = session.recordId;
|
|
75
93
|
return addSecurityHeaders(await next(), nonce);
|
|
76
94
|
});
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAG/C,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE;QACR,GAAG,CAAC,EAAE;YACJ,gBAAgB,CAAC,EAAE,MAAM,CAAC;SAC3B,CAAC;KACH,CAAC;CACH;AAGD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,GAAG,CAAC;QACZ,UAAU,MAAO,SAAQ,iBAAiB;SAAG;KAC9C;CACF;AAED,MAAM,WAAW,uBAAuB;IACtC,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,qBAAqB,GAAG,iBAAiB,CAAC"}
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mindfulauth/core",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Mindful Auth core authentication library for Astro",
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
|
+
"description": "Mindful Auth core authentication library for Astro 6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -42,10 +42,11 @@
|
|
|
42
42
|
"author": "Mindful Auth",
|
|
43
43
|
"license": "MIT",
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"astro": "^
|
|
45
|
+
"astro": "^6.0.0-beta.14"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"
|
|
48
|
+
"@cloudflare/workers-types": "^4.20260303.0",
|
|
49
|
+
"astro": "^6.0.0-beta.14",
|
|
49
50
|
"typescript": "^5.9.3"
|
|
50
51
|
}
|
|
51
|
-
}
|
|
52
|
+
}
|