Module soap
ballerina/soap Ballerina library
Overview
This module offers a set of APIs that facilitate the transmission of XML requests to a SOAP backend. It excels in managing security policies within SOAP requests, ensuring the transmission of secured SOAP envelopes. Moreover, it possesses the capability to efficiently extract data from security-applied SOAP responses.
SOAP module abstracts out the details of the creation of a SOAP envelope, headers, and the body in a SOAP message.
Client
The Client
is used to connect to and interact with SOAP
endpoints.
SOAP 1.1 Client
import ballerina/soap.soap11; soap11:Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL");
SOAP 1.2 Client
import ballerina/soap.soap12; soap12:Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL");
APIs associated with SOAP
- Send & Receive: Sends SOAP request and receives a response.
- Send Only: Fires and forgets requests. Sends the request without the possibility of any response from the service.
The SOAP 1.1 specification requires the inclusion of the action
parameter as a mandatory component within its APIs. In contrast, SOAP 1.2 relaxes this requirement, making the action parameter optional.
Example: Send & Receive
import ballerina/soap.soap11; public function main() returns error? { soap11:Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL"); xml envelope = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <quer:Add xmlns:quer="http://tempuri.org/"> <quer:intA>2</quer:intA> <quer:intB>3</quer:intB> </quer:Add> </soap:Body> </soap:Envelope>`; xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add"); }
Example: Send Only
import ballerina/soap.soap11; public function main() returns error? { soap11:Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL"); xml envelope = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <quer:Add xmlns:quer="http://tempuri.org/"> <quer:intA>2</quer:intA> <quer:intB>3</quer:intB> </quer:Add> </soap:Body> </soap:Envelope>`; check soapClient->sendOnly(envelope, "http://tempuri.org/Add"); }
Security
The SOAP client module introduces a robust framework for configuring security measures in SOAP communication. Security is a critical concern when exchanging data via web services, and this module offers comprehensive options to fortify SOAP requests and responses.
There are two primary security configurations available for SOAP clients:
-
inboundSecurity
: This configuration is applied to the SOAP envelope when a request is made. It includes various ws security policies such as Username Token, Timestamp Token, X509 Token, Symmetric Binding, Asymmetric Binding, and Transport Binding, either individually or in combination with each other. -
outboundSecurity
: This configuration is applied to the SOAP envelope when a response is received. Its purpose is to decrypt the data within the envelope and verify the digital signature for security validation.
Policies
This library currently supports the following WS Security policies:
- Username Token: Provides authentication through username and password credentials.
- Timestamp Token: Enhances message integrity by incorporating timestamp information.
- X509 Token: Allows the use of X.509 certificates for secure communication.
- Symmetric Binding: Enables symmetric key-based security mechanisms.
- Asymmetric Binding: Facilitates the use of asymmetric cryptography for enhanced security.
These policies empower SOAP clients to enhance the security of their web service communications by selecting and implementing the appropriate security mechanisms to safeguard their SOAP envelopes.
Security Policy Configuration Types
Inbound Security Configurations
-
TimestampTokenConfig
: Represents the record for Timestamp Token policy.- Fields:
int
timeToLive : The time to get expired
- Fields:
-
UsernameTokenConfig
: Represents the record for Username Token policy.- Fields:
-
string
username : The name of the user -string
password : The password of the user -PasswordType
passwordType : The password type of the username token
- Fields:
-
-
SymmetricBindingConfig
: Represents the record for Symmetric Binding policy.- Fields:
crypto:PrivateKey
symmetricKey : The key to sign and encrypt the SOAP envelopecrypto:PublicKey
servicePublicKey : The key to encrypt the symmetric keySignatureAlgorithm
signatureAlgorithm : The algorithm to sign the SOAP envelopeEncryptionAlgorithm
encryptionAlgorithm : The algorithm to encrypt the SOAP envelopestring
x509Token : The path or token of the X509 certificate
- Fields:
-
AsymmetricBindingConfig
: Represents the record for Username Token with Asymmetric Binding policy.- Fields:
crypto:PrivateKey
signatureKey : The private key to sign the SOAP envelopecrypto:PublicKey
encryptionKey : The public key to encrypt the SOAP bodySignatureAlgorithm
signatureAlgorithm : The algorithm to sign the SOAP envelopeEncryptionAlgorithm
encryptionAlgorithm : The algorithm to encrypt the SOAP bodystring
x509Token : field description
- Fields:
Outbound Security Configurations
OutboundSecurityConfig
: Represents the record for outbound security configurations to verify and decrypt SOAP envelopes.- Fields:
crypto:PublicKey
verificationKey : The public key to verify the signature of the SOAP envelopecrypto:PrivateKey
|crypto:PublicKey
decryptionKey : The private key to decrypt the SOAP envelopeSignatureAlgorithm
signatureAlgorithm : The algorithm to verify the SOAP envelopeEncryptionAlgorithm
decryptionAlgorithm : The algorithm to decrypt the SOAP body
- Fields:
Apply Security Policies
SOAP 1.1 Client: UsernameToken and TranportBinding Policy
import ballerina/crypto; import ballerina/mime; import ballerina/soap; import ballerina/soap.soap11; public function main() returns error? { soap11:Client soapClient = check new ("https://www.secured-soap-endpoint.com", { inboundSecurity: [ { username: "username", password: "password", passwordType: soap:TEXT }, TRANSPORT_BINDING ] }); xml envelope = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <quer:Add xmlns:quer="http://tempuri.org/"> <quer:intA>2</quer:intA> <quer:intB>3</quer:intB> </quer:Add> </soap:Body> </soap:Envelope>`; xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add"); }
SOAP 1.2 Client with Asymmetric Binding and Outbound Security Configuration
import ballerina/crypto; import ballerina/mime; import ballerina/soap; import ballerina/soap.soap12; public function main() returns error? { configurable crypto:PrivateKey clientPrivateKey = ?; configurable crypto:PublicKey clientPublicKey = ?; configurable crypto:PublicKey serverPublicKey = ?; soap12:Client soapClient = check new ("https://www.secured-soap-endpoint.com", { inboundSecurity: { signatureAlgorithm: soap:RSA_SHA256, encryptionAlgorithm: soap:RSA_ECB, signatureKey: clientPrivateKey, encryptionKey: serverPublicKey, }, outboundSecurity: { verificationKey: serverPublicKey, signatureAlgorithm: soap:RSA_SHA256, decryptionKey: clientPrivateKey, decryptionAlgorithm: soap:RSA_ECB } }); xml envelope = xml `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <quer:Add xmlns:quer="http://tempuri.org/"> <quer:intA>2</quer:intA> <quer:intB>3</quer:intB> </quer:Add> </soap:Body> </soap:Envelope>`; xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add"); }