@opengis/fastify-table 2.0.56 → 2.0.57

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.
@@ -1,4 +1,6 @@
1
- import { FastifyInstance } from "fastify";
1
+ import { FastifyInstance, FastifyReply } from "fastify";
2
+ import { ExtendedRequest } from "../../types/core.js";
3
+ export declare function onRequest(req: ExtendedRequest, reply: FastifyReply): Promise<null>;
2
4
  declare function plugin(fastify: FastifyInstance): void;
3
5
  export default plugin;
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/plugins/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAkB1C,iBAAS,MAAM,CAAC,OAAO,EAAE,eAAe,QAuIvC;AAED,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/plugins/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAYxD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAMtD,wBAAsB,SAAS,CAAC,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,YAAY,iBA6GxE;AAED,iBAAS,MAAM,CAAC,OAAO,EAAE,eAAe,QA6BvC;AAED,eAAe,MAAM,CAAC"}
@@ -7,6 +7,91 @@ import config from "../../../config.js";
7
7
  import getRedis from "../redis/funcs/getRedis.js";
8
8
  const fastifyPassport = new Authenticator();
9
9
  const { prefix = "/api" } = config;
10
+ export async function onRequest(req, reply) {
11
+ const { hostname, headers, routeOptions } = req;
12
+ const { policy = [] } = routeOptions?.config || {};
13
+ // proxy from old apps to editor, bi etc.
14
+ const validToken = (req.ip === "193.239.152.181" ||
15
+ req.ip === "127.0.0.1" ||
16
+ req.ip?.startsWith?.("192.168.") ||
17
+ config.debug) &&
18
+ req.headers?.token &&
19
+ config.auth?.tokens?.includes?.(headers.token);
20
+ if (validToken && !req?.user?.uid) {
21
+ req.user = {
22
+ uid: req.headers?.uid?.toString?.(),
23
+ user_type: req.headers?.user_type?.toString?.() || "regular",
24
+ };
25
+ }
26
+ const isAdmin = process.env.NODE_ENV === "admin" ||
27
+ hostname?.split?.(":")?.shift?.() === config.adminDomain ||
28
+ config.admin ||
29
+ hostname?.startsWith?.("admin");
30
+ const isPublic = Array.isArray(policy)
31
+ ? policy.includes("public")
32
+ : policy === "L0";
33
+ if (!req.session?.passport?.user?.uid &&
34
+ (config.auth?.disable || config.auth?.user) &&
35
+ !isPublic) {
36
+ req.session = req.session || {};
37
+ req.session.passport = req.session.passport || {}; // ensure passport session exists
38
+ req.session.passport.user = {
39
+ ...(config.auth?.user || {}),
40
+ uid: config.auth?.user?.uid?.toString?.() || "1",
41
+ user_rnokpp: config.auth?.user?.rnokpp,
42
+ user_type: config.auth?.user?.type || "regular",
43
+ };
44
+ req.user = req.session.passport.user;
45
+ }
46
+ // ! intentional: null || undefined > undefined
47
+ req.user = req.user || req.session?.passport?.user || undefined; // fix for user.uid errors, by default user is null, while with express passport it was {}, unauthorized user does not trigger serializer
48
+ // currently 2factor + auth with passwd file not supported
49
+ const ispasswd = (existsSync("passwd") && !config.auth?.["2factor"]) || config.auth?.passwd;
50
+ const loginPageUrl = config.auth?.link?.core?.login || config?.auth?.redirect || "/login";
51
+ if (!req.user?.uid &&
52
+ !config.auth?.disable &&
53
+ isAdmin &&
54
+ !isPublic &&
55
+ !config.auth?.disableRedirect &&
56
+ !req.url.startsWith(prefix) &&
57
+ !req.url.startsWith("/api") &&
58
+ !req.url.startsWith(loginPageUrl) &&
59
+ !req.url.includes(".") &&
60
+ !req.url.includes("@")) {
61
+ return reply.redirect(`${loginPageUrl}` + `?redirect=${req.url}`);
62
+ }
63
+ // by default, disable 2factor for id.gov.ua auth
64
+ const check = req.user?.auth_type === "govid" ? config.auth?.["2factor"]?.govid : true;
65
+ const login2faPage = config.auth?.link?.["2fa"]?.login || "/2factor";
66
+ // example: 2factor for admin env only, while public env does not require it
67
+ const checkEnv = () => {
68
+ if (!config.auth?.["2factorEnv"])
69
+ return true;
70
+ if ((config.auth?.["2factorEnv"] &&
71
+ process.env.NODE_ENV === config.auth?.["2factorEnv"]) ||
72
+ (config.auth?.["2factorEnv"] === "admin" && isAdmin)) {
73
+ return true;
74
+ }
75
+ return false;
76
+ };
77
+ // if 2factor is enabled globally + for user and secondFactorPassed not true => redirect to 2factor login page
78
+ if (req.user?.uid &&
79
+ req.user?.twofa &&
80
+ config.auth?.["2factor"] &&
81
+ !isPublic &&
82
+ (routeOptions?.method || "GET") === "GET" &&
83
+ !req.session?.secondFactorPassed &&
84
+ !ispasswd &&
85
+ !config.auth?.disableRedirect &&
86
+ !config.auth?.disable &&
87
+ check &&
88
+ checkEnv()) {
89
+ if (!req.url.startsWith(login2faPage)) {
90
+ return reply.redirect(login2faPage);
91
+ }
92
+ }
93
+ return null;
94
+ }
10
95
  function plugin(fastify) {
11
96
  if (!config.redis) {
12
97
  return;
@@ -27,82 +112,6 @@ function plugin(fastify) {
27
112
  fastifyPassport.registerUserSerializer(async (user) => ({ user }));
28
113
  // deserialize user used to add user info from session store to req
29
114
  fastifyPassport.registerUserDeserializer(async (passport) => passport?.user || passport);
30
- fastify.addHook("onRequest", async (req, reply) => {
31
- const { pg, hostname, headers, routeOptions } = req;
32
- const { policy = [] } = routeOptions?.config || {};
33
- // proxy from old apps to editor, bi etc.
34
- const validToken = (req.ip === "193.239.152.181" ||
35
- req.ip === "127.0.0.1" ||
36
- req.ip?.startsWith?.("192.168.") ||
37
- config.debug) &&
38
- req.headers?.token &&
39
- config.auth?.tokens?.includes?.(headers.token);
40
- if (validToken && !req?.user?.uid) {
41
- req.user = {
42
- uid: req.headers?.uid?.toString?.(),
43
- user_type: req.headers?.user_type?.toString?.() || "regular",
44
- };
45
- }
46
- const isAdmin = process.env.NODE_ENV === "admin" ||
47
- hostname?.split?.(":")?.shift?.() === config.adminDomain ||
48
- config.admin ||
49
- hostname?.startsWith?.("admin");
50
- const isPublic = Array.isArray(policy)
51
- ? policy.includes("public")
52
- : policy === "L0";
53
- // if 2factor is enabled globally + for user and secondFactorPassed not true => redirect to 2factor login page
54
- const { secondFactorPassed, passport = {} } = req.session || {}; // base login +
55
- if (!passport.user?.uid &&
56
- (config.auth?.disable || config.auth?.user) &&
57
- !isPublic) {
58
- req.session = req.session || {};
59
- req.session.passport = req.session.passport || {}; // ensure passport session exists
60
- req.session.passport.user = {
61
- ...(config.auth?.user || {}),
62
- uid: config.auth?.user?.uid?.toString?.() || "1",
63
- user_rnokpp: config.auth?.user?.rnokpp,
64
- user_type: config.auth?.user?.type || "regular",
65
- };
66
- req.user = req.session.passport.user;
67
- }
68
- // ! intentional: null || undefined > undefined
69
- req.user = req.user || req.session?.passport?.user || undefined; // fix for user.uid errors, by default user is null, while with express passport it was {}, unauthorized user does not trigger serializer
70
- if (config.trace && false) {
71
- console.log("req.user?.uid", req.user?.uid, "req.session?.passport?.user?.uid", req.session?.passport?.user?.uid, "config.auth", config.auth);
72
- }
73
- // currently 2factor + auth with passwd file not supported
74
- const ispasswd = (existsSync("passwd") && !config.auth?.["2factor"]) ||
75
- config.auth?.passwd;
76
- if (!passport.user?.uid &&
77
- !config.auth?.disable &&
78
- isAdmin &&
79
- !isPublic &&
80
- !config.auth?.disableRedirect &&
81
- !req.url.startsWith(prefix) &&
82
- !req.url.startsWith("/api") &&
83
- !req.url.startsWith("/login") &&
84
- !req.url.includes('.') &&
85
- !req.url.includes('@')) {
86
- return reply.redirect(`${config?.auth?.redirect || "/login"}` + `?redirect=${req.url}`);
87
- }
88
- // by default, disable 2factor for id.gov.ua auth
89
- const check = passport.user?.auth_type === "govid"
90
- ? config.auth?.["2factor"]?.govid
91
- : true;
92
- if (passport.user?.uid &&
93
- passport.user?.twofa &&
94
- config.auth?.["2factor"] &&
95
- !isPublic &&
96
- (routeOptions?.method || "GET") === "GET" &&
97
- !secondFactorPassed &&
98
- !ispasswd &&
99
- check) {
100
- const href = config.auth?.["2factorRedirect"] || "/2factor";
101
- if (!href.includes(req.url)) {
102
- return reply.redirect(href);
103
- }
104
- }
105
- return null;
106
- });
115
+ fastify.addHook("onRequest", onRequest);
107
116
  }
108
117
  export default plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.0.56",
3
+ "version": "2.0.57",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [