@opengis/fastify-table 2.0.56 → 2.0.58

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.
@@ -59,8 +59,8 @@ if (_returnType != 'text') then
59
59
  end loop;
60
60
 
61
61
  raise notice 'reassign default finish: %', _tables;
62
-
63
- else
62
+
63
+ else
64
64
  raise notice 'skip default reassign';
65
65
 
66
66
  /* CREATE EXTENSION if not exists "uuid-ossp";
@@ -81,7 +81,7 @@ else
81
81
  COST 100;
82
82
  end if;
83
83
 
84
- end $$
84
+ end $$;
85
85
 
86
86
  CREATE OR REPLACE FUNCTION array_intersect(
87
87
  anyarray,
@@ -1 +1 @@
1
- {"version":3,"file":"authorizeUser.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/auth/funcs/authorizeUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AA2BtD,wBAA8B,aAAa,CACzC,IAAI,EAAE,YAAY,EAClB,GAAG,EAAE,GAAG,EACR,QAAQ,SAAe,EACvB,MAAM,CAAC,EAAE,MAAM,gBAkFhB"}
1
+ {"version":3,"file":"authorizeUser.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/auth/funcs/authorizeUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AA4BtD,wBAA8B,aAAa,CACzC,IAAI,EAAE,YAAY,EAClB,GAAG,EAAE,GAAG,EACR,QAAQ,SAAe,EACvB,MAAM,CAAC,EAAE,MAAM,gBA+HhB"}
@@ -1,5 +1,6 @@
1
1
  import config from "../../../../config.js";
2
2
  import logger from "../../logger/getLogger.js";
3
+ import pgClients from "../../pg/pgClients.js";
3
4
  import applyHook from "../../hook/applyHook.js";
4
5
  import logAuth from "./logAuth.js";
5
6
  /*
@@ -18,6 +19,7 @@ const getIp = (req) => (req.headers?.["x-real-ip"] ||
18
19
  export default async function authorizeUser(user, req, authType = "creds-user", expire) {
19
20
  if (!user?.uid)
20
21
  return "/logout";
22
+ const { pg = pgClients.client } = req;
21
23
  Object.assign(user, { auth_type: authType });
22
24
  // fastify/passport
23
25
  await req.login(user);
@@ -54,10 +56,39 @@ export default async function authorizeUser(user, req, authType = "creds-user",
54
56
  })) ||
55
57
  {};
56
58
  await req.session?.save?.();
57
- if (req.method === "POST") {
58
- return { message: "ok", status: "200" };
59
- }
60
59
  const redirectUrl = req.headers?.referer?.match?.(/[?&]redirect=([^&]+)/)?.[1] || "/";
60
+ const twofaEnabled = config.auth?.["2factor"] && user?.twofa && user.uid && pg;
61
+ const registered = false; // ? check by created/updated date?
62
+ if (req.method === "POST" &&
63
+ (!twofaEnabled || req.session?.secondFactorPassed)) {
64
+ return {
65
+ redirectUrl: href ||
66
+ config.auth?.redirectAfter ||
67
+ (redirectUrl.startsWith("/") ? redirectUrl : "/"),
68
+ authType,
69
+ registered,
70
+ user: { ...user, password: undefined, salt: undefined },
71
+ };
72
+ }
73
+ if (req.method === "POST" &&
74
+ twofaEnabled &&
75
+ !req.session?.secondFactorPassed) {
76
+ const verified = await pg
77
+ .query(`select
78
+ enabled and social_auth_url is not null as verified
79
+ from admin.users_social_auth
80
+ where uid = $1
81
+ and social_auth_type = $2`, [user.uid, "TOTP"])
82
+ .then((el) => el.rows?.[0]?.verified || false);
83
+ const redirect2fa = `${config.auth?.link?.["2fa"]?.login || "/2factor"}?redirect=${redirectUrl.startsWith("/") ? redirectUrl : "/"}`;
84
+ return {
85
+ redirectUrl: redirect2fa,
86
+ authType,
87
+ registered,
88
+ "2factor": { verified, enabled: !!user?.twofa },
89
+ user: { ...user, password: undefined, salt: undefined },
90
+ };
91
+ }
61
92
  // by default, disable 2factor for id.gov.ua auth
62
93
  const check = authType === "govid" ? config.auth?.["2factor"]?.govid : true;
63
94
  if (config.auth?.["2factor"] && user?.twofa && check) {
@@ -1 +1 @@
1
- {"version":3,"file":"loginUser.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/auth/funcs/loginUser.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAC;AAEvD,wBAA8B,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,YAAY,kBA+CpE"}
1
+ {"version":3,"file":"loginUser.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/auth/funcs/loginUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAUvC,wBAA8B,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,YAAY,kBA+CpE"}
@@ -9,14 +9,14 @@ export default async function loginUser(req, reply) {
9
9
  if (!config.pg) {
10
10
  return req.method === "GET"
11
11
  ? reply.status(302).redirect("/login?confirm=wrong_pass&message=empty pg")
12
- : reply.status(400).send({ message: "empty pg" });
12
+ : reply.status(400).send({ error: "empty pg", code: 400 });
13
13
  }
14
14
  if (!config.redis) {
15
15
  return req.method === "GET"
16
16
  ? reply
17
17
  .status(302)
18
18
  .redirect("/login?confirm=wrong_pass&message=empty redis")
19
- : reply.status(400).send({ message: "empty redis" });
19
+ : reply.status(400).send({ error: "empty redis", code: 400 });
20
20
  }
21
21
  const { user, message = "invalid user" } = (await verifyPassword({
22
22
  pg,
@@ -28,19 +28,19 @@ export default async function loginUser(req, reply) {
28
28
  ? reply
29
29
  .status(302)
30
30
  .redirect(`/login?confirm=wrong_pass&message=${message}`)
31
- : reply.status(400).send({ message });
31
+ : reply.status(400).send({ error: message, code: 400 });
32
32
  }
33
33
  if (!user && (req.query?.username || req.body?.username)) {
34
34
  return req.method === "GET"
35
35
  ? reply
36
36
  .status(302)
37
37
  .redirect(`/login?confirm=wrong_pass&message=${message}`)
38
- : reply.status(400).send({ message });
38
+ : reply.status(400).send({ error: message, code: 400 });
39
39
  }
40
40
  logger.metrics("user.login");
41
41
  const authType = "creds-" + (user.user_type === "admin" ? "admin" : "user");
42
- const href = await authorizeUser(user, req, authType); // creds-admin / creds-admin
42
+ const result = await authorizeUser(user, req, authType); // creds-admin / creds-admin
43
43
  return req.method === "GET"
44
- ? reply.status(302).redirect(href)
45
- : reply.status(200).send(href);
44
+ ? reply.status(302).redirect(result)
45
+ : reply.status(200).send(result);
46
46
  }
@@ -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;
@@ -0,0 +1,4 @@
1
+ import { FastifyReply } from "fastify";
2
+ import { ExtendedRequest } from "../../../../types/core.js";
3
+ export default function qrCode(req: ExtendedRequest, reply: FastifyReply): Promise<never>;
4
+ //# sourceMappingURL=qrcode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"qrcode.d.ts","sourceRoot":"","sources":["../../../../../../server/routes/auth/controllers/2factor/qrcode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAa5D,wBAA8B,MAAM,CAClC,GAAG,EAAE,eAAe,EACpB,KAAK,EAAE,YAAY,kBAsDpB"}
@@ -0,0 +1,48 @@
1
+ import qr from "qrcode";
2
+ import config from "../../../../../config.js";
3
+ import pgClients from "../../../../plugins/pg/pgClients.js";
4
+ import { getSecret, generate } from "./providers/totp.js";
5
+ const headers = {
6
+ "Content-Type": "image/png",
7
+ };
8
+ export default async function qrCode(req, reply) {
9
+ const { pg = pgClients.client } = req;
10
+ const { uid } = req.session?.passport?.user || {};
11
+ if (!uid) {
12
+ return reply.status(401).send({ error: "unauthorized", code: 401 });
13
+ }
14
+ if (!pg || !pg?.pk?.["admin.users"] || !pg?.pk?.["admin.users_social_auth"]) {
15
+ return reply.status(400).send({
16
+ error: "db connection / users/users_social_auth tables are required",
17
+ code: 400,
18
+ });
19
+ }
20
+ const userExists = await pg
21
+ .query(`select uid from admin.users where uid=$1`, [uid])
22
+ .then((el) => el.rows?.[0]?.uid);
23
+ if (!userExists && config.pg) {
24
+ return reply.status(404).send({ error: "invalid user", code: 404 });
25
+ }
26
+ const { enabled, secret } = pg
27
+ ? await getSecret({ pg, TYPE: "TOTP", uid })
28
+ : {};
29
+ const otp = secret
30
+ ? await pg
31
+ .query(`select social_auth_url
32
+ from admin.users_social_auth where uid=$1 and social_auth_type=$2`, [uid, "TOTP"])
33
+ ?.then((el) => el.rows?.[0]?.social_auth_url)
34
+ : await generate({ uid, pg });
35
+ const base64 = otp ? await qr.toDataURL(otp) : undefined;
36
+ if (enabled && !config.local && !config.debug) {
37
+ return reply
38
+ .status(400)
39
+ .send({ error: "2factor already enabled", code: 400 });
40
+ }
41
+ if (!otp || !base64) {
42
+ return reply.status(500).send({ error: "generation error", code: 500 });
43
+ }
44
+ // substring to exclude data:image/png;base64,
45
+ return reply
46
+ .headers(headers)
47
+ .send(Buffer.from(base64.substring(22), "base64"));
48
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/routes/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAiC1C,iBAAe,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,GAAE,GAAQ,iBA4ExD;AAED,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../server/routes/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAkC1C,iBAAe,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,GAAE,GAAQ,iBA8ExD;AAED,eAAe,MAAM,CAAC"}
@@ -18,6 +18,7 @@ import login2faTemplate from "./controllers/page/login2faTemplate.js";
18
18
  // jwt
19
19
  import oauthAuthorize from "./controllers/jwt/authorize.js";
20
20
  import oauthToken from "./controllers/jwt/token.js";
21
+ import qrCode from "./controllers/2factor/qrcode.js";
21
22
  const params = { config: { policy: "L0" } };
22
23
  async function plugin(app, opt = {}) {
23
24
  if (opt.routes === false || config.auth?.customRoutes) {
@@ -70,6 +71,9 @@ async function plugin(app, opt = {}) {
70
71
  !config.auth?.customGovId) {
71
72
  app.get("/auth/by_data", params, authByData);
72
73
  }
74
+ if (!app.hasRoute({ method: "GET", url: "/2factor/qr" })) {
75
+ app.get("/2factor/qr", params, qrCode);
76
+ }
73
77
  // pages
74
78
  if (!app.hasRoute({ method: "GET", url: "/2factor" }) &&
75
79
  !config.auth?.disable &&
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.58",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [