Skip to content

Commit

Permalink
Merge pull request #21 from gmqz/feature/Add_WSSecurity_Auth_Method
Browse files Browse the repository at this point in the history
Adding WSSecurity Auth method
  • Loading branch information
lehh authored May 26, 2022
2 parents d61e4cb + ef832f4 commit 475a8bb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/soap-constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const SOAP_MODULE_OPTIONS = 'SOAP_MODULE_OPTIONS';
export const BASIC_AUTH = 'basic';
export const WSSECURITY_AUTH = 'wssecurity';
21 changes: 19 additions & 2 deletions src/soap-module-options.type.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
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 = {
interface Auth {
type: typeof BASIC_AUTH | typeof WSSECURITY_AUTH;
username: string;
password: string;
}

export interface BasicAuth extends Auth { }

export interface WSSecurityAuth extends Auth {
options?: WSSecurityOptions
}

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 | WSSecurityAuth;
clientOptions?: IOptions;
};

Expand Down
38 changes: 36 additions & 2 deletions src/soap.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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';
import { BASIC_AUTH, SOAP_MODULE_OPTIONS, WSSECURITY_AUTH } from './soap-constants';

const soapModuleOptionsMock = {
uri: 'some-uri',
Expand Down Expand Up @@ -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_AUTH', async () => {
const auth = soapModuleOptionsMock.auth;
service.soapModuleOptions.auth.type = BASIC_AUTH;

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_AUTH', async () => {
const auth = soapModuleOptionsMock.auth;
service.soapModuleOptions.auth.type = WSSECURITY_AUTH;

soapCreateClientAsyncMock.mockResolvedValue(clientMock);

await service.createAsyncClient();

expect(clientMock.setSecurity).toBeCalledWith(expect.any(WSSecurity));

soapModuleOptionsMock.auth = auth;
});

it('Should return client', async () => {
soapCreateClientAsyncMock.mockResolvedValue(clientMock);

Expand Down
17 changes: 9 additions & 8 deletions src/soap.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { SoapModuleOptions } from './soap-module-options.type';
import { SOAP_MODULE_OPTIONS } from './soap-constants';
import { BasicAuthSecurity, Client, createClientAsync, ISecurity } from 'soap';
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()
export class SoapService {
Expand All @@ -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_AUTH
? new WSSecurity(username, password, (options.auth as WSSecurityAuth).options)
: new BasicAuthSecurity(username, password);

client.setSecurity(basicAuth);
client.setSecurity(authMethod);

return client;

Expand Down

0 comments on commit 475a8bb

Please sign in to comment.