Skip to content

Commit

Permalink
Merge pull request #735 from jayharper/master
Browse files Browse the repository at this point in the history
Timestamp Optional
  • Loading branch information
herom committed Oct 5, 2015
2 parents 505f50f + b7132b1 commit c567e8d
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ errors).
WSSecurity implements WS-Security. UsernameToken and PasswordText/PasswordDigest is supported. An instance of WSSecurity is passed to Client.setSecurity.

``` javascript
new WSSecurity(username, password, passwordType)
//'PasswordDigest' or 'PasswordText' default is PasswordText
new WSSecurity(username, password, options)
//the 'options' object is optional and contains properties:
//passwordType: 'PasswordDigest' or 'PasswordText' default is PasswordText
//hasTimeStamp: true or false default is true
```

## Handling XML Attributes, Value and XML (wsdlOptions).
Expand Down
35 changes: 26 additions & 9 deletions lib/security/WSSecurity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
"use strict";

var crypto = require('crypto')
, passwordDigest = require('../utils').passwordDigest;
var crypto = require('crypto');
var passwordDigest = require('../utils').passwordDigest;
var validPasswordTypes = ['PasswordDigest', 'PasswordText'];

function WSSecurity(username, password, passwordType) {
function WSSecurity(username, password, options) {
options = options || {};
this._username = username;
this._password = password;
this._passwordType = passwordType || 'PasswordText';
//must account for backward compatibility for passwordType String param as well as object options defaults: passwordType = 'PasswordText', hasTimeStamp = true
if (typeof options === 'string') {
this._passwordType = options ? options : 'PasswordText';
} else {
this._passwordType = options.passwordType ? options.passwordType : 'PasswordText';
}

if (validPasswordTypes.indexOf(this._passwordType) === -1) {
this._passwordType = 'PasswordText';
}

this._hasTimeStamp = options.hasTimeStamp || typeof options.hasTimeStamp === 'boolean' ? !!options.hasTimeStamp : true;
}

WSSecurity.prototype.toXML = function() {
Expand All @@ -24,7 +37,14 @@ WSSecurity.prototype.toXML = function() {
}
var now = new Date();
var created = getDate(now);
var expires = getDate(new Date(now.getTime() + (1000 * 600)));
var timeStampXml = '';
if (this._hasTimeStamp) {
var expires = getDate( new Date(now.getTime() + (1000 * 600)) );
timeStampXml = "<wsu:Timestamp wsu:Id=\"Timestamp-"+created+"\">" +
"<wsu:Created>"+created+"</wsu:Created>" +
"<wsu:Expires>"+expires+"</wsu:Expires>" +
"</wsu:Timestamp>";
}

var password;
if(this._passwordType === 'PasswordText') {
Expand All @@ -39,10 +59,7 @@ WSSecurity.prototype.toXML = function() {
}

return "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" +
"<wsu:Timestamp wsu:Id=\"Timestamp-" + created + "\">" +
"<wsu:Created>" + created + "</wsu:Created>" +
"<wsu:Expires>" + expires + "</wsu:Expires>" +
"</wsu:Timestamp>" +
timeStampXml +
"<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-" + created + "\">" +
"<wsse:Username>" + this._username + "</wsse:Username>" +
password +
Expand Down
2 changes: 1 addition & 1 deletion test/request-response-samples-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ
}
}
if (securityJSON && securityJSON.type === 'ws') {
client.setSecurity(new WSSecurity(securityJSON.username, securityJSON.password));
client.setSecurity(new WSSecurity(securityJSON.username, securityJSON.password, securityJSON.options));
}
client[methodName](requestJSON, function(err, json, body, soapHeader){
if(requestJSON){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Timestamp wsu:Id="Timestamp-2014-10-12T01:02:03Z"><wsu:Created>2014-10-12T01:02:03Z</wsu:Created><wsu:Expires>2014-10-12T01:12:03Z</wsu:Expires></wsu:Timestamp><wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-2014-10-12T01:02:03Z"><wsse:Username>basicuser</wsse:Username><wsse:Password>basicpass</wsse:Password><wsu:Created>2014-10-12T01:02:03Z</wsu:Created></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body><Request xmlns="http://www.example.com/v1"></Request></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ><soap:Header></soap:Header><soap:Body><Response xmlns="http://www.example.com/v1"></Response></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "ws",
"username": "basicuser",
"password": "basicpass",
"options": "PasswordText"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="MyService" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1">
<xs:element name="Request">
</xs:element>
<xs:element name="Response">
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="InputMessage">
<wsdl:part name="parameter" element="Request">
</wsdl:part>
</wsdl:message>
<wsdl:message name="OutputMessage">
<wsdl:part name="parameter" element="Response">
</wsdl:part>
</wsdl:message>

<wsdl:portType name="MyServicePortType">
<wsdl:operation name="RequestHeaders">
<wsdl:input message="InputMessage">
</wsdl:input>
<wsdl:output message="OutputMessage">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="MyServiceBinding" type="MyServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="RequestHeaders">
<soap:operation soapAction="RequestHeaders"/>
<wsdl:input>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="MyService">
<wsdl:port name="MyServicePort" binding="MyServiceBinding">
<soap:address location="http://www.example.com/v1"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Timestamp wsu:Id="Timestamp-2014-10-12T01:02:03Z"><wsu:Created>2014-10-12T01:02:03Z</wsu:Created><wsu:Expires>2014-10-12T01:12:03Z</wsu:Expires></wsu:Timestamp><wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-2014-10-12T01:02:03Z"><wsse:Username>basicuser</wsse:Username><wsse:Password>basicpass</wsse:Password><wsu:Created>2014-10-12T01:02:03Z</wsu:Created></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body><Request xmlns="http://www.example.com/v1"></Request></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ><soap:Header></soap:Header><soap:Body><Response xmlns="http://www.example.com/v1"></Response></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "ws",
"username": "basicuser",
"password": "basicpass",
"options": {
"passwordType": "invalid",
"hasTimeStamp": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="MyService" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1">
<xs:element name="Request">
</xs:element>
<xs:element name="Response">
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="InputMessage">
<wsdl:part name="parameter" element="Request">
</wsdl:part>
</wsdl:message>
<wsdl:message name="OutputMessage">
<wsdl:part name="parameter" element="Response">
</wsdl:part>
</wsdl:message>

<wsdl:portType name="MyServicePortType">
<wsdl:operation name="RequestHeaders">
<wsdl:input message="InputMessage">
</wsdl:input>
<wsdl:output message="OutputMessage">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="MyServiceBinding" type="MyServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="RequestHeaders">
<soap:operation soapAction="RequestHeaders"/>
<wsdl:input>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="MyService">
<wsdl:port name="MyServicePort" binding="MyServiceBinding">
<soap:address location="http://www.example.com/v1"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-2014-10-12T01:02:03Z"><wsse:Username>basicuser</wsse:Username><wsse:Password>basicpass</wsse:Password><wsu:Created>2014-10-12T01:02:03Z</wsu:Created></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body><Request xmlns="http://www.example.com/v1"></Request></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ><soap:Header></soap:Header><soap:Body><Response xmlns="http://www.example.com/v1"></Response></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "ws",
"username": "basicuser",
"password": "basicpass",
"options": {
"hasTimeStamp": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="MyService" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.example.com/v1" xmlns="http://www.example.com/v1">
<xs:element name="Request">
</xs:element>
<xs:element name="Response">
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="InputMessage">
<wsdl:part name="parameter" element="Request">
</wsdl:part>
</wsdl:message>
<wsdl:message name="OutputMessage">
<wsdl:part name="parameter" element="Response">
</wsdl:part>
</wsdl:message>

<wsdl:portType name="MyServicePortType">
<wsdl:operation name="RequestHeaders">
<wsdl:input message="InputMessage">
</wsdl:input>
<wsdl:output message="OutputMessage">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="MyServiceBinding" type="MyServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="RequestHeaders">
<soap:operation soapAction="RequestHeaders"/>
<wsdl:input>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="MyService">
<wsdl:port name="MyServicePort" binding="MyServiceBinding">
<soap:address location="http://www.example.com/v1"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

0 comments on commit c567e8d

Please sign in to comment.