diff --git a/plugins/core/base/routes/admin/migration.js b/plugins/core/base/routes/admin/migration.js index c0c66fe8..0adb1578 100644 --- a/plugins/core/base/routes/admin/migration.js +++ b/plugins/core/base/routes/admin/migration.js @@ -10,13 +10,15 @@ module.exports = { path: "/admin/migration/{tool}/{id?}", method: "GET", config: { - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { - credentials: true + credentials: true, }, description: "Executes migration of items in database for specified tool or for a single item with the specified id respectively", - tags: ["api"] + tags: ["api"], }, handler: async (request, h) => { const tool = request.params.tool; @@ -33,8 +35,8 @@ module.exports = { ignoreInactive: true, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } + artifacts: request.auth.artifacts, + }, }); const migrationStatus = await migrateItem( item, @@ -42,7 +44,7 @@ module.exports = { request.server.app.db ); return { - status: migrationStatus.status + status: migrationStatus.status, }; } catch (err) { Bounce.rethrow(err, "system"); @@ -53,11 +55,11 @@ module.exports = { tool, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } + artifacts: request.auth.artifacts, + }, }); - let migrationStatuses = items.map(async item => { + let migrationStatuses = items.map(async (item) => { return await migrateItem(item, toolBaseUrl, request.server.app.db); }); @@ -77,13 +79,13 @@ module.exports = { return stats; } - } + }, }; function migrateItem(item, toolBaseUrl, db) { return new Promise(async (resolve, reject) => { const body = { - item: item + item: item, }; try { @@ -91,8 +93,8 @@ function migrateItem(item, toolBaseUrl, db) { method: "POST", body: JSON.stringify(body), headers: { - "Content-Type": "application/json" - } + "Content-Type": "application/json", + }, }); if (response.status === 200) { @@ -100,14 +102,14 @@ function migrateItem(item, toolBaseUrl, db) { await saveItem(json.item, db); return resolve({ id: item._id, - status: statusUpdated + status: statusUpdated, }); } if (response.status === 304) { return resolve({ id: item._id, - status: statusNotUpdated + status: statusNotUpdated, }); } @@ -115,7 +117,7 @@ function migrateItem(item, toolBaseUrl, db) { } catch (e) { resolve({ id: item._id, - status: statusFailed + status: statusFailed, }); } }); diff --git a/plugins/core/base/routes/item.js b/plugins/core/base/routes/item.js index 64ffee86..af66c9ab 100644 --- a/plugins/core/base/routes/item.js +++ b/plugins/core/base/routes/item.js @@ -25,7 +25,7 @@ function validateAgainstSchema(request, doc) { reject( Boom.badRequest( validate.errors - .map(error => { + .map((error) => { return JSON.stringify(error); }) .join("\n") @@ -41,30 +41,30 @@ module.exports = { method: "GET", options: { auth: { - strategies: ["q-auth"], - mode: "optional" + strategies: ["q-auth-azure", "q-auth-ld"], + mode: "optional", }, cors: { - credentials: true + credentials: true, }, validate: { params: { - id: Joi.string().required() - } + id: Joi.string().required(), + }, }, description: "gets the item with the given id from the database", - tags: ["api", "editor"] + tags: ["api", "editor"], }, - handler: async function(request, h) { + handler: async function (request, h) { return request.server.methods.db.item.getById({ id: request.params.id, ignoreInactive: true, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } + artifacts: request.auth.artifacts, + }, }); - } + }, }, post: { path: "/item", @@ -75,21 +75,23 @@ module.exports = { _id: Joi.string().optional(), _rev: Joi.forbidden(), title: Joi.string().required(), - tool: Joi.string().required() + tool: Joi.string().required(), }, options: { - allowUnknown: true - } + allowUnknown: true, + }, + }, + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], }, - auth: "q-auth", cors: { - credentials: true + credentials: true, }, description: "stores a new item to the database and returns the id among other things", - tags: ["api", "editor"] + tags: ["api", "editor"], }, - handler: async function(request, h) { + handler: async function (request, h) { let doc = request.payload; let now = new Date(); @@ -118,8 +120,8 @@ module.exports = { doc, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } + artifacts: request.auth.artifacts, + }, }); docDiff._id = res.id; @@ -127,11 +129,11 @@ module.exports = { const savedDoc = Object.assign(doc, docDiff); request.server.events.emit("item.new", { - newItem: savedDoc + newItem: savedDoc, }); return docDiff; - } + }, }, put: { path: "/item", @@ -142,21 +144,23 @@ module.exports = { _id: Joi.string().required(), _rev: Joi.string().required(), title: Joi.string().required(), - tool: Joi.string().required() + tool: Joi.string().required(), }, options: { - allowUnknown: true - } + allowUnknown: true, + }, + }, + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], }, - auth: "q-auth", cors: { - credentials: true + credentials: true, }, description: "updates an existing item to the database and returns the new revision number among other things", - tags: ["api", "editor"] + tags: ["api", "editor"], }, - handler: async function(request, h) { + handler: async function (request, h) { let doc = request.payload; let now = new Date(); try { @@ -178,7 +182,7 @@ module.exports = { const oldDoc = await request.server.methods.db.item.getById({ id: request.payload._id, - ignoreInactive: true + ignoreInactive: true, }); // if the active state change to true, we set activateDate @@ -206,8 +210,8 @@ module.exports = { doc, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } + artifacts: request.auth.artifacts, + }, }); docDiff._rev = res.rev; @@ -215,7 +219,7 @@ module.exports = { const eventData = { newItem: savedDoc, - oldItem: oldDoc + oldItem: oldDoc, }; if (isNewActive) { @@ -231,6 +235,6 @@ module.exports = { request.server.events.emit("item.update", eventData); return docDiff; - } - } + }, + }, }; diff --git a/plugins/core/base/routes/search.js b/plugins/core/base/routes/search.js index 3cd8893b..0927959a 100644 --- a/plugins/core/base/routes/search.js +++ b/plugins/core/base/routes/search.js @@ -5,7 +5,7 @@ module.exports = { method: "GET", options: { auth: { - strategy: "q-auth", + strategies: ["q-auth-azure", "q-auth-ld"], mode: "try", }, cors: { diff --git a/plugins/core/editor/routes/tools-ordered-by-user-usage.js b/plugins/core/editor/routes/tools-ordered-by-user-usage.js index 8812bc54..be16277f 100644 --- a/plugins/core/editor/routes/tools-ordered-by-user-usage.js +++ b/plugins/core/editor/routes/tools-ordered-by-user-usage.js @@ -2,30 +2,31 @@ module.exports = { path: "/editor/tools-ordered-by-user-usage", method: "GET", options: { - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { - credentials: true + credentials: true, }, description: "Returns all available Q tool names", - tags: ["api", "editor"] + tags: ["api", "editor"], }, handler: async (request, h) => { const username = request.auth.credentials.name; - const toolsWithUsageByUser = await request.server.methods.db.tools.getWithUserUsage( - { + const toolsWithUsageByUser = + await request.server.methods.db.tools.getWithUserUsage({ username, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } - } - ); + artifacts: request.auth.artifacts, + }, + }); return toolsWithUsageByUser .sort((a, b) => { return b.usage - a.usage; }) - .map(row => { + .map((row) => { return row.tool; }); - } + }, }; diff --git a/plugins/file/index.js b/plugins/file/index.js index e47405ac..f4b0a2be 100644 --- a/plugins/file/index.js +++ b/plugins/file/index.js @@ -1,5 +1,9 @@ const Boom = require("@hapi/boom"); -const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3"); +const { + S3Client, + PutObjectCommand, + GetObjectCommand, +} = require("@aws-sdk/client-s3"); const Mimos = require("@hapi/mimos"); const mimos = new Mimos.Mimos(); const hasha = require("hasha"); @@ -56,7 +60,9 @@ module.exports = { method: "POST", path: "/file", options: { - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { credentials: true, additionalHeaders: [ @@ -157,10 +163,12 @@ module.exports = { }, handler: async function (request, h) { try { - const data = await s3Client.send(new GetObjectCommand({ - Bucket: options.s3Bucket, - Key: request.params.fileKey, - })); + const data = await s3Client.send( + new GetObjectCommand({ + Bucket: options.s3Bucket, + Key: request.params.fileKey, + }) + ); return h .response(data.Body) .header( diff --git a/plugins/fixtures/routes.js b/plugins/fixtures/routes.js index 6485614a..064a0306 100644 --- a/plugins/fixtures/routes.js +++ b/plugins/fixtures/routes.js @@ -5,20 +5,20 @@ module.exports = { path: "/fixtures/data", method: "POST", options: { - auth: "q-auth", + auth: "q-auth-azure", cors: { - credentials: true + credentials: true, }, description: "creates fixture data items for all tools and stores them in db", - tags: ["api", "fixtures", "non-critical"] + tags: ["api", "fixtures", "non-critical"], }, handler: async (request, h) => { try { const fixtures = await request.server.methods.plugins.q.fixtures.get(); let result = { saved: [], - errors: [] + errors: [], }; // update all existing fixture data items in db @@ -28,17 +28,15 @@ module.exports = { method: "PUT", payload: item, auth: { - strategy: "q-auth", - credentials: request.auth.credentials - } + strategy: "q-auth-azure", + credentials: request.auth.credentials, + }, }); if (updateResponse.statusCode === 200) { result.saved.push(item._id); } else { result.errors.push( - `Error ${updateResponse.statusCode} - ${ - updateResponse.result.message - } for ${item._id}` + `Error ${updateResponse.statusCode} - ${updateResponse.result.message} for ${item._id}` ); } } @@ -50,17 +48,15 @@ module.exports = { method: "POST", payload: item, auth: { - strategy: "q-auth", - credentials: request.auth.credentials - } + strategy: "q-auth-azure", + credentials: request.auth.credentials, + }, }); if (createResponse.statusCode === 200) { result.saved.push(item._id); } else { result.errors.push( - `Error ${createResponse.statusCode} - ${ - createResponse.result.message - } for ${item._id}` + `Error ${createResponse.statusCode} - ${createResponse.result.message} for ${item._id}` ); } } @@ -68,28 +64,28 @@ module.exports = { } catch (e) { return Boom.internal(e.message); } - } + }, }, getExistingFixtureIds: { path: "/fixtures/data", method: "GET", options: { description: "returns all available fixture data ids", - tags: ["api", "fixtures", "non-critical"] + tags: ["api", "fixtures", "non-critical"], }, handler: async (request, h) => { try { const fixtures = await request.server.methods.plugins.q.fixtures.get(); if (fixtures) { - return fixtures.existing.map(item => { + return fixtures.existing.map((item) => { return { - _id: item._id + _id: item._id, }; }); } } catch (e) { return Boom.internal(e.message); } - } - } + }, + }, }; diff --git a/plugins/statistics/index.js b/plugins/statistics/index.js index 491caa5d..520ae038 100644 --- a/plugins/statistics/index.js +++ b/plugins/statistics/index.js @@ -4,41 +4,40 @@ const Boom = require("@hapi/boom"); module.exports = { name: "q-statistics", dependencies: "q-db", - register: async function(server, options) { + register: async function (server, options) { server.route({ path: "/statistics/number-of-items/{since?}", method: "GET", config: { auth: { - strategies: ["q-auth"], - mode: "optional" + strategies: ["q-auth-azure", "q-auth-ld"], + mode: "optional", }, cors: { - credentials: true + credentials: true, }, validate: { params: { - since: Joi.number().optional() - } + since: Joi.number().optional(), + }, }, description: "returns the number of items. If given since the timestamp passed.", - tags: ["api", "statistics", "non-critical"] + tags: ["api", "statistics", "non-critical"], }, handler: async (request, h) => { - const value = await request.server.methods.db.statistics.getNumberOfItems( - { + const value = + await request.server.methods.db.statistics.getNumberOfItems({ since: request.params.since, session: { credentials: request.auth.credentials, - artifacts: request.auth.artifacts - } - } - ); + artifacts: request.auth.artifacts, + }, + }); return { - value: value + value: value, }; - } + }, }); - } + }, }; diff --git a/plugins/tasks/index.js b/plugins/tasks/index.js index b9e620a5..f008cb2b 100644 --- a/plugins/tasks/index.js +++ b/plugins/tasks/index.js @@ -1,14 +1,14 @@ const Hoek = require("@hapi/hoek"); const defaults = { - tasksConfig: {} + tasksConfig: {}, }; module.exports = { name: "q-tasks", - register: async function(server, options) { + register: async function (server, options) { const settings = Hoek.applyToDefaults(defaults, options); - settings.tasksConfig.tasks.forEach(task => { + settings.tasksConfig.tasks.forEach((task) => { server.route(task.route); }); server.route({ @@ -17,32 +17,34 @@ module.exports = { options: { description: "Returns configuration for tasks", tags: ["api", "tasks"], - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { - credentials: true - } + credentials: true, + }, }, handler: (request, h) => { return { tasks: settings.tasksConfig.tasks - .filter(task => { + .filter((task) => { // if the tasks has no onlyRoles property, it's available for everyone if (!task.hasOwnProperty("onlyRoles")) { return true; } // otherwise we only include it in the config if the authenticated user as roles in the credentials and these roles include one role defined in onlyRoles. return task.onlyRoles.some( - role => + (role) => request.auth.credentials.roles && request.auth.credentials.roles.includes(role) ); }) - .map(task => { + .map((task) => { delete task.route.handler; return task; - }) + }), }; - } + }, }); - } + }, }; diff --git a/test/config/tasks.js b/test/config/tasks.js index 514dcf54..1899aac1 100644 --- a/test/config/tasks.js +++ b/test/config/tasks.js @@ -10,30 +10,32 @@ const tasks = { path: "/tasks/test", method: "POST", options: { - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { - credentials: true - } + credentials: true, + }, }, - handler: async function(request, h) { + handler: async function (request, h) { return { type: "json", data: { label: "test 1", - content: request.payload - } + content: request.payload, + }, }; - } + }, }, schema: { type: "object", properties: { someTaskInput: { title: "task input", - type: "string" - } - } - } + type: "string", + }, + }, + }, }, { id: "adminTask", @@ -42,34 +44,36 @@ const tasks = { path: "/tasks/admintest", method: "POST", options: { - auth: "q-auth", + auth: { + strategies: ["q-auth-azure", "q-auth-ld"], + }, cors: { - credentials: true - } + credentials: true, + }, }, - handler: async function(request, h) { + handler: async function (request, h) { return { type: "json", data: { label: "test 1", - content: request.payload - } + content: request.payload, + }, }; - } + }, }, schema: { type: "object", properties: { someTaskInput: { title: "task input", - type: "string" - } - } + type: "string", + }, + }, }, - onlyRoles: ["admin"] - } - ] - } + onlyRoles: ["admin"], + }, + ], + }, }; const env = process.env.APP_ENV || "local"; diff --git a/test/e2e-tests.js b/test/e2e-tests.js index 414b7d08..d287063d 100644 --- a/test/e2e-tests.js +++ b/test/e2e-tests.js @@ -85,7 +85,7 @@ lab.experiment("core item", () => { const request = { method: "POST", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -103,7 +103,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -119,7 +119,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -142,7 +142,7 @@ lab.experiment("core item", () => { const request = { method: "POST", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -168,7 +168,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -199,7 +199,7 @@ lab.experiment("core item", () => { const request = { method: "POST", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -230,7 +230,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -262,7 +262,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -294,7 +294,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -326,7 +326,7 @@ lab.experiment("core item", () => { const request = { method: "PUT", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, url: "/item", @@ -801,7 +801,7 @@ lab.experiment("fixture data plugin", () => { method: "POST", url: "/fixtures/data", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, }); @@ -868,7 +868,7 @@ lab.experiment("tasks plugin", () => { method: "GET", url: "/tasks", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, }); @@ -880,7 +880,7 @@ lab.experiment("tasks plugin", () => { method: "GET", url: "/tasks", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass", roles: ["admin"] }, }, }); @@ -897,7 +897,7 @@ lab.experiment("tasks plugin", () => { method: "POST", url: "/tasks/test", auth: { - strategy: "q-auth", + strategy: "q-auth-azure", credentials: { username: "user", password: "pass" }, }, payload: payload, diff --git a/test/server.js b/test/server.js index c3e0e1b0..6f0c0981 100644 --- a/test/server.js +++ b/test/server.js @@ -6,24 +6,26 @@ function getServer() { port: process.env.PORT || 3333, app: { tools: require("./config/tools.js"), - targets: require("./config/targets.js") + targets: require("./config/targets.js"), }, routes: { - cors: true - } + cors: true, + }, }); server.validator(Joi); // mock the auth strategy - server.auth.scheme("mock", function(server, options) { + server.auth.scheme("mock", function (server, options) { return { - authenticate: function(request, h) { + authenticate: function (request, h) { return h.authenticated({ credentials: "user" }); - } + }, }; }); - server.auth.strategy("q-auth", "mock"); + server.auth.strategy("q-auth-azure", "mock"); + server.auth.strategy("q-auth-ld", "mock"); + server.auth.default("q-auth-azure"); return server; }