From 952f8f3dfcb6976b98d33ba3ab5da5286f8de632 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 7 Oct 2024 15:14:03 +0200 Subject: [PATCH 1/5] feat(Docuware): refs #8066 use oath --- back/methods/docuware/core.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 74d922236..760909aa3 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -8,12 +8,22 @@ module.exports = Self => { * @return {object} - The headers */ Self.getOptions = async() => { - const docuwareConfig = await Self.app.models.DocuwareConfig.findOne(); + const {url, username, password} = await Self.app.models.DocuwareConfig.findOne(); + const {IdentityServiceUrl} = await axios.get(`${url}/Home/IdentityServiceInfo`); + const {token_endpoint} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); + const {access_token} = await axios.post(token_endpoint, { + grant_type: 'password', + scope: 'docuware.platform', + client_id: 'docuware.platform.net.client', + username, // falta añadirlos + password // falta añadirlos + }); + const headers = { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', - 'Cookie': docuwareConfig.cookie + 'access_token': access_token } }; -- 2.40.1 From 9695fea48f30207dffe9897993af2446acb23b9a Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 8 Oct 2024 12:05:19 +0200 Subject: [PATCH 2/5] feat(Docuware): refs #8066 add username and password --- back/methods/docuware/core.js | 25 ++++++++++--------- back/models/docuware-config.json | 17 ++++++------- .../11291-purpleChico/00-firstScript.sql | 2 ++ 3 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 db/versions/11291-purpleChico/00-firstScript.sql diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 760909aa3..9545cd878 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -9,15 +9,16 @@ module.exports = Self => { */ Self.getOptions = async() => { const {url, username, password} = await Self.app.models.DocuwareConfig.findOne(); - const {IdentityServiceUrl} = await axios.get(`${url}/Home/IdentityServiceInfo`); - const {token_endpoint} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); - const {access_token} = await axios.post(token_endpoint, { + const {data: {IdentityServiceUrl}} = await axios.get(`${url}/Home/IdentityServiceInfo`); + const {data: {token_endpoint}} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); + const data = await axios.post(token_endpoint, JSON.stringify({ grant_type: 'password', scope: 'docuware.platform', client_id: 'docuware.platform.net.client', - username, // falta añadirlos - password // falta añadirlos - }); + username, + password + })); + console.log('data: ', data); const headers = { headers: { @@ -42,8 +43,8 @@ module.exports = Self => { * @return {number} - The fileCabinet id */ Self.getDialog = async(code, action, fileCabinetId) => { - if (!process.env.NODE_ENV) - return Math.floor(Math.random() + 100); + // if (!process.env.NODE_ENV) + // return Math.floor(Math.random() + 100); const docuwareInfo = await Self.app.models.Docuware.findOne({ where: { @@ -69,8 +70,8 @@ module.exports = Self => { * @return {number} - The fileCabinet id */ Self.getFileCabinet = async code => { - if (!process.env.NODE_ENV) - return Math.floor(Math.random() + 100); + // if (!process.env.NODE_ENV) + // return Math.floor(Math.random() + 100); const options = await Self.getOptions(); const docuwareInfo = await Self.app.models.Docuware.findOne({ @@ -95,7 +96,7 @@ module.exports = Self => { * @return {object} - The data */ Self.get = async(code, filter, parse) => { - if (!process.env.NODE_ENV) return; + // if (!process.env.NODE_ENV) return; const options = await Self.getOptions(); const fileCabinetId = await Self.getFileCabinet(code); @@ -118,7 +119,7 @@ module.exports = Self => { * @return {object} - The data */ Self.getById = async(code, id, parse) => { - if (!process.env.NODE_ENV) return; + // if (!process.env.NODE_ENV) return; const docuwareInfo = await Self.app.models.Docuware.findOne({ fields: ['findById'], diff --git a/back/models/docuware-config.json b/back/models/docuware-config.json index 9d06c4874..244ae2790 100644 --- a/back/models/docuware-config.json +++ b/back/models/docuware-config.json @@ -18,15 +18,12 @@ }, "cookie": { "type": "string" + }, + "username": { + "type": "string" + }, + "password": { + "type": "string" } - }, - "acls": [ - { - "property": "*", - "accessType": "*", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW" - } - ] + } } diff --git a/db/versions/11291-purpleChico/00-firstScript.sql b/db/versions/11291-purpleChico/00-firstScript.sql new file mode 100644 index 000000000..db66c4c98 --- /dev/null +++ b/db/versions/11291-purpleChico/00-firstScript.sql @@ -0,0 +1,2 @@ +ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS username varchar(100) NULL; +ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS password varchar(100) NULL; -- 2.40.1 From 3c881d5e483b904dfcb0be489e313345099c755a Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 15 Oct 2024 08:00:19 +0200 Subject: [PATCH 3/5] refactor(docuware): refs #8066 use Authorization --- back/methods/docuware/core.js | 42 ++++++++++++------- back/methods/docuware/upload.js | 2 +- back/models/docuware-config.json | 5 ++- .../11291-purpleChico/00-firstScript.sql | 2 + 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 9545cd878..6d3bb58ed 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -4,32 +4,45 @@ module.exports = Self => { /** * Returns basic headers * - * @param {string} cookie - The docuware cookie * @return {object} - The headers */ Self.getOptions = async() => { - const {url, username, password} = await Self.app.models.DocuwareConfig.findOne(); - const {data: {IdentityServiceUrl}} = await axios.get(`${url}/Home/IdentityServiceInfo`); - const {data: {token_endpoint}} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); - const data = await axios.post(token_endpoint, JSON.stringify({ - grant_type: 'password', - scope: 'docuware.platform', - client_id: 'docuware.platform.net.client', - username, - password - })); - console.log('data: ', data); + const docuwareConfig = await Self.app.models.DocuwareConfig.findOne(); + const now = new Date().getTime(); + let {url, username, password, token, expired} = docuwareConfig; + + if (!expired || expired < now + 60) { + const {data: {IdentityServiceUrl}} = await axios.get(`${url}/Home/IdentityServiceInfo`); + const {data: {token_endpoint}} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); + const {data} = await axios.post(token_endpoint, { + grant_type: 'password', + scope: 'docuware.platform', + client_id: 'docuware.platform.net.client', + username, + password + }, {headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }}); + + const newToken = data.access_token; + token = data.token_type + ' ' + newToken; + await docuwareConfig.updateAttributes({ + token, + expired: now + data.expires_in + }); + } const headers = { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', - 'access_token': access_token + 'Authorization': token } }; return { - url: docuwareConfig.url, + url, headers }; }; @@ -80,6 +93,7 @@ module.exports = Self => { } }); + console.log('options.headers: ', options.headers); const fileCabinetResponse = await axios.get(`${options.url}/FileCabinets`, options.headers); const fileCabinets = fileCabinetResponse.data.FileCabinet; const fileCabinetId = fileCabinets.find(fileCabinet => fileCabinet.Name === docuwareInfo.fileCabinetName).Id; diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 0102911e0..5b35b7598 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -143,7 +143,7 @@ module.exports = Self => { headers: { 'Content-Type': 'multipart/form-data', 'X-File-ModifiedDate': Date.vnNew(), - 'Cookie': docuwareOptions.headers.headers.Cookie, + 'Authorization': docuwareOptions.headers.headers.Authorization, ...data.getHeaders() }, }; diff --git a/back/models/docuware-config.json b/back/models/docuware-config.json index 244ae2790..b15cb4c03 100644 --- a/back/models/docuware-config.json +++ b/back/models/docuware-config.json @@ -16,7 +16,7 @@ "url": { "type": "string" }, - "cookie": { + "token": { "type": "string" }, "username": { @@ -24,6 +24,9 @@ }, "password": { "type": "string" + }, + "expired":{ + "type": "number" } } } diff --git a/db/versions/11291-purpleChico/00-firstScript.sql b/db/versions/11291-purpleChico/00-firstScript.sql index db66c4c98..e60b90376 100644 --- a/db/versions/11291-purpleChico/00-firstScript.sql +++ b/db/versions/11291-purpleChico/00-firstScript.sql @@ -1,2 +1,4 @@ ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS username varchar(100) NULL; ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS password varchar(100) NULL; +ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS token text NULL; +ALTER TABLE vn.docuwareConfig ADD IF NOT EXISTS expired int(11) NULL; -- 2.40.1 From b1b36a33a0eb7fb2215305ef56bb3e12c650ca8b Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 15 Oct 2024 09:47:39 +0200 Subject: [PATCH 4/5] chore(docuware_core): refs #8066 add returns --- back/methods/docuware/core.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 6d3bb58ed..391a8b4ab 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -56,8 +56,8 @@ module.exports = Self => { * @return {number} - The fileCabinet id */ Self.getDialog = async(code, action, fileCabinetId) => { - // if (!process.env.NODE_ENV) - // return Math.floor(Math.random() + 100); + if (!process.env.NODE_ENV) + return Math.floor(Math.random() + 100); const docuwareInfo = await Self.app.models.Docuware.findOne({ where: { @@ -83,8 +83,8 @@ module.exports = Self => { * @return {number} - The fileCabinet id */ Self.getFileCabinet = async code => { - // if (!process.env.NODE_ENV) - // return Math.floor(Math.random() + 100); + if (!process.env.NODE_ENV) + return Math.floor(Math.random() + 100); const options = await Self.getOptions(); const docuwareInfo = await Self.app.models.Docuware.findOne({ @@ -93,7 +93,6 @@ module.exports = Self => { } }); - console.log('options.headers: ', options.headers); const fileCabinetResponse = await axios.get(`${options.url}/FileCabinets`, options.headers); const fileCabinets = fileCabinetResponse.data.FileCabinet; const fileCabinetId = fileCabinets.find(fileCabinet => fileCabinet.Name === docuwareInfo.fileCabinetName).Id; @@ -110,7 +109,7 @@ module.exports = Self => { * @return {object} - The data */ Self.get = async(code, filter, parse) => { - // if (!process.env.NODE_ENV) return; + if (!process.env.NODE_ENV) return; const options = await Self.getOptions(); const fileCabinetId = await Self.getFileCabinet(code); @@ -133,7 +132,7 @@ module.exports = Self => { * @return {object} - The data */ Self.getById = async(code, id, parse) => { - // if (!process.env.NODE_ENV) return; + if (!process.env.NODE_ENV) return; const docuwareInfo = await Self.app.models.Docuware.findOne({ fields: ['findById'], -- 2.40.1 From c355fcfece4c201ae7ab183d4c1237530f26f755 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 16 Oct 2024 09:03:47 +0200 Subject: [PATCH 5/5] test(docuware_core): refs #8066 adapt --- back/methods/docuware/core.js | 4 +- back/methods/docuware/specs/core.spec.js | 164 ++++++++++++----------- 2 files changed, 86 insertions(+), 82 deletions(-) diff --git a/back/methods/docuware/core.js b/back/methods/docuware/core.js index 391a8b4ab..3de33b786 100644 --- a/back/methods/docuware/core.js +++ b/back/methods/docuware/core.js @@ -8,10 +8,10 @@ module.exports = Self => { */ Self.getOptions = async() => { const docuwareConfig = await Self.app.models.DocuwareConfig.findOne(); - const now = new Date().getTime(); + const now = Date.vnNow(); let {url, username, password, token, expired} = docuwareConfig; - if (!expired || expired < now + 60) { + if (process.env.NODE_ENV && (!expired || expired < now + 60)) { const {data: {IdentityServiceUrl}} = await axios.get(`${url}/Home/IdentityServiceInfo`); const {data: {token_endpoint}} = await axios.get(`${IdentityServiceUrl}/.well-known/openid-configuration`); const {data} = await axios.post(token_endpoint, { diff --git a/back/methods/docuware/specs/core.spec.js b/back/methods/docuware/specs/core.spec.js index cdf8a3b62..47580483d 100644 --- a/back/methods/docuware/specs/core.spec.js +++ b/back/methods/docuware/specs/core.spec.js @@ -2,87 +2,54 @@ const axios = require('axios'); const models = require('vn-loopback/server/server').models; describe('Docuware core', () => { - beforeAll(() => { + const fileCabinetCode = 'deliveryNote'; + beforeAll(async() => { process.env.NODE_ENV = 'testing'; - }); - afterAll(() => { - delete process.env.NODE_ENV; - }); - - describe('getOptions()', () => { - it('should return url and headers', async() => { - const result = await models.Docuware.getOptions(); - - expect(result.url).toBeDefined(); - expect(result.headers).toBeDefined(); + const docuwareInfo = await models.Docuware.findOne({ + where: { + code: fileCabinetCode + } }); - }); - describe('getDialog()', () => { - it('should return dialogId', async() => { - const dialogs = { - data: { - Dialog: [ - { - DisplayName: 'find', - Id: 'getDialogTest' - } - ] - } - }; - spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs))); - const result = await models.Docuware.getDialog('deliveryNote', 'find', 'randomFileCabinetId'); + spyOn(axios, 'get').and.callFake(url => { + if (url.includes('IdentityServiceInfo')) return {data: {IdentityServiceUrl: 'IdentityServiceUrl'}}; + if (url.includes('IdentityServiceUrl')) return {data: {token_endpoint: 'token_endpoint'}}; + if (url.includes('dialogs')) { + return { + data: { + Dialog: [ + { + DisplayName: 'find', + Id: 'getDialogTest' + } + ] + } + }; + } - expect(result).toEqual('getDialogTest'); - }); - }); - - describe('getFileCabinet()', () => { - it('should return fileCabinetId', async() => { - const code = 'deliveryNote'; - const docuwareInfo = await models.Docuware.findOne({ - where: { - code - } - }); - const dialogs = { - data: { + if (url.includes('FileCabinets')) { + return {data: { FileCabinet: [ { Name: docuwareInfo.fileCabinetName, Id: 'getFileCabinetTest' } ] - } - }; - spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(dialogs))); - const result = await models.Docuware.getFileCabinet(code); - - expect(result).toEqual('getFileCabinetTest'); - }); - }); - - describe('get()', () => { - it('should return data without parse', async() => { - spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random())))); - spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random())))); - const data = { - data: { - id: 1 - } - }; - spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data))); - const result = await models.Docuware.get('deliveryNote'); - - expect(result.id).toEqual(1); + }}; + } }); - it('should return data with parse', async() => { - spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random())))); - spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random())))); - const data = { - data: { + spyOn(axios, 'post').and.callFake(url => { + if (url.includes('token_endpoint')) { + return {data: { + access_token: 'access_token', + token_type: 'bearer', + expires_in: 10000 + }}; + } + if (url.includes('DialogExpression')) { + return {data: { Items: [{ Fields: [ { @@ -103,12 +70,52 @@ describe('Docuware core', () => { ] }] } - }; + }; + } + }); + }); + + afterAll(() => { + delete process.env.NODE_ENV; + }); + + describe('getOptions()', () => { + it('should return url and headers', async() => { + const result = await models.Docuware.getOptions(); + + expect(result.url).toBeDefined(); + expect(result.headers).toBeDefined(); + }); + }); + + describe('Dialog()', () => { + it('should return dialogId', async() => { + const result = await models.Docuware.getDialog('deliveryNote', 'find', 'randomFileCabinetId'); + + expect(result).toEqual('getDialogTest'); + }); + }); + + describe('getFileCabinet()', () => { + it('should return fileCabinetId', async() => { + const result = await models.Docuware.getFileCabinet(fileCabinetCode); + + expect(result).toEqual('getFileCabinetTest'); + }); + }); + + describe('get()', () => { + it('should return data without parse', async() => { + const [result] = await models.Docuware.get('deliveryNote'); + + expect(result.firstRequiredField).toEqual(1); + }); + + it('should return data with parse', async() => { const parse = { 'firstRequiredField': 'id', 'secondRequiredField': 'name', }; - spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data))); const [result] = await models.Docuware.get('deliveryNote', null, parse); expect(result.id).toEqual(1); @@ -119,17 +126,14 @@ describe('Docuware core', () => { describe('getById()', () => { it('should return data', async() => { - spyOn(models.Docuware, 'getFileCabinet').and.returnValue((new Promise(resolve => resolve(Math.random())))); - spyOn(models.Docuware, 'getDialog').and.returnValue((new Promise(resolve => resolve(Math.random())))); - const data = { - data: { - id: 1 - } - }; - spyOn(axios, 'post').and.returnValue(new Promise(resolve => resolve(data))); - const result = await models.Docuware.getById('deliveryNote', 1); + spyOn(models.Docuware, 'get'); + await models.Docuware.getById('deliveryNote', 1); - expect(result.id).toEqual(1); + expect(models.Docuware.get).toHaveBeenCalledWith( + 'deliveryNote', + {condition: [Object({DBName: 'N__ALBAR_N', Value: [1]})]}, + undefined + ); }); }); }); -- 2.40.1