Skip to content

Commit 902bc83

Browse files
committed
Adds promise server authentication
1 parent 57cf2b7 commit 902bc83

3 files changed

Lines changed: 77 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chriswiggins/soap",
3-
"version": "0.27.4",
3+
"version": "0.27.5",
44
"description": "A minimal node SOAP client",
55
"engines": {
66
"node": ">=4.0.0"

src/server.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Server extends EventEmitter {
7373
public services: IServices;
7474
public log: (type: string, data: any) => any;
7575
public authorizeConnection: (req: Request, res?: Response) => boolean;
76-
public authenticate: (security: IServerAuthenticate, processAuthResult?: (result: boolean) => void, req?: Request, obj?: any) => boolean | void | Promise<void>;
76+
public authenticate: (security: IServerAuthenticate, processAuthResult?: (result: boolean) => void, req?: Request, obj?: any) => boolean | void | Promise<boolean>;
7777

7878
private wsdl: WSDL;
7979
private suppressStack: boolean;
@@ -385,10 +385,27 @@ export class Server extends EventEmitter {
385385
// Authentication
386386
if (typeof authenticate === 'function') {
387387
let authResultProcessed = false;
388-
const processAuthResult = (authResult) => {
389-
if (!authResultProcessed && (authResult || authResult === false)) {
390-
authResultProcessed = true;
391-
if (authResult) {
388+
const processAuthResult = (authResult: boolean | Error) => {
389+
if (authResultProcessed) {
390+
return;
391+
}
392+
393+
authResultProcessed = true;
394+
// Handle errors
395+
if (authResult instanceof Error) {
396+
return this._sendError({
397+
Code: {
398+
Value: 'SOAP-ENV:Server',
399+
Subcode: { value: 'InternalServerError' },
400+
},
401+
Reason: { Text: authResult.toString() },
402+
statusCode: 500,
403+
}, callback, includeTimestamp);
404+
}
405+
406+
// Handle actual results
407+
if (typeof authResult === 'boolean') {
408+
if (authResult === true) {
392409
try {
393410
process();
394411
} catch (error) {
@@ -417,7 +434,17 @@ export class Server extends EventEmitter {
417434
}
418435
};
419436

420-
processAuthResult(authenticate(obj.Header && obj.Header.Security, processAuthResult, req, obj));
437+
const functionResult = authenticate(obj.Header && obj.Header.Security, processAuthResult, req, obj);
438+
if (isPromiseLike<boolean>(functionResult)) {
439+
functionResult.then((result: boolean) => {
440+
processAuthResult(result);
441+
}, (err: any) => {
442+
processAuthResult(err);
443+
});
444+
}
445+
if (typeof functionResult === 'boolean') {
446+
processAuthResult(functionResult);
447+
}
421448
} else {
422449
throw new Error('Invalid authenticate function (not a function)');
423450
}

test/server-authentication-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ describe('SOAP Server', function() {
103103
});
104104
});
105105

106+
it('should succeed on valid promise authentication', function(done) {
107+
test.authenticate = function(security) {
108+
return new Promise((resolve) => {
109+
setTimeout(function delayed() {
110+
resolve(true);
111+
}, 10);
112+
})
113+
};
114+
115+
soap.createClient(test.baseUrl + '/stockquote?wsdl', function(err, client) {
116+
assert.ifError(err);
117+
118+
client.GetLastTradePrice({ tickerSymbol: 'AAPL'}, function(err, result) {
119+
assert.ifError(err);
120+
assert.equal(19.56, parseFloat(result.price));
121+
done();
122+
});
123+
});
124+
});
125+
106126
it('should fail on invalid synchronous authentication', function(done) {
107127
test.authenticate = function(security, callback) {
108128
setTimeout(function delayed() {
@@ -145,4 +165,27 @@ describe('SOAP Server', function() {
145165
});
146166
});
147167
});
168+
169+
it('should fail on invalid promise authentication', function(done) {
170+
test.authenticate = function(security) {
171+
return new Promise((resolve) => {
172+
setTimeout(function delayed() {
173+
resolve(false);
174+
}, 10);
175+
})
176+
};
177+
178+
soap.createClient(test.baseUrl + '/stockquote?wsdl', function(err, client) {
179+
assert.ifError(err);
180+
181+
client.GetLastTradePrice({ tickerSymbol: 'AAPL'}, function (err, result) {
182+
assert.ok(err);
183+
assert.ok(err.root.Envelope.Body.Fault.Code.Value);
184+
assert.equal(err.root.Envelope.Body.Fault.Code.Value, 'SOAP-ENV:Client');
185+
assert.ok(err.root.Envelope.Body.Fault.Code.Subcode.value);
186+
assert.equal(err.root.Envelope.Body.Fault.Code.Subcode.value, 'AuthenticationFailure');
187+
done();
188+
});
189+
});
190+
});
148191
});

0 commit comments

Comments
 (0)