npm install ws.js-buffer-fixjavascript
var ws = require('ws.js')
, Http = ws.Http
, Security = ws.Security
, UsernameToken = ws.UsernameToken
var request = '' +
' ' +
'' +
'' +
'123' +
' ' +
'' +
' '
var ctx = { request: request
, url: "http://service/security"
, action: "http://tempuri.org/EchoString"
, contentType: "text/xml"
}
var handlers = [ new Security({}, [new UsernameToken({username: "yaron", password: "1234"})])
, new Http()
]
ws.send(handlers, ctx, function(ctx) {
console.log("response: " + ctx.response);
})
```
==>
```xml
2012-02-26T11:03:40Z
2012-02-26T11:08:40Z
yaron
1234
123
```
$3
```javascript
var ws = require('ws.js')
, fs = require('fs')
, sec = ws.Security
, X509BinarySecurityToken = ws.X509BinarySecurityToken
, FileKeyInfo = require('xml-crypto').FileKeyInfo
var x509 = new X509BinarySecurityToken(
{ "key": fs.readFileSync("client.pem").toString()})
var signature = new ws.Signature(x509)
signature.addReference("//*[local-name(.)='Body']")
signature.addReference("//*[local-name(.)='Timestamp']")
var sec = new ws.Security({}, [ x509, signature ])
var handlers = [ new ws.Addr("http://www.w3.org/2005/08/addressing")
, sec
, new ws.Http()
]
request = "" +
" " +
"" +
"" +
"123 " +
" " +
"" +
" "
var ctx = { request: request
, url: "http://localhost:7171/Service/signature"
, action: "http://tempuri.org/IService/GetData"
, contentType: "text/xml"
}
ws.send(handlers, ctx, function(ctx) {
console.log("status " + ctx.statusCode)
console.log("messagse " + ctx.response)
})
```
==>
```xml
http://tempuri.org/IService/GetData
http://localhost:8888/
ca62b7d7-4f74-75ed-9f5c-b09b173f6747
http://www.w3.org/2005/08/addressing/role/anonymous
2012-05-25T12:18:38Z
2012-05-25T12:23:38Z
MIIBxDCCAW6gAwIBAgIQxUSXFzWJYYtOZnmmuOMKkjANBgkqhkiG9w0BAQQFADAWMRQwEgYDVQQDEwtSb290IEFnZW5jeTAeFw0wMzA3MDgxODQ3NTlaFw0zOTEyMzEyMzU5NTlaMB8xHTAbBgNVBAMTFFdTRTJRdWlja1N0YXJ0Q2xpZW50MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+L6aB9x928noY4+0QBsXnxkQE4quJl7c3PUPdVu7k9A02hRG481XIfWhrDY5i7OEB7KGW7qFJotLLeMec/UkKUwCgv3VvJrs2nE9xO3SSWIdNzADukYh+Cxt+FUU6tUkDeqg7dqwivOXhuOTRyOI3HqbWTbumaLdc8jufz2LhaQIDAQABo0swSTBHBgNVHQEEQDA+gBAS5AktBh0dTwCNYSHcFmRjoRgwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3mCEAY3bACqAGSKEc+41KpcNfQwDQYJKoZIhvcNAQEEBQADQQAfIbnMPVYkNNfX1tG1F+qfLhHwJdfDUZuPyRPucWF5qkh6sSdWVBY5sT/txBnVJGziyO8DPYdu2fPMER8ajJfl
fxQc0PEh2GHA43IXltm6gjbccsA=
L+vrfEszbn2ZtXiWfNyDG8nM1e8=
AcOb1KJHpyQnnChEZFKaIjVag8iREL7g+LEXnvHQ/wH9ffIj0s1sdF2xO2AvQkqLUeefIEPgyiKUPR9sk8RMchJxv7UDx8wGuvD2WFPbd3yy50qcsu2UkiUm0lW/R3lpv88w83Z95LFZ1yq1MnOe2Sh0y5esoCbJo1fJETS8mQ0=
123
```
Notes:
By default incoming signatures are not validates. To validate these signatures when you create the security channel specify the validateResponseSignature parameter:
```javascript
var sec = new ws.Security({"validateResponseSignature": true} ...
```
Next specify the server certificate (the public key corresponding to the server private sgining key):
```javascript
sec.options.responseKeyInfoProvider = new FileKeyInfo("./examples/server_public.pem")
```
In the future the server certificate will be extracted from the BinarySecurityToken automatically (when available).
$3
```javascript
var ws = require('ws.js')
, Http = ws.Http
, Mtom = ws.Mtom
var request = '' +
'' +
'' +
' ' +
' ' +
' ' +
' '
var ctx = { request: request
, contentType: "application/soap+xml"
, url: "http://localhost:7171/Service/mtom"
, action: "http://tempuri.org/IService/EchoFiles"
}
//add attachment to the soap request
ws.addAttachment(ctx, "request", "//*[local-name(.)='File1']",
"me.jpg", "image/jpeg")
var handlers = [ new Mtom()
, new Http()
];
ws.send(handlers, ctx, function(ctx) {
//read an attachment from the soap response
var file = ws.getAttachment(ctx, "response", "//*[local-name(.)='File1']")
fs.writeFileSync("result.jpg", file)
})
```
==>
```xml
--my_unique_boundary
Content-ID:
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml"
--my_unique_boundary
Content-ID:
Content-Transfer-Encoding: binary
Content-Type: image/jpeg
[binary here...]
```
$3
```javascript
var ws = require('ws.js')
, Http = ws.Http
, Addr = ws.Addr
, ctx = { request: "" +
" " +
"" +
"" +
"123" +
" " +
"" +
" "
, url: "http://localhost/service"
, action: "http://tempuri.org/EchoString"
, contentType: "text/xml"
}
var handlers = [ new Addr("http://schemas.xmlsoap.org/ws/2004/08/addressing")
, new Http()
]
ws.send(handlers, ctx, function(ctx) {
console.log("response: " + ctx.response);
})
```
==>
```xml
http://tempuri.org/EchoString
http://server/wsa/
6c4189e5-60f5-195e-b7ce-4e236d63c379
http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
123
```
$3
Just specify an https address in any of the previous samples.
$3
```javascript
var ws = require('ws.js')
, Http = ws.Http
, Addr = ws.Addr
, Mtom = ws.Mtom
, Security = ws.Security
, UsernameToken = ws.UsernameToken
, ctx = { request: "" +
" " +
"" +
"" +
" " +
" " +
"" +
" "
, url: "https://localhost/service"
, action: "http://tempuri.org/EchoString"
, contentType: "text/xml"
}
ws.addAttachment(ctx, "request", "//*[local-name(.)='File1']",
"me.jpg", "image/jpeg")
var handlers = [ new Security({}, [new UsernameToken({username: "yaron", password: "1234"})])
, new Addr("http://ws-addressing/v8")
, new Mtom() //Mtom must be after everything except http
, new Http()
]
ws.send(handlers, ctx, function(ctx) {
console.log("response: " + ctx.response);
})
````
