aaspai-authx 0.0.1 → 0.0.3

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/dist/index.d.cts CHANGED
@@ -1303,7 +1303,7 @@ declare const AuthXSessionDecorator: (...dataOrPipes: unknown[]) => ParameterDec
1303
1303
  */
1304
1304
  declare class AuthXStrategy extends Strategy {
1305
1305
  name: string;
1306
- authenticate(req: Request): void;
1306
+ authenticate(req: Request): Promise<void>;
1307
1307
  }
1308
1308
  /**
1309
1309
  * Factory function to create AuthXStrategy instance
package/dist/index.d.ts CHANGED
@@ -1303,7 +1303,7 @@ declare const AuthXSessionDecorator: (...dataOrPipes: unknown[]) => ParameterDec
1303
1303
  */
1304
1304
  declare class AuthXStrategy extends Strategy {
1305
1305
  name: string;
1306
- authenticate(req: Request): void;
1306
+ authenticate(req: Request): Promise<void>;
1307
1307
  }
1308
1308
  /**
1309
1309
  * Factory function to create AuthXStrategy instance
package/dist/index.js CHANGED
@@ -270,7 +270,9 @@ function requireAuth() {
270
270
  try {
271
271
  const apiKey = req.headers["x-api-key"] || req.headers["x-apikey"];
272
272
  const userId = req.headers["x-user-id"] || req.headers["x-userId"];
273
+ console.log(apiKey, "apikey", userId, "userId");
273
274
  if (apiKey) {
275
+ console.log("inside apikey");
274
276
  if (apiKey !== process.env.SERVER_API_KEY) {
275
277
  return res.status(401).json({ error: "Invalid API key" });
276
278
  }
@@ -281,26 +283,28 @@ function requireAuth() {
281
283
  if (!user) {
282
284
  return res.status(401).json({ error: "User not found" });
283
285
  }
284
- const session2 = buildSession({
286
+ const session = buildSession({
285
287
  sub: user.id.toString(),
286
288
  email: user.email,
287
289
  roles: user.roles || []
288
290
  });
289
- session2.authType = "api-key";
290
- session2.projectId = readProjectId(req) || user.projectId || void 0;
291
- req.user = session2;
291
+ session.authType = "api-key";
292
+ session.projectId = readProjectId(req) || user.projectId || void 0;
293
+ req.user = session;
292
294
  return next();
295
+ } else {
296
+ console.log("inside token");
297
+ const token = extractToken(req);
298
+ if (!token) {
299
+ return res.status(401).json({ error: "Missing token" });
300
+ }
301
+ const claims = await verifyJwt(token);
302
+ const session = buildSession(claims);
303
+ const pid = readProjectId(req);
304
+ if (pid) session.projectId = pid;
305
+ req.user = session;
306
+ next();
293
307
  }
294
- const token = extractToken(req);
295
- if (!token) {
296
- return res.status(401).json({ error: "Missing token" });
297
- }
298
- const claims = await verifyJwt(token);
299
- const session = buildSession(claims);
300
- const pid = readProjectId(req);
301
- if (pid) session.projectId = pid;
302
- req.user = session;
303
- next();
304
308
  } catch (e) {
305
309
  res.status(401).json({ error: e?.message || "Unauthorized" });
306
310
  }
@@ -313,7 +317,6 @@ function authorize(roles = []) {
313
317
  if (!user) {
314
318
  return res.status(401).json({ error: "Unauthorized" });
315
319
  }
316
- console.log(user, "user");
317
320
  const have = new Set((user.roles || []).map(String));
318
321
  const ok = roles.some((r) => have.has(r));
319
322
  if (!ok) {
@@ -1941,27 +1944,44 @@ var AuthXSessionDecorator = createParamDecorator(
1941
1944
  import { Strategy } from "passport";
1942
1945
  var AuthXStrategy = class extends Strategy {
1943
1946
  name = "authx";
1944
- authenticate(req) {
1947
+ async authenticate(req) {
1945
1948
  try {
1946
- console.log("AuthXStrategy.authenticate - starting");
1947
- const token = extractToken(req);
1948
- console.log("AuthXStrategy.authenticate - token extracted:", token ? "yes" : "no");
1949
- if (!token) {
1950
- console.log("AuthXStrategy.authenticate - no token, failing");
1951
- return this.fail({ message: "Missing token" }, 401);
1952
- }
1953
- console.log("AuthXStrategy.authenticate - verifying JWT");
1954
- verifyJwt(token).then((claims) => {
1955
- console.log("AuthXStrategy.authenticate - JWT verified successfully");
1956
- const session = buildSession(claims);
1949
+ const apiKey = req.headers["x-api-key"];
1950
+ const userId = req.headers["x-user-id"];
1951
+ if (apiKey) {
1952
+ if (apiKey !== process.env.SERVER_API_KEY) {
1953
+ return this.fail({ message: "Invalid API key" }, 401);
1954
+ }
1955
+ if (!userId) {
1956
+ return this.fail({ message: "User Id is required" }, 401);
1957
+ }
1958
+ const user = await OrgUser.findOne({
1959
+ id: userId,
1960
+ orgId: process.env.ORG_ID || null
1961
+ });
1962
+ if (!user) {
1963
+ return this.fail({ message: "User not found" }, 401);
1964
+ }
1965
+ const session = buildSession(user);
1957
1966
  req.user = session;
1958
1967
  return this.success(session);
1959
- }).catch((error) => {
1960
- console.log("AuthXStrategy.authenticate - JWT verification failed:", error?.message || error);
1961
- return this.fail({ message: error?.message || "Unauthorized" }, 401);
1962
- });
1968
+ } else {
1969
+ const token = extractToken(req);
1970
+ if (!token) {
1971
+ return this.fail({ message: "Missing token" }, 401);
1972
+ }
1973
+ verifyJwt(token).then((claims) => {
1974
+ const session = buildSession(claims);
1975
+ req.user = session;
1976
+ return this.success(session);
1977
+ }).catch((error) => {
1978
+ return this.fail(
1979
+ { message: error?.message || "Unauthorized" },
1980
+ 401
1981
+ );
1982
+ });
1983
+ }
1963
1984
  } catch (error) {
1964
- console.log("AuthXStrategy.authenticate - exception caught:", error?.message || error);
1965
1985
  return this.fail({ message: error?.message || "Unauthorized" }, 401);
1966
1986
  }
1967
1987
  }