@altafusion/fugue-simple-auth 0.0.0-stage → 0.3.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.
- package/CHANGELOG.md +15 -0
- package/DESIGN.md +67 -0
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/app/middleware/auth.global.ts +29 -0
- package/app/pages/login.vue +38 -0
- package/components/SimpleAuthBrandLogo.vue +26 -0
- package/components/SimpleAuthLoginForm.vue +176 -0
- package/composables/useAppBrand.ts +23 -0
- package/composables/useSimpleAuthLogin.ts +59 -0
- package/composables/useSimpleAuthSession.ts +19 -0
- package/composables/useSimpleAuthSignOut.ts +21 -0
- package/eslint.config.mjs +6 -0
- package/lib/env.ts +61 -0
- package/lib/login.ts +67 -0
- package/lib/path-gate.ts +48 -0
- package/lib/session.ts +32 -0
- package/modules/simple-auth.ts +237 -0
- package/nuxt.config.ts +42 -0
- package/package.json +57 -3
- package/server/api/auth/login.post.ts +22 -0
- package/server/api/auth/logout.post.ts +6 -0
- package/server/api/auth/session.get.ts +12 -0
- package/server/middleware/auth.ts +25 -0
- package/server/utils/access-key.ts +12 -0
- package/server/utils/config.ts +5 -0
- package/server/utils/session.ts +29 -0
- package/types/app-brand.ts +22 -0
- package/types/config.ts +34 -0
- package/types/nuxt-fugue-simple-auth.d.ts +24 -0
package/lib/login.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export interface SimpleAuthSession {
|
|
2
|
+
authenticated: boolean
|
|
3
|
+
required: boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* After the access-key gate, send editors to a public page — not `/_studio`.
|
|
8
|
+
* Studio OAuth is started from the header **Edit** control.
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeAuthGateLoginRedirect(targetPath: string, fallback = '/'): string {
|
|
11
|
+
if (!targetPath.startsWith('/'))
|
|
12
|
+
return fallback
|
|
13
|
+
|
|
14
|
+
const queryIndex = targetPath.indexOf('?')
|
|
15
|
+
const pathname = queryIndex === -1
|
|
16
|
+
? targetPath
|
|
17
|
+
: targetPath.slice(0, queryIndex)
|
|
18
|
+
const search = queryIndex === -1
|
|
19
|
+
? ''
|
|
20
|
+
: targetPath.slice(queryIndex + 1)
|
|
21
|
+
|
|
22
|
+
if (pathname.startsWith('/__nuxt_studio/'))
|
|
23
|
+
return fallback
|
|
24
|
+
|
|
25
|
+
if (pathname !== '/_studio' && !pathname.startsWith('/_studio/'))
|
|
26
|
+
return targetPath
|
|
27
|
+
|
|
28
|
+
if (search) {
|
|
29
|
+
const inner = new URLSearchParams(search).get('redirect')
|
|
30
|
+
if (inner?.startsWith('/'))
|
|
31
|
+
return inner
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return fallback
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveSimpleAuthRedirect(
|
|
38
|
+
redirectQuery: unknown,
|
|
39
|
+
fallback = '/'
|
|
40
|
+
): string {
|
|
41
|
+
if (typeof redirectQuery === 'string' && redirectQuery.startsWith('/'))
|
|
42
|
+
return normalizeAuthGateLoginRedirect(redirectQuery, fallback)
|
|
43
|
+
|
|
44
|
+
return fallback
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function shouldShowSimpleAuthLogin(session: SimpleAuthSession | null | undefined): boolean {
|
|
48
|
+
return Boolean(session?.required && !session?.authenticated)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function shouldShowSimpleAuthSignOut(session: SimpleAuthSession | null | undefined): boolean {
|
|
52
|
+
return Boolean(session?.required && session?.authenticated)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* POST /api/auth/logout to clear orphan peek-gate cookies when the session API
|
|
57
|
+
* reports they are not active (invalid token, gate off at runtime, or anonymous).
|
|
58
|
+
*/
|
|
59
|
+
export function shouldClearStaleAuthSession(session: SimpleAuthSession | null | undefined): boolean {
|
|
60
|
+
if (!session)
|
|
61
|
+
return false
|
|
62
|
+
|
|
63
|
+
if (!session.required)
|
|
64
|
+
return true
|
|
65
|
+
|
|
66
|
+
return !session.authenticated
|
|
67
|
+
}
|
package/lib/path-gate.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { FugueSimpleAuthConfig } from '../types/config'
|
|
2
|
+
|
|
3
|
+
function normalizePrefix(prefix: string): string {
|
|
4
|
+
return prefix.endsWith('/')
|
|
5
|
+
? prefix
|
|
6
|
+
: `${prefix}/`
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function matchesProtectedRoute(pathname: string, pattern: string): boolean {
|
|
10
|
+
if (pattern.endsWith('/**')) {
|
|
11
|
+
const base = pattern.slice(0, -3)
|
|
12
|
+
return pathname === base || pathname.startsWith(`${base}/`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return pathname === pattern
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isAssetOrAuthPath(pathname: string, config: FugueSimpleAuthConfig): boolean {
|
|
19
|
+
if (config.publicExact.includes(pathname))
|
|
20
|
+
return true
|
|
21
|
+
|
|
22
|
+
if (config.publicPaths.includes(pathname))
|
|
23
|
+
return true
|
|
24
|
+
|
|
25
|
+
if (pathname === config.loginPath)
|
|
26
|
+
return true
|
|
27
|
+
|
|
28
|
+
const apiPrefix = normalizePrefix(config.apiPrefix)
|
|
29
|
+
if (pathname.startsWith(apiPrefix) || pathname === config.apiPrefix)
|
|
30
|
+
return true
|
|
31
|
+
|
|
32
|
+
return config.publicPrefixes.some(prefix => pathname.startsWith(prefix))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Returns true when the path is reachable without authentication. */
|
|
36
|
+
export function isPathPublic(pathname: string, config: FugueSimpleAuthConfig): boolean {
|
|
37
|
+
if (isAssetOrAuthPath(pathname, config))
|
|
38
|
+
return true
|
|
39
|
+
|
|
40
|
+
if (config.mode === 'full')
|
|
41
|
+
return false
|
|
42
|
+
|
|
43
|
+
return !config.protectedRoutes.some(pattern => matchesProtectedRoute(pathname, pattern))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function shouldGatePath(pathname: string, config: FugueSimpleAuthConfig): boolean {
|
|
47
|
+
return !isPathPublic(pathname, config)
|
|
48
|
+
}
|
package/lib/session.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
import { createHmac, timingSafeEqual } from 'node:crypto'
|
|
3
|
+
|
|
4
|
+
export function keysMatch(provided: string, expected: string): boolean {
|
|
5
|
+
const providedBuffer = Buffer.from(provided.trim())
|
|
6
|
+
const expectedBuffer = Buffer.from(expected.trim())
|
|
7
|
+
|
|
8
|
+
if (providedBuffer.length !== expectedBuffer.length)
|
|
9
|
+
return false
|
|
10
|
+
|
|
11
|
+
return timingSafeEqual(providedBuffer, expectedBuffer)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createSessionToken(accessKey: string, sessionLabel: string): string {
|
|
15
|
+
return createHmac('sha256', accessKey)
|
|
16
|
+
.update(sessionLabel)
|
|
17
|
+
.digest('base64url')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function verifySessionToken(
|
|
21
|
+
token: string,
|
|
22
|
+
accessKey: string,
|
|
23
|
+
sessionLabel: string
|
|
24
|
+
): boolean {
|
|
25
|
+
const expected = createSessionToken(accessKey, sessionLabel)
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
return timingSafeEqual(Buffer.from(token), Buffer.from(expected))
|
|
29
|
+
} catch {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { Nuxt } from '@nuxt/schema'
|
|
2
|
+
import process from 'node:process'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import { addComponent, addImports, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
|
|
5
|
+
import { defu } from 'defu'
|
|
6
|
+
import { isAuthEnabledAtBuild } from '../lib/env'
|
|
7
|
+
import { fugueSimpleAuthDefaults } from '../types/config'
|
|
8
|
+
|
|
9
|
+
const layerRoot = fileURLToPath(new URL('..', import.meta.url))
|
|
10
|
+
const resolver = createResolver(import.meta.url)
|
|
11
|
+
|
|
12
|
+
/** Netlify sets NETLIFY; NETLIFY_CI covers some CI/build-runner paths. */
|
|
13
|
+
function isNetlifyBuild(): boolean {
|
|
14
|
+
return Boolean(process.env.NETLIFY || process.env.NETLIFY_CI)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `nuxt generate` — static export; no Netlify server function is deployed. */
|
|
18
|
+
function isNuxtGenerate(): boolean {
|
|
19
|
+
return process.argv.includes('generate')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** SSR auth gate — requires `nuxt build`, not `nuxt generate`. */
|
|
23
|
+
function useNetlifySsrAuth(authEnabled: boolean): boolean {
|
|
24
|
+
return authEnabled && !isNuxtGenerate()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Bundle these in the function — externalized vue omits server-renderer (nuxt/nuxt#30224). */
|
|
28
|
+
const VUE_SSR_INLINE = ['vue', '@vue/server-renderer', 'vue/server-renderer'] as const
|
|
29
|
+
|
|
30
|
+
interface NitroExternals { inline?: (string | RegExp)[] }
|
|
31
|
+
|
|
32
|
+
function mergeInlineExternals(
|
|
33
|
+
externals: NitroExternals | undefined,
|
|
34
|
+
additions: readonly string[]
|
|
35
|
+
): NitroExternals {
|
|
36
|
+
const existing = externals?.inline ?? []
|
|
37
|
+
const inline = [
|
|
38
|
+
...existing,
|
|
39
|
+
...additions
|
|
40
|
+
]
|
|
41
|
+
return {
|
|
42
|
+
...externals,
|
|
43
|
+
inline: [
|
|
44
|
+
...new Set(inline)
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function configureNetlifySsr(nuxt: Nuxt) {
|
|
50
|
+
// Nuxt 4 defaults externalVue: true, which externalizes vue without server-renderer subpath.
|
|
51
|
+
nuxt.options.experimental.externalVue = false
|
|
52
|
+
nuxt.options.nitro.preset = 'netlify'
|
|
53
|
+
nuxt.options.nitro.externals = mergeInlineExternals(
|
|
54
|
+
nuxt.options.nitro.externals as NitroExternals | undefined,
|
|
55
|
+
VUE_SSR_INLINE
|
|
56
|
+
) as typeof nuxt.options.nitro.externals
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function authModeStartupMessage(config: typeof fugueSimpleAuthDefaults): string {
|
|
60
|
+
if (config.mode === 'full')
|
|
61
|
+
return 'Mode: full (whole site gated when access key is set)'
|
|
62
|
+
|
|
63
|
+
if (config.protectedRoutes.length)
|
|
64
|
+
return `Mode: routes (public site open; gated: ${config.protectedRoutes.join(', ')})`
|
|
65
|
+
|
|
66
|
+
return 'Mode: routes (public site open; no protected routes configured)'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export default defineNuxtModule({
|
|
70
|
+
meta: {
|
|
71
|
+
name: '@altafusion/fugue-simple-auth',
|
|
72
|
+
configKey: 'fugueSimpleAuth'
|
|
73
|
+
},
|
|
74
|
+
defaults: fugueSimpleAuthDefaults,
|
|
75
|
+
setup(options, nuxt) {
|
|
76
|
+
nuxt.options.alias = defu(nuxt.options.alias, {
|
|
77
|
+
'#simple-auth': layerRoot
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
nuxt.options.nitro = defu(nuxt.options.nitro, {
|
|
81
|
+
alias: {
|
|
82
|
+
'#simple-auth': layerRoot
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
addImports([
|
|
87
|
+
{
|
|
88
|
+
name: 'useSimpleAuthLogin',
|
|
89
|
+
from: resolver.resolve('../composables/useSimpleAuthLogin')
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'useSimpleAuthSession',
|
|
93
|
+
from: resolver.resolve('../composables/useSimpleAuthSession')
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: 'useSimpleAuthSignOut',
|
|
97
|
+
from: resolver.resolve('../composables/useSimpleAuthSignOut')
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'useAppBrand',
|
|
101
|
+
from: resolver.resolve('../composables/useAppBrand')
|
|
102
|
+
}
|
|
103
|
+
])
|
|
104
|
+
|
|
105
|
+
addComponent({
|
|
106
|
+
name: 'SimpleAuthLoginForm',
|
|
107
|
+
filePath: resolver.resolve('../components/SimpleAuthLoginForm.vue')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
addComponent({
|
|
111
|
+
name: 'SimpleAuthBrandLogo',
|
|
112
|
+
filePath: resolver.resolve('../components/SimpleAuthBrandLogo.vue')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const config = defu(
|
|
116
|
+
nuxt.options.runtimeConfig.public.fugueSimpleAuth,
|
|
117
|
+
options
|
|
118
|
+
) as typeof fugueSimpleAuthDefaults
|
|
119
|
+
|
|
120
|
+
const authEnabled = isAuthEnabledAtBuild(config.envPrefix)
|
|
121
|
+
const ssrAuth = useNetlifySsrAuth(authEnabled)
|
|
122
|
+
config.authRequired = ssrAuth
|
|
123
|
+
nuxt.options.runtimeConfig.public.fugueSimpleAuth = config
|
|
124
|
+
|
|
125
|
+
const logger = useLogger('fugue-simple-auth')
|
|
126
|
+
logger.info(
|
|
127
|
+
authEnabled
|
|
128
|
+
? authModeStartupMessage(config)
|
|
129
|
+
: `Mode: ${config.mode} (inactive — no ${config.envPrefix}_ACCESS_KEY)`
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
if (!authEnabled)
|
|
133
|
+
return
|
|
134
|
+
|
|
135
|
+
if (authEnabled && isNuxtGenerate() && isNetlifyBuild()) {
|
|
136
|
+
nuxt.hooks.hook('build:done', () => {
|
|
137
|
+
logger.warn(
|
|
138
|
+
'Auth env is set but the build used `nuxt generate`. '
|
|
139
|
+
+ 'SSR redirects were skipped — the preview gate is inactive. '
|
|
140
|
+
+ 'Use `nuxt build` with Package directory set for Netlify SSR auth.'
|
|
141
|
+
)
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
nuxt.options.routeRules = defu(nuxt.options.routeRules, {
|
|
146
|
+
[config.loginPath]: { prerender: true },
|
|
147
|
+
...(ssrAuth
|
|
148
|
+
? {
|
|
149
|
+
'/**': { ssr: true, prerender: false }
|
|
150
|
+
}
|
|
151
|
+
: {})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
if (config.mode === 'full') {
|
|
155
|
+
// Nuxt types `robots` as the full module options object. Edit `disallow`
|
|
156
|
+
// through a narrower view so a partial update still typechecks.
|
|
157
|
+
const options = nuxt.options as Omit<typeof nuxt.options, 'robots'> & {
|
|
158
|
+
robots?: { disallow?: string | string[] }
|
|
159
|
+
}
|
|
160
|
+
const current = options.robots?.disallow
|
|
161
|
+
const list = current == null
|
|
162
|
+
? []
|
|
163
|
+
: Array.isArray(current)
|
|
164
|
+
? [...current]
|
|
165
|
+
: [current]
|
|
166
|
+
if (!list.includes('/'))
|
|
167
|
+
list.unshift('/')
|
|
168
|
+
options.robots = {
|
|
169
|
+
...options.robots,
|
|
170
|
+
disallow: list
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
nuxt.options.nitro = defu(nuxt.options.nitro, {})
|
|
175
|
+
if (isNetlifyBuild() && ssrAuth)
|
|
176
|
+
configureNetlifySsr(nuxt)
|
|
177
|
+
|
|
178
|
+
nuxt.options.nitro.prerender = {
|
|
179
|
+
...nuxt.options.nitro.prerender,
|
|
180
|
+
...(ssrAuth
|
|
181
|
+
? { crawlLinks: false }
|
|
182
|
+
: {}),
|
|
183
|
+
routes: [
|
|
184
|
+
...(nuxt.options.nitro.prerender?.routes ?? []),
|
|
185
|
+
config.loginPath
|
|
186
|
+
]
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
nuxt.hooks.hook('nitro:config', (nitroConfig) => {
|
|
190
|
+
nitroConfig.prerender = {
|
|
191
|
+
...nitroConfig.prerender,
|
|
192
|
+
...(ssrAuth
|
|
193
|
+
? { crawlLinks: false }
|
|
194
|
+
: {}),
|
|
195
|
+
routes: [
|
|
196
|
+
...(nitroConfig.prerender?.routes ?? []),
|
|
197
|
+
config.loginPath
|
|
198
|
+
]
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (isNetlifyBuild() && ssrAuth) {
|
|
202
|
+
nitroConfig.preset = nitroConfig.preset ?? 'netlify'
|
|
203
|
+
nitroConfig.externals = mergeInlineExternals(
|
|
204
|
+
nitroConfig.externals as NitroExternals | undefined,
|
|
205
|
+
VUE_SSR_INLINE
|
|
206
|
+
) as typeof nitroConfig.externals
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
if (!ssrAuth)
|
|
211
|
+
return
|
|
212
|
+
|
|
213
|
+
nuxt.hooks.hook('nitro:build:public-assets', async (nitro) => {
|
|
214
|
+
const { readFile, writeFile } = await import('node:fs/promises')
|
|
215
|
+
const { join } = await import('node:path')
|
|
216
|
+
const redirectsPath = join(nitro.options.output.publicDir, '_redirects')
|
|
217
|
+
const catchAll = [
|
|
218
|
+
'/_nuxt/* /_nuxt/:splat 200',
|
|
219
|
+
'/* /.netlify/functions/server 200'
|
|
220
|
+
].join('\n')
|
|
221
|
+
|
|
222
|
+
let existing = ''
|
|
223
|
+
try {
|
|
224
|
+
existing = await readFile(redirectsPath, 'utf8')
|
|
225
|
+
} catch {
|
|
226
|
+
// no existing redirects file
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!/^\/\* /m.test(existing)) {
|
|
230
|
+
const contents = existing.trim()
|
|
231
|
+
? `${existing.trim()}\n${catchAll}\n`
|
|
232
|
+
: `${catchAll}\n`
|
|
233
|
+
await writeFile(redirectsPath, contents)
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
})
|
package/nuxt.config.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @altafusion/fugue-simple-auth — Nuxt layer
|
|
3
|
+
*
|
|
4
|
+
* Optional access-key gate with signed session cookie and Netlify SSR hooks.
|
|
5
|
+
* Requires a parent layer or app to register @nuxt/ui (Docus, shell, or fugue-theme).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/// <reference path="./types/nuxt-fugue-simple-auth.d.ts" />
|
|
9
|
+
|
|
10
|
+
import { fileURLToPath } from 'node:url'
|
|
11
|
+
|
|
12
|
+
export default defineNuxtConfig({
|
|
13
|
+
modules: [
|
|
14
|
+
fileURLToPath(new URL('./modules/simple-auth', import.meta.url))
|
|
15
|
+
],
|
|
16
|
+
|
|
17
|
+
components: [
|
|
18
|
+
{
|
|
19
|
+
path: './components',
|
|
20
|
+
pathPrefix: false
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
|
|
24
|
+
runtimeConfig: {
|
|
25
|
+
public: {
|
|
26
|
+
fugueSimpleAuth: {
|
|
27
|
+
envPrefix: 'APP',
|
|
28
|
+
cookieName: 'fugue-simple-auth',
|
|
29
|
+
sessionLabel: 'fugue-simple-authenticated',
|
|
30
|
+
mode: 'full',
|
|
31
|
+
loginPath: '/login',
|
|
32
|
+
apiPrefix: '/api/auth',
|
|
33
|
+
protectedRoutes: [],
|
|
34
|
+
publicExact: ['/favicon.ico', '/favicon.svg'],
|
|
35
|
+
publicPrefixes: ['/_nuxt/', '/__nuxt/'],
|
|
36
|
+
publicPaths: [],
|
|
37
|
+
loginDescription: 'Enter the access key to continue.',
|
|
38
|
+
authRequired: false
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altafusion/fugue-simple-auth",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"description": "Optional access-key gate for Nuxt apps — session cookie, SSR-safe Netlify deploy",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/altafusion/fugue/tree/main/packages-fugue/fugue-simple-auth#readme",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/altafusion/fugue/issues"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/altafusion/fugue.git",
|
|
14
|
+
"directory": "packages-fugue/fugue-simple-auth"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./nuxt.config.ts",
|
|
21
|
+
"./lib/*": "./lib/*.ts",
|
|
22
|
+
"./composables/*": "./composables/*.ts",
|
|
23
|
+
"./types/*": "./types/*.ts"
|
|
24
|
+
},
|
|
25
|
+
"main": "./nuxt.config.ts",
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@nuxt/ui": "^4.0.0",
|
|
28
|
+
"nuxt": ">=4.0.0",
|
|
29
|
+
"vue": "^3.5.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@nuxt/kit": "^4.6.4",
|
|
33
|
+
"defu": "^6.1.7"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@altafusion/eslint-config": "0.3.0",
|
|
37
|
+
"@altafusion/nuxt-base": "0.3.0",
|
|
38
|
+
"@nuxt/ui": "^4.13.0",
|
|
39
|
+
"@types/node": "^26.6.6",
|
|
40
|
+
"eslint": "^10.15.0",
|
|
41
|
+
"nuxt": "^4.6.4",
|
|
42
|
+
"typescript": "^6.0.3",
|
|
43
|
+
"vitest": "^5.1.1",
|
|
44
|
+
"vue": "^3.5.46"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"**",
|
|
48
|
+
"!**/*.test.ts",
|
|
49
|
+
"!**/.nuxt",
|
|
50
|
+
"!**/.output",
|
|
51
|
+
"!**/.data",
|
|
52
|
+
"!**/coverage",
|
|
53
|
+
"!**/node_modules"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"clean": "rm -rf .output .nuxt node_modules/.cache dist",
|
|
57
|
+
"lint": "eslint .",
|
|
58
|
+
"lint:fix": "eslint . --fix"
|
|
59
|
+
}
|
|
6
60
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { keysMatch } from '#simple-auth/lib/session'
|
|
2
|
+
import { getAccessKey, isAuthRequired } from '../../utils/access-key'
|
|
3
|
+
import { setSessionCookie } from '../../utils/session'
|
|
4
|
+
|
|
5
|
+
export default defineEventHandler(async (event) => {
|
|
6
|
+
if (!isAuthRequired())
|
|
7
|
+
return { authenticated: true }
|
|
8
|
+
|
|
9
|
+
const body = await readBody<{ key?: string }>(event)
|
|
10
|
+
const providedKey = String(body?.key ?? '')
|
|
11
|
+
|
|
12
|
+
if (!keysMatch(providedKey, getAccessKey())) {
|
|
13
|
+
throw createError({
|
|
14
|
+
statusCode: 401,
|
|
15
|
+
statusMessage: 'Invalid access key'
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setSessionCookie(event, getAccessKey())
|
|
20
|
+
|
|
21
|
+
return { authenticated: true }
|
|
22
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getAccessKey, isAuthRequired } from '../../utils/access-key'
|
|
2
|
+
import { isAuthenticated } from '../../utils/session'
|
|
3
|
+
|
|
4
|
+
export default defineEventHandler((event) => {
|
|
5
|
+
if (!isAuthRequired())
|
|
6
|
+
return { authenticated: true, required: false }
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
authenticated: isAuthenticated(event, getAccessKey()),
|
|
10
|
+
required: true
|
|
11
|
+
}
|
|
12
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { normalizeAuthGateLoginRedirect } from '#simple-auth/lib/login'
|
|
2
|
+
import { shouldGatePath } from '#simple-auth/lib/path-gate'
|
|
3
|
+
import { getAccessKey, isAuthRequired } from '../utils/access-key'
|
|
4
|
+
import { getSimpleAuthConfig } from '../utils/config'
|
|
5
|
+
import { isAuthenticated } from '../utils/session'
|
|
6
|
+
|
|
7
|
+
export default defineEventHandler((event) => {
|
|
8
|
+
if (!isAuthRequired())
|
|
9
|
+
return
|
|
10
|
+
|
|
11
|
+
const config = getSimpleAuthConfig()
|
|
12
|
+
const pathname = getRequestURL(event).pathname
|
|
13
|
+
|
|
14
|
+
if (!shouldGatePath(pathname, config))
|
|
15
|
+
return
|
|
16
|
+
|
|
17
|
+
if (isAuthenticated(event, getAccessKey()))
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
const returnTo = normalizeAuthGateLoginRedirect(
|
|
21
|
+
pathname + (getRequestURL(event).search || '')
|
|
22
|
+
)
|
|
23
|
+
const redirect = encodeURIComponent(returnTo)
|
|
24
|
+
return sendRedirect(event, `${config.loginPath}?redirect=${redirect}`, 302)
|
|
25
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { isAccessKeyConfigured, readAccessKey } from '#simple-auth/lib/env'
|
|
2
|
+
import { getSimpleAuthConfig } from './config'
|
|
3
|
+
|
|
4
|
+
export function getAccessKey(): string {
|
|
5
|
+
const { envPrefix } = getSimpleAuthConfig()
|
|
6
|
+
return readAccessKey(envPrefix, import.meta.dev)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isAuthRequired(): boolean {
|
|
10
|
+
const { envPrefix } = getSimpleAuthConfig()
|
|
11
|
+
return isAccessKeyConfigured(envPrefix, import.meta.dev)
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { H3Event } from 'h3'
|
|
2
|
+
import process from 'node:process'
|
|
3
|
+
import { createSessionToken, verifySessionToken } from '#simple-auth/lib/session'
|
|
4
|
+
import { getSimpleAuthConfig } from './config'
|
|
5
|
+
|
|
6
|
+
export function isAuthenticated(event: H3Event, accessKey: string): boolean {
|
|
7
|
+
const { cookieName, sessionLabel } = getSimpleAuthConfig()
|
|
8
|
+
const token = getCookie(event, cookieName)
|
|
9
|
+
if (!token)
|
|
10
|
+
return false
|
|
11
|
+
|
|
12
|
+
return verifySessionToken(token, accessKey, sessionLabel)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function setSessionCookie(event: H3Event, accessKey: string) {
|
|
16
|
+
const { cookieName, sessionLabel } = getSimpleAuthConfig()
|
|
17
|
+
setCookie(event, cookieName, createSessionToken(accessKey, sessionLabel), {
|
|
18
|
+
httpOnly: true,
|
|
19
|
+
secure: process.env.NODE_ENV === 'production',
|
|
20
|
+
sameSite: 'lax',
|
|
21
|
+
maxAge: 60 * 60 * 24 * 30,
|
|
22
|
+
path: '/'
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function clearSessionCookie(event: H3Event) {
|
|
27
|
+
const { cookieName } = getSimpleAuthConfig()
|
|
28
|
+
deleteCookie(event, cookieName, { path: '/' })
|
|
29
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface AppHeaderConfig {
|
|
2
|
+
title?: string
|
|
3
|
+
showColorMode?: boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** Product app branding — set in each app's `app.config.ts`. */
|
|
7
|
+
export interface AppBrandConfig {
|
|
8
|
+
name?: string
|
|
9
|
+
logo?: string
|
|
10
|
+
logoAlt?: string
|
|
11
|
+
/** Extra classes on the logo `<img>` (e.g. `dark:brightness-0 dark:invert`). */
|
|
12
|
+
logoClass?: string
|
|
13
|
+
favicon?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Resolved brand values for login chrome and headers. */
|
|
17
|
+
export interface AppBrand {
|
|
18
|
+
name: string
|
|
19
|
+
logo?: string
|
|
20
|
+
logoAlt: string
|
|
21
|
+
logoClass?: string
|
|
22
|
+
}
|
package/types/config.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type FugueSimpleAuthMode = 'full' | 'routes'
|
|
2
|
+
|
|
3
|
+
export interface FugueSimpleAuthConfig {
|
|
4
|
+
/** Env prefix — e.g. MY_SITE → MY_SITE_ACCESS_KEY */
|
|
5
|
+
envPrefix: string
|
|
6
|
+
cookieName: string
|
|
7
|
+
/** HMAC payload label — unique per app */
|
|
8
|
+
sessionLabel: string
|
|
9
|
+
mode: FugueSimpleAuthMode
|
|
10
|
+
loginPath: string
|
|
11
|
+
apiPrefix: string
|
|
12
|
+
protectedRoutes: string[]
|
|
13
|
+
publicExact: string[]
|
|
14
|
+
publicPrefixes: string[]
|
|
15
|
+
publicPaths: string[]
|
|
16
|
+
loginDescription: string
|
|
17
|
+
/** Set at build time — whether SSR auth preset is active */
|
|
18
|
+
authRequired: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const fugueSimpleAuthDefaults: FugueSimpleAuthConfig = {
|
|
22
|
+
envPrefix: 'APP',
|
|
23
|
+
cookieName: 'fugue-simple-auth',
|
|
24
|
+
sessionLabel: 'fugue-simple-authenticated',
|
|
25
|
+
mode: 'full',
|
|
26
|
+
loginPath: '/login',
|
|
27
|
+
apiPrefix: '/api/auth',
|
|
28
|
+
protectedRoutes: [],
|
|
29
|
+
publicExact: ['/favicon.ico', '/favicon.svg'],
|
|
30
|
+
publicPrefixes: ['/_nuxt/', '/__nuxt/'],
|
|
31
|
+
publicPaths: [],
|
|
32
|
+
loginDescription: 'Enter the access key to continue.',
|
|
33
|
+
authRequired: false
|
|
34
|
+
}
|