@openape/nuxt-auth-sp 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2025 Patrick Hofmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @openape/nuxt-auth-sp
2
+
3
+ OpenAPE Service Provider Nuxt module — adds OIDC login via DNS-based IdP discovery.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @openape/nuxt-auth-sp
9
+ ```
10
+
11
+ ## License
12
+
13
+ MIT
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,12 @@
1
+ import * as nuxt_schema from 'nuxt/schema';
2
+
3
+ interface ModuleOptions {
4
+ spId: string;
5
+ spName: string;
6
+ sessionSecret: string;
7
+ openapeUrl: string;
8
+ }
9
+ declare const _default: nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
10
+
11
+ export { _default as default };
12
+ export type { ModuleOptions };
@@ -0,0 +1,12 @@
1
+ import * as nuxt_schema from 'nuxt/schema';
2
+
3
+ interface ModuleOptions {
4
+ spId: string;
5
+ spName: string;
6
+ sessionSecret: string;
7
+ openapeUrl: string;
8
+ }
9
+ declare const _default: nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
10
+
11
+ export { _default as default };
12
+ export type { ModuleOptions };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@openape/nuxt-auth-sp",
3
+ "configKey": "openapeSp",
4
+ "version": "0.1.0",
5
+ "builder": {
6
+ "@nuxt/module-builder": "0.8.4",
7
+ "unbuild": "unknown"
8
+ }
9
+ }
@@ -0,0 +1,31 @@
1
+ import { defineNuxtModule, createResolver, addServerImportsDir, addImportsDir, addServerHandler } from '@nuxt/kit';
2
+ import { defu } from 'defu';
3
+
4
+ const module = defineNuxtModule({
5
+ meta: {
6
+ name: "@openape/nuxt-auth-sp",
7
+ configKey: "openapeSp"
8
+ },
9
+ defaults: {
10
+ spId: "",
11
+ spName: "OpenAPE Service Provider",
12
+ sessionSecret: "change-me-sp-secret-at-least-32-chars-long",
13
+ openapeUrl: ""
14
+ },
15
+ setup(options, nuxt) {
16
+ const { resolve } = createResolver(import.meta.url);
17
+ nuxt.options.runtimeConfig.openapeSp = defu(
18
+ nuxt.options.runtimeConfig.openapeSp || {},
19
+ options
20
+ );
21
+ addServerImportsDir(resolve("./runtime/server/utils"));
22
+ addImportsDir(resolve("./runtime/composables"));
23
+ addServerHandler({ route: "/api/login", method: "post", handler: resolve("./runtime/server/api/login.post") });
24
+ addServerHandler({ route: "/api/callback", handler: resolve("./runtime/server/api/callback.get") });
25
+ addServerHandler({ route: "/api/logout", method: "post", handler: resolve("./runtime/server/api/logout.post") });
26
+ addServerHandler({ route: "/api/me", handler: resolve("./runtime/server/api/me.get") });
27
+ addServerHandler({ route: "/.well-known/sp-manifest.json", handler: resolve("./runtime/server/routes/well-known/sp-manifest.json.get") });
28
+ }
29
+ });
30
+
31
+ export { module as default };
File without changes
@@ -0,0 +1,26 @@
1
+ import { useState, navigateTo } from "#imports";
2
+ export function useOpenApeAuth() {
3
+ const user = useState("openape-user", () => null);
4
+ const loading = useState("openape-auth-loading", () => true);
5
+ async function fetchUser() {
6
+ try {
7
+ user.value = await $fetch("/api/me");
8
+ } catch {
9
+ user.value = null;
10
+ }
11
+ loading.value = false;
12
+ }
13
+ async function login(email) {
14
+ const { redirectUrl } = await $fetch("/api/login", {
15
+ method: "POST",
16
+ body: { email }
17
+ });
18
+ navigateTo(redirectUrl, { external: true });
19
+ }
20
+ async function logout() {
21
+ await $fetch("/api/logout", { method: "POST" });
22
+ user.value = null;
23
+ navigateTo("/");
24
+ }
25
+ return { user, loading, fetchUser, login, logout };
26
+ }
File without changes
@@ -0,0 +1,6 @@
1
+ import { defineNuxtRouteMiddleware, navigateTo } from "#imports";
2
+ export default defineNuxtRouteMiddleware(async () => {
3
+ const { user, fetchUser, loading } = useOpenApeAuth();
4
+ if (loading.value) await fetchUser();
5
+ if (!user.value) return navigateTo("/");
6
+ });
File without changes
@@ -0,0 +1,38 @@
1
+ import { handleCallback } from "@openape/auth";
2
+ export default defineEventHandler(async (event) => {
3
+ const query = getQuery(event);
4
+ const { code, state, error, error_description } = query;
5
+ const { spId } = getSpConfig();
6
+ const origin = getRequestURL(event).origin;
7
+ const redirectUri = `${origin}/api/callback`;
8
+ if (error) {
9
+ const msg = error_description || error;
10
+ return sendRedirect(event, `/?error=${encodeURIComponent(msg)}`);
11
+ }
12
+ if (!code || !state) {
13
+ return sendRedirect(event, `/?error=${encodeURIComponent("Missing code or state parameter")}`);
14
+ }
15
+ const flowState = await getFlowState(event, state);
16
+ if (!flowState) {
17
+ return sendRedirect(event, `/?error=${encodeURIComponent("Invalid or expired state \u2014 please try again")}`);
18
+ }
19
+ try {
20
+ const result = await handleCallback({
21
+ code,
22
+ state,
23
+ flowState,
24
+ spId,
25
+ redirectUri
26
+ });
27
+ clearFlowState(event);
28
+ const session = await getSpSession(event);
29
+ await session.update({
30
+ claims: result.claims
31
+ });
32
+ return sendRedirect(event, "/dashboard");
33
+ } catch (err) {
34
+ clearFlowState(event);
35
+ const message = err instanceof Error ? err.message : "Callback processing failed";
36
+ return sendRedirect(event, `/?error=${encodeURIComponent(message)}`);
37
+ }
38
+ });
File without changes
@@ -0,0 +1,31 @@
1
+ import { createAuthorizationURL, discoverIdP } from "@openape/auth";
2
+ export default defineEventHandler(async (event) => {
3
+ const body = await readBody(event);
4
+ const { spId, openapeUrl } = getSpConfig();
5
+ const origin = getRequestURL(event).origin;
6
+ const redirectUri = `${origin}/api/callback`;
7
+ if (!body?.email || !body.email.includes("@")) {
8
+ throw createError({ statusCode: 400, statusMessage: "Valid email required" });
9
+ }
10
+ const email = body.email.trim();
11
+ const domain = email.split("@")[1];
12
+ let idpConfig;
13
+ if (openapeUrl) {
14
+ idpConfig = { idpUrl: openapeUrl, record: { idp: openapeUrl } };
15
+ } else {
16
+ idpConfig = await discoverIdP(email);
17
+ }
18
+ if (!idpConfig) {
19
+ throw createError({
20
+ statusCode: 404,
21
+ statusMessage: `No DDISA IdP found for domain "${domain}"`
22
+ });
23
+ }
24
+ const { url, flowState } = await createAuthorizationURL(idpConfig, {
25
+ spId,
26
+ redirectUri,
27
+ email
28
+ });
29
+ await saveFlowState(event, flowState.state, flowState);
30
+ return { redirectUrl: url };
31
+ });
File without changes
@@ -0,0 +1,5 @@
1
+ export default defineEventHandler(async (event) => {
2
+ const session = await getSpSession(event);
3
+ await session.clear();
4
+ return { ok: true };
5
+ });
File without changes
@@ -0,0 +1,8 @@
1
+ export default defineEventHandler(async (event) => {
2
+ const session = await getSpSession(event);
3
+ const data = session.data;
4
+ if (!data.claims) {
5
+ throw createError({ statusCode: 401, statusMessage: "Not authenticated" });
6
+ }
7
+ return data.claims;
8
+ });
@@ -0,0 +1,11 @@
1
+ import { createSPManifest } from "@openape/auth";
2
+ export default defineEventHandler((event) => {
3
+ const { spId, spName } = getSpConfig();
4
+ const origin = getRequestURL(event).origin;
5
+ return createSPManifest({
6
+ sp_id: spId,
7
+ name: spName,
8
+ redirect_uris: [`${origin}/api/callback`],
9
+ description: `${spName} \u2014 OpenAPE Service Provider`
10
+ });
11
+ });
File without changes
@@ -0,0 +1,42 @@
1
+ const FLOW_COOKIE = "openape-flow";
2
+ export function getSpConfig() {
3
+ const config = useRuntimeConfig();
4
+ return {
5
+ spId: config.openapeSp.spId || "sp.example.com",
6
+ openapeUrl: config.openapeSp.openapeUrl || "",
7
+ spName: config.openapeSp.spName || "OpenAPE Service Provider"
8
+ };
9
+ }
10
+ export async function saveFlowState(event, state, flow) {
11
+ const config = useRuntimeConfig();
12
+ const session = await useSession(event, {
13
+ name: FLOW_COOKIE,
14
+ password: config.openapeSp.sessionSecret,
15
+ maxAge: 600
16
+ });
17
+ await session.update({
18
+ state,
19
+ flow,
20
+ exp: Date.now() + 10 * 60 * 1e3
21
+ });
22
+ }
23
+ export async function getFlowState(event, expectedState) {
24
+ const config = useRuntimeConfig();
25
+ const session = await useSession(event, {
26
+ name: FLOW_COOKIE,
27
+ password: config.openapeSp.sessionSecret
28
+ });
29
+ const data = session.data;
30
+ if (!data?.state) return null;
31
+ if (data.state !== expectedState) return null;
32
+ if (data.exp < Date.now()) return null;
33
+ return data.flow;
34
+ }
35
+ export async function clearFlowState(event) {
36
+ const config = useRuntimeConfig();
37
+ const session = await useSession(event, {
38
+ name: FLOW_COOKIE,
39
+ password: config.openapeSp.sessionSecret
40
+ });
41
+ await session.clear();
42
+ }
File without changes
@@ -0,0 +1,7 @@
1
+ export async function getSpSession(event) {
2
+ const config = useRuntimeConfig();
3
+ return await useSession(event, {
4
+ name: "openape-sp",
5
+ password: config.openapeSp.sessionSecret
6
+ });
7
+ }
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module.js'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { default } from './module.js'
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { default } from './module'
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@openape/nuxt-auth-sp",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "OpenAPE Service Provider Nuxt module — adds OIDC login via DNS-based IdP discovery",
6
+ "author": "Patrick Hofmann",
7
+ "license": "MIT",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types.d.mts",
11
+ "import": "./dist/module.mjs"
12
+ }
13
+ },
14
+ "main": "./dist/module.mjs",
15
+ "types": "./dist/types.d.mts",
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "nuxt-module-build build",
21
+ "dev": "nuxt-module-build build --stub",
22
+ "typecheck": "tsc --noEmit"
23
+ },
24
+ "dependencies": {
25
+ "@nuxt/kit": "^3.16.0",
26
+ "@openape/auth": "^0.1.0",
27
+ "@openape/core": "^0.1.0",
28
+ "defu": "^6.1.4"
29
+ },
30
+ "peerDependencies": {
31
+ "nuxt": "^4.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@nuxt/module-builder": "^0.8.4",
35
+ "nuxt": "^4.3.1",
36
+ "typescript": "^5.7.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/openape-ai/nuxt-auth-sp.git"
44
+ }
45
+ }