jxp 3.0.0 → 4.1.0
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/.env.sample +43 -6
- package/README.md +27 -13
- package/dist/bin/init_db.d.ts +3 -0
- package/dist/bin/init_db.d.ts.map +1 -0
- package/dist/bin/init_db.js +48 -0
- package/dist/bin/init_db.js.map +1 -0
- package/dist/bin/server.d.ts +4 -0
- package/dist/bin/server.d.ts.map +1 -0
- package/dist/bin/server.js +132 -0
- package/dist/bin/server.js.map +1 -0
- package/dist/bin/setup.d.ts +3 -0
- package/dist/bin/setup.d.ts.map +1 -0
- package/dist/bin/setup.js +185 -0
- package/dist/bin/setup.js.map +1 -0
- package/dist/globals.d.ts +14 -0
- package/dist/globals.d.ts.map +1 -0
- package/dist/globals.js +3 -0
- package/dist/globals.js.map +1 -0
- package/dist/libs/aggregate_guard.d.ts +5 -0
- package/dist/libs/aggregate_guard.d.ts.map +1 -0
- package/dist/libs/aggregate_guard.js +58 -0
- package/dist/libs/aggregate_guard.js.map +1 -0
- package/dist/libs/bulkwrite_guard.d.ts +5 -0
- package/dist/libs/bulkwrite_guard.d.ts.map +1 -0
- package/dist/libs/bulkwrite_guard.js +44 -0
- package/dist/libs/bulkwrite_guard.js.map +1 -0
- package/dist/libs/cache.d.ts +2 -0
- package/dist/libs/cache.d.ts.map +1 -0
- package/dist/libs/cache.js +94 -0
- package/dist/libs/cache.js.map +1 -0
- package/dist/libs/call_guard.d.ts +3 -0
- package/dist/libs/call_guard.d.ts.map +1 -0
- package/dist/libs/call_guard.js +17 -0
- package/dist/libs/call_guard.js.map +1 -0
- package/dist/libs/connection_string.d.ts +3 -0
- package/dist/libs/connection_string.d.ts.map +1 -0
- package/dist/libs/connection_string.js +4 -0
- package/dist/libs/connection_string.js.map +1 -0
- package/dist/libs/datamunging.d.ts +2 -0
- package/dist/libs/datamunging.d.ts.map +1 -0
- package/dist/libs/datamunging.js +320 -0
- package/dist/libs/datamunging.js.map +1 -0
- package/dist/libs/docs-auth.d.ts +25 -0
- package/dist/libs/docs-auth.d.ts.map +1 -0
- package/dist/libs/docs-auth.js +205 -0
- package/dist/libs/docs-auth.js.map +1 -0
- package/dist/libs/docs.d.ts +2 -0
- package/dist/libs/docs.d.ts.map +1 -0
- package/dist/libs/docs.js +328 -0
- package/dist/libs/docs.js.map +1 -0
- package/dist/libs/groups.d.ts +2 -0
- package/dist/libs/groups.d.ts.map +1 -0
- package/dist/libs/groups.js +136 -0
- package/dist/libs/groups.js.map +1 -0
- package/dist/libs/jxp.d.ts +4 -0
- package/dist/libs/jxp.d.ts.map +1 -0
- package/dist/libs/jxp.js +1176 -0
- package/dist/libs/jxp.js.map +1 -0
- package/dist/libs/link_index.d.ts +14 -0
- package/dist/libs/link_index.d.ts.map +1 -0
- package/dist/libs/link_index.js +37 -0
- package/dist/libs/link_index.js.map +1 -0
- package/dist/libs/load-config.d.ts +16 -0
- package/dist/libs/load-config.d.ts.map +1 -0
- package/dist/libs/load-config.js +166 -0
- package/dist/libs/load-config.js.map +1 -0
- package/dist/libs/login.d.ts +2 -0
- package/dist/libs/login.d.ts.map +1 -0
- package/dist/libs/login.js +244 -0
- package/dist/libs/login.js.map +1 -0
- package/dist/libs/login_rate_limit.d.ts +16 -0
- package/dist/libs/login_rate_limit.d.ts.map +1 -0
- package/dist/libs/login_rate_limit.js +49 -0
- package/dist/libs/login_rate_limit.js.map +1 -0
- package/dist/libs/modeldir.d.ts +5 -0
- package/dist/libs/modeldir.d.ts.map +1 -0
- package/dist/libs/modeldir.js +15 -0
- package/dist/libs/modeldir.js.map +1 -0
- package/dist/libs/query_limits.d.ts +2 -0
- package/dist/libs/query_limits.d.ts.map +1 -0
- package/dist/libs/query_limits.js +84 -0
- package/dist/libs/query_limits.js.map +1 -0
- package/dist/libs/query_manipulation.d.ts +3 -0
- package/dist/libs/query_manipulation.d.ts.map +1 -0
- package/{libs → dist/libs}/query_manipulation.js +22 -21
- package/dist/libs/query_manipulation.js.map +1 -0
- package/dist/libs/query_sanitize.d.ts +5 -0
- package/dist/libs/query_sanitize.d.ts.map +1 -0
- package/dist/libs/query_sanitize.js +109 -0
- package/dist/libs/query_sanitize.js.map +1 -0
- package/dist/libs/response_sanitize.d.ts +7 -0
- package/dist/libs/response_sanitize.d.ts.map +1 -0
- package/dist/libs/response_sanitize.js +57 -0
- package/dist/libs/response_sanitize.js.map +1 -0
- package/dist/libs/safe_error.d.ts +3 -0
- package/dist/libs/safe_error.d.ts.map +1 -0
- package/dist/libs/safe_error.js +14 -0
- package/dist/libs/safe_error.js.map +1 -0
- package/dist/libs/schema.d.ts +14 -0
- package/dist/libs/schema.d.ts.map +1 -0
- package/dist/libs/schema.js +98 -0
- package/dist/libs/schema.js.map +1 -0
- package/dist/libs/schema_description.d.ts +3 -0
- package/dist/libs/schema_description.d.ts.map +1 -0
- package/{libs → dist/libs}/schema_description.js +12 -21
- package/dist/libs/schema_description.js.map +1 -0
- package/dist/libs/security.d.ts +2 -0
- package/dist/libs/security.d.ts.map +1 -0
- package/dist/libs/security.js +428 -0
- package/dist/libs/security.js.map +1 -0
- package/dist/libs/setup.d.ts +2 -0
- package/dist/libs/setup.d.ts.map +1 -0
- package/dist/libs/setup.js +87 -0
- package/dist/libs/setup.js.map +1 -0
- package/dist/libs/startup-deprecations.d.ts +2 -0
- package/dist/libs/startup-deprecations.d.ts.map +1 -0
- package/dist/libs/startup-deprecations.js +21 -0
- package/dist/libs/startup-deprecations.js.map +1 -0
- package/dist/libs/startup.d.ts +16 -0
- package/dist/libs/startup.d.ts.map +1 -0
- package/dist/libs/startup.js +63 -0
- package/dist/libs/startup.js.map +1 -0
- package/dist/libs/ws.d.ts +2 -0
- package/dist/libs/ws.d.ts.map +1 -0
- package/{libs → dist/libs}/ws.js +91 -73
- package/dist/libs/ws.js.map +1 -0
- package/dist/models/apikey_model.d.ts +10 -0
- package/dist/models/apikey_model.d.ts.map +1 -0
- package/dist/models/apikey_model.js +14 -0
- package/dist/models/apikey_model.js.map +1 -0
- package/dist/models/link_model.d.ts +9 -0
- package/dist/models/link_model.d.ts.map +1 -0
- package/dist/models/link_model.js +20 -0
- package/dist/models/link_model.js.map +1 -0
- package/dist/models/refreshtoken_model.d.ts +11 -0
- package/dist/models/refreshtoken_model.d.ts.map +1 -0
- package/dist/models/refreshtoken_model.js +22 -0
- package/dist/models/refreshtoken_model.js.map +1 -0
- package/dist/models/test_model.d.ts +24 -0
- package/dist/models/test_model.d.ts.map +1 -0
- package/dist/models/test_model.js +44 -0
- package/dist/models/test_model.js.map +1 -0
- package/dist/models/token_model.d.ts +14 -0
- package/dist/models/token_model.d.ts.map +1 -0
- package/dist/models/token_model.js +25 -0
- package/dist/models/token_model.js.map +1 -0
- package/dist/models/user_model.d.ts +15 -0
- package/dist/models/user_model.d.ts.map +1 -0
- package/dist/models/user_model.js +38 -0
- package/dist/models/user_model.js.map +1 -0
- package/dist/models/usergroups_model.d.ts +9 -0
- package/dist/models/usergroups_model.d.ts.map +1 -0
- package/dist/models/usergroups_model.js +18 -0
- package/dist/models/usergroups_model.js.map +1 -0
- package/dist/types/jxp-config.d.ts +151 -0
- package/dist/types/jxp-config.d.ts.map +1 -0
- package/dist/types/jxp-config.js +3 -0
- package/dist/types/jxp-config.js.map +1 -0
- package/dist/types/schema-fields.d.ts +43 -0
- package/dist/types/schema-fields.d.ts.map +1 -0
- package/dist/types/schema-fields.js +3 -0
- package/dist/types/schema-fields.js.map +1 -0
- package/docs/aggregations.md +40 -21
- package/docs/api.md +55 -15
- package/docs/authentication.md +60 -37
- package/docs/bulk_writes.md +11 -8
- package/docs/caching.md +28 -11
- package/docs/changelog.md +258 -31
- package/docs/configuration.md +126 -115
- package/docs/hooks.md +12 -8
- package/docs/index.md +17 -17
- package/docs/installation.md +23 -18
- package/docs/permissions.md +33 -19
- package/docs/queries.md +9 -5
- package/docs/schemas.md +21 -10
- package/docs/special.md +4 -4
- package/docs/typescript.md +86 -0
- package/docs/websocket.md +99 -1
- package/mkdocs.yml +1 -0
- package/package.json +121 -69
- package/templates/api.pug +32 -0
- package/templates/app-server.js +43 -0
- package/templates/app.env.sample +8 -0
- package/templates/assets/api-console.js +151 -0
- package/templates/assets/docs-login.js +66 -0
- package/templates/assets/docs.css +244 -0
- package/templates/index.pug +67 -2
- package/templates/layout.pug +21 -11
- package/templates/login.pug +39 -0
- package/templates/md.pug +1 -1
- package/templates/model.pug +117 -68
- package/templates/navbar.pug +25 -21
- package/templates/sidebar.pug +30 -0
- package/.env +0 -9
- package/.eslintrc.json +0 -20
- package/.jshintrc +0 -4
- package/.vscode/launch.json +0 -34
- package/.vscode/settings.json +0 -3
- package/Dockerfile +0 -7
- package/bin/init_db.js +0 -54
- package/bin/server.js +0 -131
- package/bin/setup.js +0 -190
- package/config_sample.json +0 -10
- package/docker-compose.yml +0 -32
- package/libs/cache.js +0 -92
- package/libs/connection_string.js +0 -24
- package/libs/datamunging.js +0 -335
- package/libs/docs.js +0 -162
- package/libs/groups.js +0 -133
- package/libs/jxp.js +0 -1208
- package/libs/login.js +0 -222
- package/libs/modeldir.js +0 -14
- package/libs/schema.js +0 -99
- package/libs/security.js +0 -410
- package/libs/setup.js +0 -90
- package/models/apikey_model.js +0 -10
- package/models/link_model.js +0 -17
- package/models/refreshtoken_model.js +0 -20
- package/models/test_model.js +0 -50
- package/models/token_model.js +0 -23
- package/models/user_model.js +0 -38
- package/models/usergroups_model.js +0 -15
- package/setup.sh +0 -3
- package/test/.eslintrc.json +0 -13
- package/test/auth.js +0 -190
- package/test/init.js +0 -92
- package/test/setup.test.js +0 -62
- package/test/test.js +0 -1527
- package/test/wstest.js.old +0 -124
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const JXPHelper = require("jxp-helper");
|
|
9
|
+
const modeldir = require("./modeldir");
|
|
10
|
+
const load_config_1 = require("./load-config");
|
|
11
|
+
// Set some global types for JS model files using /* global ObjectId Mixed */
|
|
12
|
+
global.ObjectId = mongoose_1.default.Schema.Types.ObjectId;
|
|
13
|
+
global.Mixed = mongoose_1.default.Schema.Types.Mixed;
|
|
14
|
+
function resolveModelDir(dir) {
|
|
15
|
+
if (path_1.default.isAbsolute(dir))
|
|
16
|
+
return dir;
|
|
17
|
+
return path_1.default.resolve(process.cwd(), dir);
|
|
18
|
+
}
|
|
19
|
+
function getModelDir() {
|
|
20
|
+
const fromEnv = (0, load_config_1.getModelDirFromEnv)();
|
|
21
|
+
if (fromEnv)
|
|
22
|
+
return resolveModelDir(fromEnv);
|
|
23
|
+
return modeldir.findModelDir(path_1.default.dirname(process.argv[1] ?? "."));
|
|
24
|
+
}
|
|
25
|
+
const getModelFileFromRef = (ref) => {
|
|
26
|
+
return path_1.default.join(getModelDir(), `${String(ref).toLowerCase()}_model`);
|
|
27
|
+
};
|
|
28
|
+
class JXPSchema extends mongoose_1.default.Schema {
|
|
29
|
+
constructor(definition, opts = {}) {
|
|
30
|
+
opts = Object.assign({
|
|
31
|
+
timestamps: true,
|
|
32
|
+
toJSON: { virtuals: true },
|
|
33
|
+
toObject: { virtuals: true },
|
|
34
|
+
}, opts);
|
|
35
|
+
definition = Object.assign({
|
|
36
|
+
_deleted: { type: Boolean, default: false, index: true },
|
|
37
|
+
_owner_id: { type: global.ObjectId, link: "User", map_to: "_owner", index: true },
|
|
38
|
+
_updated_by_id: { type: global.ObjectId, link: "User", map_to: "_updated_by", index: true },
|
|
39
|
+
}, definition);
|
|
40
|
+
if (!global.jxphelper) {
|
|
41
|
+
const jxp_settings = {};
|
|
42
|
+
if (global.apikey)
|
|
43
|
+
jxp_settings.apikey = global.apikey;
|
|
44
|
+
if (global.server)
|
|
45
|
+
jxp_settings.server = global.server;
|
|
46
|
+
if (jxp_settings.apikey && jxp_settings.server) {
|
|
47
|
+
global.jxphelper = new JXPHelper(jxp_settings);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
super(definition, opts);
|
|
51
|
+
this.opts = opts;
|
|
52
|
+
this.definition = definition;
|
|
53
|
+
this.index({ createdAt: -1 });
|
|
54
|
+
this.index({ updatedAt: -1 });
|
|
55
|
+
this.setPerms();
|
|
56
|
+
this.generateLinks();
|
|
57
|
+
}
|
|
58
|
+
setPerms() {
|
|
59
|
+
// JXP stores permission strings on the schema for runtime auth checks
|
|
60
|
+
this.set("_perms", Object.assign({
|
|
61
|
+
admin: "",
|
|
62
|
+
owner: "",
|
|
63
|
+
user: "",
|
|
64
|
+
all: "",
|
|
65
|
+
}, this.opts.perms));
|
|
66
|
+
}
|
|
67
|
+
generateLinks() {
|
|
68
|
+
const loaded_files = [];
|
|
69
|
+
for (const key of Object.keys(this.definition)) {
|
|
70
|
+
let def = this.definition[key];
|
|
71
|
+
let is_array = false;
|
|
72
|
+
if (Array.isArray(def) && def[0]) {
|
|
73
|
+
def = def[0];
|
|
74
|
+
is_array = true;
|
|
75
|
+
}
|
|
76
|
+
if (!def || typeof def !== "object" || !("link" in def) || !def.link)
|
|
77
|
+
continue;
|
|
78
|
+
const virtual_name = def.map_to || def.virtual || String(def.link).toLowerCase();
|
|
79
|
+
if (!loaded_files.includes(def.link)) {
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
81
|
+
const linked = require(getModelFileFromRef(def.link));
|
|
82
|
+
void (linked.default || linked);
|
|
83
|
+
loaded_files.push(def.link);
|
|
84
|
+
}
|
|
85
|
+
this.virtual(virtual_name, {
|
|
86
|
+
ref: def.link,
|
|
87
|
+
localField: key,
|
|
88
|
+
foreignField: "_id",
|
|
89
|
+
justOne: def.justOne ?? !is_array,
|
|
90
|
+
options: Object.assign({}, def.options),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
static Types = mongoose_1.default.Schema.Types;
|
|
95
|
+
static model = mongoose_1.default.model.bind(mongoose_1.default);
|
|
96
|
+
}
|
|
97
|
+
exports.default = JXPSchema;
|
|
98
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/libs/schema.ts"],"names":[],"mappings":";;;;;AAAA,wDAAuE;AACvE,gDAAwB;AACxB,wCAAyC;AACzC,uCAAwC;AACxC,+CAAmD;AAGnD,6EAA6E;AAC7E,MAAM,CAAC,QAAQ,GAAG,kBAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC;AACjD,MAAM,CAAC,KAAK,GAAG,kBAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;AAE3C,SAAS,eAAe,CAAC,GAAW;IACnC,IAAI,cAAI,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IACrC,OAAO,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,GAAG,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,WAAW;IACnB,MAAM,OAAO,GAAG,IAAA,gCAAkB,GAAE,CAAC;IACrC,IAAI,OAAO;QAAE,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;IAC7C,OAAO,QAAQ,CAAC,YAAY,CAAC,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;AACpE,CAAC;AAED,MAAM,mBAAmB,GAAG,CAAC,GAAW,EAAU,EAAE;IACnD,OAAO,cAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;AACvE,CAAC,CAAC;AAWF,MAAM,SAAU,SAAQ,kBAAQ,CAAC,MAAM;IAIt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I,CAAC,EAAE,CAAC;gBACtC,iEAAiE;gBACjE,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;gBACtD,KAAK,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,CAAC;gBAChC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC7B,CAAC;YACD,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;gBAC1B,GAAG,EAAE,GAAG,CAAC,IAAI;gBACb,UAAU,EAAE,GAAG;gBACf,YAAY,EAAE,KAAK;gBACnB,OAAO,EAAE,GAAG,CAAC,OAAO,IAAI,CAAC,QAAQ;gBACjC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC;aACvC,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;IAED,MAAM,CAAC,KAAK,GAAG,kBAAQ,CAAC,MAAM,CAAC,KAAK,CAAC;IACrC,MAAM,CAAC,KAAK,GAAG,kBAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,kBAAQ,CAI9B,CAAC;;AAIf,kBAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema_description.d.ts","sourceRoot":"","sources":["../../src/libs/schema_description.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,oBAAoB,GAAI,cAAS,qBAmFtC,CAAA;AAED,SAAS,oBAAoB,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
const fs = require("fs");
|
|
2
3
|
const path = require("path");
|
|
3
4
|
const mongoose = require("mongoose");
|
|
4
|
-
|
|
5
5
|
const getModelFileContents = (model_dir) => {
|
|
6
6
|
return new Promise((resolve, reject) => {
|
|
7
7
|
try {
|
|
@@ -9,22 +9,16 @@ const getModelFileContents = (model_dir) => {
|
|
|
9
9
|
if (err) {
|
|
10
10
|
return reject(err);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
// Filter for only model files first
|
|
14
|
-
const modelFiles = files.filter(file =>
|
|
15
|
-
|
|
16
|
-
fs.statSync(path.join(model_dir, file)).isFile()
|
|
17
|
-
);
|
|
18
|
-
|
|
13
|
+
const modelFiles = files.filter(file => file.endsWith('_model.js') &&
|
|
14
|
+
fs.statSync(path.join(model_dir, file)).isFile());
|
|
19
15
|
// Limit number of files processed at once
|
|
20
16
|
const MAX_FILES = 1000;
|
|
21
17
|
if (modelFiles.length > MAX_FILES) {
|
|
22
18
|
return reject(new Error(`Too many model files (${modelFiles.length}). Maximum allowed is ${MAX_FILES}`));
|
|
23
19
|
}
|
|
24
|
-
|
|
25
20
|
let models = [];
|
|
26
21
|
let errors = [];
|
|
27
|
-
|
|
28
22
|
for (const file of modelFiles) {
|
|
29
23
|
const modelname = path.basename(file, ".js").replace("_model", "");
|
|
30
24
|
try {
|
|
@@ -34,20 +28,19 @@ const getModelFileContents = (model_dir) => {
|
|
|
34
28
|
// Convert modelname to proper case for mongoose (first letter uppercase)
|
|
35
29
|
const modelKey = modelname.charAt(0).toUpperCase() + modelname.slice(1);
|
|
36
30
|
modelobj = mongoose.models[modelKey];
|
|
37
|
-
|
|
38
31
|
// If model doesn't exist in mongoose, try to load it from file
|
|
39
32
|
if (!modelobj) {
|
|
40
33
|
const filePath = path.join(model_dir, file);
|
|
41
34
|
delete require.cache[require.resolve(filePath)];
|
|
42
35
|
modelobj = require(filePath);
|
|
43
36
|
}
|
|
44
|
-
}
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
45
39
|
// If getting from mongoose fails, try loading from file
|
|
46
40
|
const filePath = path.join(model_dir, file);
|
|
47
41
|
delete require.cache[require.resolve(filePath)];
|
|
48
42
|
modelobj = require(filePath);
|
|
49
43
|
}
|
|
50
|
-
|
|
51
44
|
// Check if we have a valid model with schema and permissions
|
|
52
45
|
if (modelobj && modelobj.schema) {
|
|
53
46
|
const perms = modelobj.schema.get("_perms");
|
|
@@ -60,11 +53,10 @@ const getModelFileContents = (model_dir) => {
|
|
|
60
53
|
continue; // Skip error handling if successful
|
|
61
54
|
}
|
|
62
55
|
}
|
|
63
|
-
|
|
64
56
|
// If we get here, the model was loaded but didn't have proper schema/perms
|
|
65
57
|
errors.push(`Invalid model structure for ${modelname}`);
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
68
60
|
// Only add to errors if it's not an OverwriteModelError
|
|
69
61
|
if (!error.message.includes('Cannot overwrite')) {
|
|
70
62
|
errors.push(`Error with model ${modelname}: ${error.message}`);
|
|
@@ -72,19 +64,18 @@ const getModelFileContents = (model_dir) => {
|
|
|
72
64
|
}
|
|
73
65
|
}
|
|
74
66
|
}
|
|
75
|
-
|
|
76
67
|
// If we have errors but also some valid models, just log the errors
|
|
77
68
|
if (errors.length > 0 && models.length > 0) {
|
|
78
69
|
console.warn('Some models failed to load:', errors);
|
|
79
70
|
}
|
|
80
|
-
|
|
81
71
|
// Return empty array if no models found, but don't treat it as an error
|
|
82
72
|
return resolve(models);
|
|
83
73
|
});
|
|
84
|
-
}
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
85
76
|
return reject(err);
|
|
86
77
|
}
|
|
87
78
|
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
79
|
+
};
|
|
80
|
+
module.exports = getModelFileContents;
|
|
81
|
+
//# sourceMappingURL=schema_description.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema_description.js","sourceRoot":"","sources":["../../src/libs/schema_description.ts"],"names":[],"mappings":";AAAA,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACzB,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAErC,MAAM,oBAAoB,GAAG,CAAC,SAAS,EAAE,E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AAK,CAAC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;4BAC9C,MAAM,CAAC,IAAI,CAAC,oBAAoB,SAAS,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;4BAC/D,OAAO,CAAC,KAAK,CAAC,0BAA0B,SAAS,GAAG,EAAE,KAAK,CAAC,CAAC;wBACjE,CAAC;oBACL,CAAC;gBACL,CAAC;gBAED,oEAAoE;gBACpE,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACzC,OAAO,CAAC,IAAI,CAAC,6BAA6B,EAAE,MAAM,CAAC,CAAC;gBACxD,CAAC;gBAED,wEAAwE;gBACxE,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YAC3B,CAAC,CAAC,CAAC;QACP,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC,CAAA;AAED,iBAAS,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/libs/security.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bcrypt = require("bcryptjs");
|
|
4
|
+
const randToken = require("rand-token");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const errors = require("restify-errors");
|
|
7
|
+
var APIKey = null;
|
|
8
|
+
var Token = null;
|
|
9
|
+
var Groups = null;
|
|
10
|
+
var User = null;
|
|
11
|
+
var RefreshToken = null;
|
|
12
|
+
var provider = "";
|
|
13
|
+
const loadModel = (modelDir, file) => {
|
|
14
|
+
const mod = require(path.join(modelDir, file));
|
|
15
|
+
return mod.default || mod;
|
|
16
|
+
};
|
|
17
|
+
const init = function (config) {
|
|
18
|
+
APIKey = loadModel(config.model_dir, "apikey_model");
|
|
19
|
+
Groups = loadModel(config.model_dir, "usergroups_model.js");
|
|
20
|
+
User = loadModel(config.model_dir, "user_model");
|
|
21
|
+
Token = loadModel(config.model_dir, "token_model");
|
|
22
|
+
RefreshToken = loadModel(config.model_dir, "refreshtoken_model");
|
|
23
|
+
if (config.url)
|
|
24
|
+
provider = config.url;
|
|
25
|
+
};
|
|
26
|
+
const basicAuthData = function (req) {
|
|
27
|
+
if (!req.headers.authorization) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const authorization = req.headers.authorization.split(" ")[1];
|
|
32
|
+
const decoded = Buffer.from(authorization, "base64").toString();
|
|
33
|
+
return decoded.split(":");
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const basicAuth = async (ba) => {
|
|
40
|
+
try {
|
|
41
|
+
if (!Array.isArray(ba) || ba.length !== 2) {
|
|
42
|
+
throw ("Basic Auth incorrectly formatted");
|
|
43
|
+
}
|
|
44
|
+
var email = ba[0];
|
|
45
|
+
var password = ba[1];
|
|
46
|
+
const user = await User.findOne({ email }).exec();
|
|
47
|
+
if (!user) {
|
|
48
|
+
throw (new Date(), `Incorrect username or password for ${email}`);
|
|
49
|
+
}
|
|
50
|
+
if (!await bcrypt.compare(password, user.password)) {
|
|
51
|
+
throw (`Incorrect username or password for ${email}`);
|
|
52
|
+
}
|
|
53
|
+
return user;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error(new Date(), err);
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const bearerAuthData = req => {
|
|
61
|
+
if (!req.headers.authorization) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const token = req.headers.authorization.split(" ")[1];
|
|
66
|
+
return token;
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const bearerAuth = async (t) => {
|
|
73
|
+
try {
|
|
74
|
+
if (!t)
|
|
75
|
+
throw ("Token invalid");
|
|
76
|
+
const token = await Token.findOne({ access_token: t, provider }).exec();
|
|
77
|
+
if (!token) {
|
|
78
|
+
throw (`Token ${t} not found`);
|
|
79
|
+
}
|
|
80
|
+
if (!tokenIsValid(token)) {
|
|
81
|
+
throw (`Token is no loger valid`);
|
|
82
|
+
}
|
|
83
|
+
const user = await User.findOne({ _id: token.user_id }).exec();
|
|
84
|
+
if (!user) {
|
|
85
|
+
throw (`Could not find user`);
|
|
86
|
+
}
|
|
87
|
+
return user;
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
console.error(err);
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const apiKeyAuth = async (apikey) => {
|
|
95
|
+
try {
|
|
96
|
+
if (!apikey)
|
|
97
|
+
throw ("Missing apikey");
|
|
98
|
+
const result = await APIKey.findOne({ apikey });
|
|
99
|
+
if (!result)
|
|
100
|
+
throw ("Could not find apikey");
|
|
101
|
+
const user = await User.findOne({ _id: result.user_id }).exec();
|
|
102
|
+
if (!user)
|
|
103
|
+
throw ("Could not find user associated to apikey");
|
|
104
|
+
return user;
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
console.error(new Date(), err);
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const getGroups = async (user_id) => {
|
|
112
|
+
try {
|
|
113
|
+
const userGroup = await Groups.findOne({ user_id });
|
|
114
|
+
var groups = userGroup && userGroup.groups ? userGroup.groups : [];
|
|
115
|
+
return groups;
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
console.error(new Date(), err);
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const encPassword = password => {
|
|
123
|
+
return bcrypt.hashSync(password, 4);
|
|
124
|
+
};
|
|
125
|
+
const generateApiKey = async (user_id) => {
|
|
126
|
+
try {
|
|
127
|
+
let existing = await APIKey.findOne({ user_id }).sort({ last_accessed: -1 }).exec();
|
|
128
|
+
if (existing) {
|
|
129
|
+
await existing.updateOne({ last_accessed: new Date() });
|
|
130
|
+
return existing;
|
|
131
|
+
}
|
|
132
|
+
var apikey = new APIKey();
|
|
133
|
+
apikey.user_id = user_id;
|
|
134
|
+
apikey.apikey = randToken.generate(16);
|
|
135
|
+
await apikey.save();
|
|
136
|
+
return apikey;
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
console.error(new Date(), err);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const tokenIsValid = token => {
|
|
144
|
+
if (!token)
|
|
145
|
+
return false;
|
|
146
|
+
const now = +new Date();
|
|
147
|
+
const expires_at = +new Date(token.createdAt) + (token.expires_in * 1000);
|
|
148
|
+
return (expires_at > now);
|
|
149
|
+
};
|
|
150
|
+
const tokenExpires = (token) => {
|
|
151
|
+
return new Date(new Date(token.createdAt).getTime() + token.expires_in * 1000);
|
|
152
|
+
};
|
|
153
|
+
const generateToken = async (user_id) => {
|
|
154
|
+
try {
|
|
155
|
+
var token = new Token();
|
|
156
|
+
token.user_id = user_id;
|
|
157
|
+
token.access_token = randToken.generate(16);
|
|
158
|
+
token.provider = provider;
|
|
159
|
+
await token.save();
|
|
160
|
+
return token;
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
console.error(new Date(), err);
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
const ensureToken = async (user_id) => {
|
|
168
|
+
try {
|
|
169
|
+
const token = await Token.findOne({ user_id, provider }).sort({ createdAt: -1 }).exec();
|
|
170
|
+
if (tokenIsValid(token)) {
|
|
171
|
+
return token;
|
|
172
|
+
}
|
|
173
|
+
return await generateToken(user_id);
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
console.error(new Date(), err);
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const refreshToken = async (user_id) => {
|
|
181
|
+
await revokeToken(user_id);
|
|
182
|
+
return await ensureToken(user_id);
|
|
183
|
+
};
|
|
184
|
+
const revokeToken = async (user_id) => {
|
|
185
|
+
await Token.deleteOne({ user_id, provider });
|
|
186
|
+
return;
|
|
187
|
+
};
|
|
188
|
+
const generateRefreshToken = async (user_id) => {
|
|
189
|
+
try {
|
|
190
|
+
var refreshtoken = new RefreshToken();
|
|
191
|
+
refreshtoken.user_id = user_id;
|
|
192
|
+
refreshtoken.refresh_token = randToken.generate(16);
|
|
193
|
+
await refreshtoken.save();
|
|
194
|
+
return refreshtoken;
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
console.error(new Date(), err);
|
|
198
|
+
throw err;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const ensureRefreshToken = async (user_id) => {
|
|
202
|
+
const refreshtoken = await RefreshToken.findOne({ user_id }).sort({ createdAt: -1 }).exec();
|
|
203
|
+
if (tokenIsValid(refreshtoken)) {
|
|
204
|
+
return refreshtoken;
|
|
205
|
+
}
|
|
206
|
+
return await generateRefreshToken(user_id);
|
|
207
|
+
};
|
|
208
|
+
const revokeRefreshToken = async (user_id) => {
|
|
209
|
+
await RefreshToken.deleteMany({ user_id });
|
|
210
|
+
return;
|
|
211
|
+
};
|
|
212
|
+
const refresh = async (req, res) => {
|
|
213
|
+
try {
|
|
214
|
+
if (req.headers.authorization && req.headers.authorization.trim().toLowerCase().indexOf("bearer") === 0) {
|
|
215
|
+
const refresh_token = await RefreshToken.findOne({ refresh_token: bearerAuthData(req) }).exec();
|
|
216
|
+
if (!refresh_token)
|
|
217
|
+
throw ("Refresh token not found");
|
|
218
|
+
if (!tokenIsValid(refresh_token))
|
|
219
|
+
throw ("Refresh token has expired");
|
|
220
|
+
const user_id = refresh_token.user_id;
|
|
221
|
+
const token = await refreshToken(user_id);
|
|
222
|
+
await revokeRefreshToken(user_id);
|
|
223
|
+
const new_refresh_token = await generateRefreshToken(user_id);
|
|
224
|
+
res.send({
|
|
225
|
+
user_id: user_id,
|
|
226
|
+
token: token.access_token,
|
|
227
|
+
token_expires: tokenExpires(token),
|
|
228
|
+
refresh_token: new_refresh_token.refresh_token,
|
|
229
|
+
refresh_token_expires: tokenExpires(new_refresh_token)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
throw ("Missing refresh token");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
console.error(err);
|
|
238
|
+
if (err.code)
|
|
239
|
+
throw err;
|
|
240
|
+
throw new errors.ForbiddenError(err.toString());
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const login = async (req, res) => {
|
|
244
|
+
try {
|
|
245
|
+
const authenticate_result = await authenticate(req);
|
|
246
|
+
if (!authenticate_result) {
|
|
247
|
+
res.user = null;
|
|
248
|
+
res.groups = [];
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
res = Object.assign(res, authenticate_result);
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
console.error(err);
|
|
255
|
+
if (err.code)
|
|
256
|
+
throw err;
|
|
257
|
+
throw new errors.ForbiddenError(err.toString());
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const authenticate = async (req) => {
|
|
261
|
+
let user = null;
|
|
262
|
+
if (!req.query.apikey && !req.headers.authorization && !(req.headers["X-API-Key"] || req.headers["x-api-key"])) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if (req.headers.authorization && req.headers.authorization.trim().toLowerCase().indexOf("basic") === 0) {
|
|
266
|
+
// Basic Auth
|
|
267
|
+
user = await basicAuth(basicAuthData(req));
|
|
268
|
+
}
|
|
269
|
+
else if (req.headers.authorization && req.headers.authorization.trim().toLowerCase().indexOf("bearer") === 0) {
|
|
270
|
+
// Token Auth
|
|
271
|
+
user = await bearerAuth(bearerAuthData(req));
|
|
272
|
+
}
|
|
273
|
+
else if (req.query.apikey) {
|
|
274
|
+
user = await apiKeyAuth(req.query.apikey);
|
|
275
|
+
}
|
|
276
|
+
else if (req.headers["X-API-Key"] || req.headers["x-api-key"]) {
|
|
277
|
+
// API Key
|
|
278
|
+
user = await apiKeyAuth(req.headers["X-API-Key"] || req.headers["x-api-key"]);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
throw ("Could not find any way to authenticate");
|
|
282
|
+
}
|
|
283
|
+
if (!user) {
|
|
284
|
+
throw ("Could not find user");
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
token: await ensureToken(user._id),
|
|
288
|
+
refresh_token: await ensureRefreshToken(user._id),
|
|
289
|
+
groups: await getGroups(user._id),
|
|
290
|
+
username: user.email,
|
|
291
|
+
user
|
|
292
|
+
};
|
|
293
|
+
};
|
|
294
|
+
const auth = async (req, res) => {
|
|
295
|
+
// Check against model as to whether we're allowed to edit this model
|
|
296
|
+
if (!req.Model) {
|
|
297
|
+
// console.error("Model missing");
|
|
298
|
+
throw new errors.BadRequestError("Model missing");
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
var method = null;
|
|
302
|
+
// console.log("req.route.name", req.route.name);
|
|
303
|
+
if (req.method == "GET" || req.route.name === "postquerymodelname" || req.route.name === "postaggregatemodelname") {
|
|
304
|
+
method = "r";
|
|
305
|
+
}
|
|
306
|
+
else if (req.method == "POST") {
|
|
307
|
+
method = "c";
|
|
308
|
+
}
|
|
309
|
+
else if (req.method == "PUT") {
|
|
310
|
+
method = "u";
|
|
311
|
+
}
|
|
312
|
+
else if (req.method == "DELETE") {
|
|
313
|
+
method = "d";
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
// console.error("Unsupported operation", req.method);
|
|
317
|
+
throw new errors.InternalServerError(`Unsupported operation: ${req.method}`);
|
|
318
|
+
}
|
|
319
|
+
return await check_perms(res.user, res.groups, req.Model, method, req.params.item_id);
|
|
320
|
+
}
|
|
321
|
+
catch (err) {
|
|
322
|
+
console.error(err);
|
|
323
|
+
if (err.code)
|
|
324
|
+
throw err;
|
|
325
|
+
throw new errors.ForbiddenError(err.toString());
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
// Bulk auth requires all CRUD permissions
|
|
329
|
+
const bulkAuth = async (req, res) => {
|
|
330
|
+
try {
|
|
331
|
+
await check_perms(res.user, res.groups, req.Model, "c");
|
|
332
|
+
await check_perms(res.user, res.groups, req.Model, "r");
|
|
333
|
+
await check_perms(res.user, res.groups, req.Model, "u");
|
|
334
|
+
await check_perms(res.user, res.groups, req.Model, "d");
|
|
335
|
+
}
|
|
336
|
+
catch (err) {
|
|
337
|
+
console.error(err);
|
|
338
|
+
if (err.code)
|
|
339
|
+
throw err;
|
|
340
|
+
throw new errors.ForbiddenError(err.toString());
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
const check_perms = async (user, groups, model, method, item_id) => {
|
|
344
|
+
try {
|
|
345
|
+
const perms = model.schema.get("_perms");
|
|
346
|
+
//If no perms are set, then this isn't an available model
|
|
347
|
+
if (!perms.admin) {
|
|
348
|
+
console.error("Model permissions not set correctly - add an admin section");
|
|
349
|
+
throw new errors.InternalServerError("Model permissions not set correctly - add an admin section");
|
|
350
|
+
}
|
|
351
|
+
//First check if "all" is able to do this. If so, let's get on with it.
|
|
352
|
+
if (perms.all && perms.all.length) {
|
|
353
|
+
if (perms.all.indexOf(method) !== -1) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
//This isn't an 'all' situation, so let's bail if the user isn't logged in
|
|
358
|
+
if (!user) {
|
|
359
|
+
throw new errors.ForbiddenError("User not logged in");
|
|
360
|
+
}
|
|
361
|
+
//Let's check perms in this order - admin, user, group, owner
|
|
362
|
+
//Admin check
|
|
363
|
+
if (user.admin && perms.admin && perms.admin.includes(method)) {
|
|
364
|
+
// console.log("Matched permission 'admin':" + method);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
//User check
|
|
368
|
+
if (perms.user && perms.user.includes(method)) {
|
|
369
|
+
// console.log("Matched permission 'user':" + method);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
//Group check
|
|
373
|
+
for (let group of groups) {
|
|
374
|
+
if (perms[group] && perms[group].includes(method)) {
|
|
375
|
+
// console.log("Matched permission '" + group + "':" + method);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
//Owner check
|
|
380
|
+
if (!item_id)
|
|
381
|
+
throw (`Authorization failed - ${method}`);
|
|
382
|
+
const item = await model.findById(item_id);
|
|
383
|
+
if (item && item._owner_id && item._owner_id.toString() == user._id.toString() && (perms.owner && perms.owner.includes(method)))
|
|
384
|
+
return;
|
|
385
|
+
throw ("Authorization failed");
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
if (err.code)
|
|
389
|
+
throw err;
|
|
390
|
+
throw new errors.ForbiddenError(err.toString());
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
const admin_only = (req, res, next) => {
|
|
394
|
+
if (!res.user) {
|
|
395
|
+
return next(new errors.ForbiddenError("User not logged in"));
|
|
396
|
+
}
|
|
397
|
+
if (!res.user.admin) {
|
|
398
|
+
return next(new errors.ForbiddenError("User not admin"));
|
|
399
|
+
}
|
|
400
|
+
next();
|
|
401
|
+
};
|
|
402
|
+
const Security = {
|
|
403
|
+
init,
|
|
404
|
+
basicAuthData,
|
|
405
|
+
basicAuth,
|
|
406
|
+
encPassword,
|
|
407
|
+
generateApiKey,
|
|
408
|
+
generateToken,
|
|
409
|
+
ensureToken,
|
|
410
|
+
refreshToken,
|
|
411
|
+
revokeToken,
|
|
412
|
+
tokenExpires,
|
|
413
|
+
generateRefreshToken,
|
|
414
|
+
ensureRefreshToken,
|
|
415
|
+
revokeRefreshToken,
|
|
416
|
+
login,
|
|
417
|
+
refresh,
|
|
418
|
+
authenticate,
|
|
419
|
+
auth,
|
|
420
|
+
admin_only,
|
|
421
|
+
check_perms,
|
|
422
|
+
getGroups,
|
|
423
|
+
apiKeyAuth,
|
|
424
|
+
bearerAuth,
|
|
425
|
+
bulkAuth
|
|
426
|
+
};
|
|
427
|
+
module.exports = Security;
|
|
428
|
+
//# sourceMappingURL=security.js.map
|