From 7fb9051d14cbbc21aaf14e82d13dbfd99f41c244 Mon Sep 17 00:00:00 2001 From: German Marquez Date: Sun, 22 May 2022 10:39:45 -0300 Subject: [PATCH 1/3] feat:[#20] Adding WSSecurity Auth method --- src/soap-module-options.type.ts | 19 ++++++++++++++++- src/soap.service.spec.ts | 36 ++++++++++++++++++++++++++++++++- src/soap.service.ts | 15 +++++++------- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/soap-module-options.type.ts b/src/soap-module-options.type.ts index 5338abd..c3b66b1 100644 --- a/src/soap-module-options.type.ts +++ b/src/soap-module-options.type.ts @@ -4,14 +4,31 @@ import { ModuleMetadata, Type } from '@nestjs/common'; export { Client, IOptions } from 'soap'; export type BasicAuth = { + type?: string username: string; password: string; }; +export type WSSecurityType = { + type: string + username: string; + password: string; + options?: string; +}; + +export type WSSecurityOptions = { + passwordType?: string; + hasTimeStamp?: boolean; + hasTokenCreated?: boolean; + hasNonce?: boolean; + mustUnderstand?: boolean, + actor?: string; +}; + export type SoapModuleOptions = { uri: string; clientName: string; - auth?: BasicAuth; + auth?: BasicAuth | WSSecurityType; clientOptions?: IOptions; }; diff --git a/src/soap.service.spec.ts b/src/soap.service.spec.ts index 4266dd9..c0b2f87 100644 --- a/src/soap.service.spec.ts +++ b/src/soap.service.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { SoapService } from './soap.service'; import { mocked } from 'ts-jest/utils'; -import { Client, createClientAsync } from 'soap'; +import { BasicAuthSecurity, Client, createClientAsync, WSSecurity } from 'soap'; import { MaybeMocked } from 'ts-jest/dist/utils/testing'; import { SoapModuleOptions } from 'src'; import { SOAP_MODULE_OPTIONS } from './soap-constants'; @@ -81,6 +81,40 @@ describe('SoapService', () => { expect(clientMock.setSecurity).toBeCalled(); }); + it('Should use BasicAuthSecurity if auth.type is not defined', async () => { + soapCreateClientAsyncMock.mockResolvedValue(clientMock); + + await service.createAsyncClient(); + + expect(clientMock.setSecurity).toBeCalledWith(expect.any(BasicAuthSecurity)); + }); + + it('Should use BasicAuthSecurity if auth.type is Basic', async () => { + const auth = soapModuleOptionsMock.auth; + service.soapModuleOptions.auth.type = 'Basic'; + + soapCreateClientAsyncMock.mockResolvedValue(clientMock); + + await service.createAsyncClient(); + + expect(clientMock.setSecurity).toBeCalledWith(expect.any(BasicAuthSecurity)); + + soapModuleOptionsMock.auth = auth; + }); + + it('Should use WSSecurity if auth.type is WSSecurity', async () => { + const auth = soapModuleOptionsMock.auth; + service.soapModuleOptions.auth.type = 'WSSecurity'; + + soapCreateClientAsyncMock.mockResolvedValue(clientMock); + + await service.createAsyncClient(); + + expect(clientMock.setSecurity).toBeCalledWith(expect.any(WSSecurity)); + + soapModuleOptionsMock.auth = auth; + }); + it('Should return client', async () => { soapCreateClientAsyncMock.mockResolvedValue(clientMock); diff --git a/src/soap.service.ts b/src/soap.service.ts index a24cc6e..f276417 100644 --- a/src/soap.service.ts +++ b/src/soap.service.ts @@ -1,7 +1,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common'; -import { SoapModuleOptions } from './soap-module-options.type'; +import { SoapModuleOptions, WSSecurityType } from './soap-module-options.type'; import { SOAP_MODULE_OPTIONS } from './soap-constants'; -import { BasicAuthSecurity, Client, createClientAsync, ISecurity } from 'soap'; +import { BasicAuthSecurity, Client, createClientAsync, ISecurity, WSSecurity } from 'soap'; @Injectable() export class SoapService { @@ -15,12 +15,13 @@ export class SoapService { if (!options.auth) return client; - const basicAuth: ISecurity = new BasicAuthSecurity( - options.auth.username, - options.auth.password, - ); + const {username, password} = options.auth; + + const authMethod: ISecurity = options.auth.type === 'WSSecurity' + ? new WSSecurity(username, password, (options.auth as WSSecurityType).options) + : new BasicAuthSecurity(username, password); - client.setSecurity(basicAuth); + client.setSecurity(authMethod); return client; From ebae896f0bb7955604853f39b62e0cf231eb6811 Mon Sep 17 00:00:00 2001 From: German Marquez Date: Tue, 24 May 2022 11:35:51 -0300 Subject: [PATCH 2/3] Using right type --- src/soap-module-options.type.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/soap-module-options.type.ts b/src/soap-module-options.type.ts index c3b66b1..ddb156a 100644 --- a/src/soap-module-options.type.ts +++ b/src/soap-module-options.type.ts @@ -13,7 +13,7 @@ export type WSSecurityType = { type: string username: string; password: string; - options?: string; + options?: WSSecurityOptions; }; export type WSSecurityOptions = { From ef832f43d3be718105163b612193f698a833b8bb Mon Sep 17 00:00:00 2001 From: German Marquez Date: Tue, 24 May 2022 18:46:42 -0300 Subject: [PATCH 3/3] feat:[#20] Types Improvements --- src/soap-constants.ts | 2 ++ src/soap-module-options.type.ts | 20 ++++++++++---------- src/soap.service.spec.ts | 10 +++++----- src/soap.service.ts | 8 ++++---- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/soap-constants.ts b/src/soap-constants.ts index bcfb68c..45e8b50 100644 --- a/src/soap-constants.ts +++ b/src/soap-constants.ts @@ -1 +1,3 @@ export const SOAP_MODULE_OPTIONS = 'SOAP_MODULE_OPTIONS'; +export const BASIC_AUTH = 'basic'; +export const WSSECURITY_AUTH = 'wssecurity'; diff --git a/src/soap-module-options.type.ts b/src/soap-module-options.type.ts index ddb156a..0029abe 100644 --- a/src/soap-module-options.type.ts +++ b/src/soap-module-options.type.ts @@ -1,20 +1,20 @@ import { IOptions } from 'soap'; import { ModuleMetadata, Type } from '@nestjs/common'; +import { BASIC_AUTH, WSSECURITY_AUTH } from './soap-constants'; export { Client, IOptions } from 'soap'; -export type BasicAuth = { - type?: string +interface Auth { + type: typeof BASIC_AUTH | typeof WSSECURITY_AUTH; username: string; password: string; -}; +} -export type WSSecurityType = { - type: string - username: string; - password: string; - options?: WSSecurityOptions; -}; +export interface BasicAuth extends Auth { } + +export interface WSSecurityAuth extends Auth { + options?: WSSecurityOptions +} export type WSSecurityOptions = { passwordType?: string; @@ -28,7 +28,7 @@ export type WSSecurityOptions = { export type SoapModuleOptions = { uri: string; clientName: string; - auth?: BasicAuth | WSSecurityType; + auth?: BasicAuth | WSSecurityAuth; clientOptions?: IOptions; }; diff --git a/src/soap.service.spec.ts b/src/soap.service.spec.ts index c0b2f87..b0eeb3e 100644 --- a/src/soap.service.spec.ts +++ b/src/soap.service.spec.ts @@ -4,7 +4,7 @@ import { mocked } from 'ts-jest/utils'; import { BasicAuthSecurity, Client, createClientAsync, WSSecurity } from 'soap'; import { MaybeMocked } from 'ts-jest/dist/utils/testing'; import { SoapModuleOptions } from 'src'; -import { SOAP_MODULE_OPTIONS } from './soap-constants'; +import { BASIC_AUTH, SOAP_MODULE_OPTIONS, WSSECURITY_AUTH } from './soap-constants'; const soapModuleOptionsMock = { uri: 'some-uri', @@ -89,9 +89,9 @@ describe('SoapService', () => { expect(clientMock.setSecurity).toBeCalledWith(expect.any(BasicAuthSecurity)); }); - it('Should use BasicAuthSecurity if auth.type is Basic', async () => { + it('Should use BasicAuthSecurity if auth.type is BASIC_AUTH', async () => { const auth = soapModuleOptionsMock.auth; - service.soapModuleOptions.auth.type = 'Basic'; + service.soapModuleOptions.auth.type = BASIC_AUTH; soapCreateClientAsyncMock.mockResolvedValue(clientMock); @@ -102,9 +102,9 @@ describe('SoapService', () => { soapModuleOptionsMock.auth = auth; }); - it('Should use WSSecurity if auth.type is WSSecurity', async () => { + it('Should use WSSecurity if auth.type is WSSECURITY_AUTH', async () => { const auth = soapModuleOptionsMock.auth; - service.soapModuleOptions.auth.type = 'WSSecurity'; + service.soapModuleOptions.auth.type = WSSECURITY_AUTH; soapCreateClientAsyncMock.mockResolvedValue(clientMock); diff --git a/src/soap.service.ts b/src/soap.service.ts index f276417..22f94f5 100644 --- a/src/soap.service.ts +++ b/src/soap.service.ts @@ -1,6 +1,6 @@ import { Inject, Injectable, Logger } from '@nestjs/common'; -import { SoapModuleOptions, WSSecurityType } from './soap-module-options.type'; -import { SOAP_MODULE_OPTIONS } from './soap-constants'; +import { SoapModuleOptions, WSSecurityAuth } from './soap-module-options.type'; +import { SOAP_MODULE_OPTIONS, WSSECURITY_AUTH } from './soap-constants'; import { BasicAuthSecurity, Client, createClientAsync, ISecurity, WSSecurity } from 'soap'; @Injectable() @@ -17,8 +17,8 @@ export class SoapService { const {username, password} = options.auth; - const authMethod: ISecurity = options.auth.type === 'WSSecurity' - ? new WSSecurity(username, password, (options.auth as WSSecurityType).options) + const authMethod: ISecurity = options.auth.type === WSSECURITY_AUTH + ? new WSSecurity(username, password, (options.auth as WSSecurityAuth).options) : new BasicAuthSecurity(username, password); client.setSecurity(authMethod);