authfyio-fastify 0.2.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ # authfyio-fastify
2
+
3
+ Fastify plugin for Authfyio — adds `request.auth`, verifies session JWTs, and exposes a decorator guard.
4
+
5
+ > Part of [Authfyio](https://authfyio.com) — a self-hostable authentication platform.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install authfyio-fastify
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ See the full guide at **https://authfyio.com/docs**.
16
+
17
+ Point the SDK at your Authfyio instance via the same-origin proxy (`/api/af`) or set `AF_API_BASE_URL` for server-side calls.
18
+
19
+ ## License
20
+
21
+ MIT
@@ -0,0 +1,30 @@
1
+ import { type AuthfyioBackendClientOptions, type SessionClaims } from 'authfyio-backend';
2
+ export type { SessionClaims };
3
+ export type FastifyAuthObject = {
4
+ userId: string;
5
+ sessionId: string;
6
+ environmentId: string;
7
+ orgId: string | null;
8
+ orgRole: string | null;
9
+ claims: SessionClaims;
10
+ getToken(): string;
11
+ };
12
+ export type AuthfyioPluginOptions = AuthfyioBackendClientOptions & {
13
+ /**
14
+ * Require a valid session on every request reaching a route that uses
15
+ * the plugin's `protect` preHandler. Default true.
16
+ */
17
+ required?: boolean;
18
+ };
19
+ /**
20
+ * Fastify plugin — decorates the app with `verifyAuth` + `protect` and
21
+ * resolves `request.auth` before your handlers. Designed to work with
22
+ * `@fastify/cookie` already registered.
23
+ *
24
+ * import fp from 'fastify-plugin';
25
+ * import authfyio from 'authfyio-fastify';
26
+ * app.register(authfyio, { baseUrl: process.env.AF_API_BASE_URL! });
27
+ * app.get('/private', { preHandler: [app.protect] }, (req) => req.auth.userId);
28
+ */
29
+ export default function authfyioPlugin(app: any, opts: AuthfyioPluginOptions): Promise<void>;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,4BAA4B,EACjC,KAAK,aAAa,EACnB,MAAM,kBAAkB,CAAC;AAE1B,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,MAAM,EAAE,aAAa,CAAC;IACtB,QAAQ,IAAI,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,4BAA4B,GAAG;IACjE;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAA8B,cAAc,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,qBAAqB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAqCf"}
package/dist/index.js ADDED
@@ -0,0 +1,59 @@
1
+ import { AuthfyioBackendClient, } from 'authfyio-backend';
2
+ /**
3
+ * Fastify plugin — decorates the app with `verifyAuth` + `protect` and
4
+ * resolves `request.auth` before your handlers. Designed to work with
5
+ * `@fastify/cookie` already registered.
6
+ *
7
+ * import fp from 'fastify-plugin';
8
+ * import authfyio from 'authfyio-fastify';
9
+ * app.register(authfyio, { baseUrl: process.env.AF_API_BASE_URL! });
10
+ * app.get('/private', { preHandler: [app.protect] }, (req) => req.auth.userId);
11
+ */
12
+ export default async function authfyioPlugin(app, opts) {
13
+ const client = new AuthfyioBackendClient(opts);
14
+ app.decorateRequest('auth', null);
15
+ async function verifyAuth(req) {
16
+ const cookie = req.headers?.cookie ?? '';
17
+ const claims = await client.getSessionFromRequest({ headers: { cookie } }).catch(() => null);
18
+ if (!claims)
19
+ return null;
20
+ const token = extractSessionCookie(cookie);
21
+ return {
22
+ userId: claims.sub,
23
+ sessionId: claims.sid,
24
+ environmentId: claims.env,
25
+ orgId: claims.org ?? null,
26
+ orgRole: claims.org_role ?? null,
27
+ claims,
28
+ getToken: () => token ?? '',
29
+ };
30
+ }
31
+ app.decorate('verifyAuth', verifyAuth);
32
+ app.decorate('protect', async function protect(req, reply) {
33
+ const auth = await verifyAuth(req);
34
+ if (!auth) {
35
+ reply.code(401).send({ error: 'unauthorized' });
36
+ return reply;
37
+ }
38
+ req.auth = auth;
39
+ });
40
+ app.addHook('onRequest', async (req) => {
41
+ // Populate request.auth opportunistically; handlers that required it
42
+ // should still use the `protect` preHandler to enforce.
43
+ req.auth = await verifyAuth(req);
44
+ });
45
+ }
46
+ function extractSessionCookie(cookieHeader) {
47
+ if (!cookieHeader)
48
+ return null;
49
+ for (const pair of cookieHeader.split(';')) {
50
+ const eq = pair.indexOf('=');
51
+ if (eq < 0)
52
+ continue;
53
+ const k = pair.slice(0, eq).trim();
54
+ const v = pair.slice(eq + 1).trim();
55
+ if (k === '__session')
56
+ return decodeURIComponent(v);
57
+ }
58
+ return null;
59
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "authfyio-fastify",
3
+ "version": "0.2.1",
4
+ "description": "Fastify plugin for Authfyio — adds `request.auth`, verifies session JWTs, and exposes a decorator guard.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.build.json",
21
+ "typecheck": "tsc -p tsconfig.build.json --noEmit",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "peerDependencies": {
25
+ "authfyio-backend": "^0.2.0",
26
+ "fastify": ">=4"
27
+ },
28
+ "keywords": [
29
+ "authfyio",
30
+ "auth",
31
+ "fastify",
32
+ "plugin"
33
+ ],
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "homepage": "https://authfyio.com/docs",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/authfyio/authfyio.git"
41
+ }
42
+ }