soap

package module
v0.0.0-...-44eefde Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2018 License: MIT Imports: 7 Imported by: 0

README

GoLang SOAP with WsseSecurity

HowTo

Sample go code:
package main

import (
	"encoding/xml"
	"fmt"
	"github.com/radoslav/soap"
)

func main() {
	env := &soap.Envelope{
		XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
		XmlnsUniv:    "http://www.example.pl/ws/test/universal",
		Header: &soap.Header{
			WsseSecurity: &soap.WsseSecurity{
				MustUnderstand: "1",
				XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
				XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
				UsernameToken: &soap.UsernameToken{
					WsuId:    "UsernameToken-1",
					Username: &soap.Username{},
					Password: &soap.Password{
						Type: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",
					},
				},
			},
		},
	}

	env.Header.WsseSecurity.UsernameToken.Username.Value = "username"
	env.Header.WsseSecurity.UsernameToken.Password.Value = "pass"
	env.Body = &soap.Body{} // interface

	output, err := xml.MarshalIndent(env, "", "   ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	fmt.Println(string(output))
}

Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1">
            <wsse:Username>username</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body></soapenv:Body>
</soapenv:Envelope>

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckFault

func CheckFault(soapResponse []byte) error

func Request

func Request(url string, soapRequest []byte, soapAction string) ([]byte, error)

func SoapFomMTOM

func SoapFomMTOM(soap []byte) ([]byte, error)
Example
s := []byte("dasdasd---dasdad<><soap:envelope>adsdasfasdfsadfsaf<adsfas><asdfasdf></soap:envelope>--sadadad<>asdasd")
out, _ := SoapFomMTOM(s)

os.Stdout.Write(out)
Output:

<soap:envelope>adsdasfasdfsadfsaf<adsfas><asdfasdf></soap:envelope>

Types

type Body

type Body struct {
	XMLName xml.Name `xml:"SOAP-ENV:Body"`
	Payload interface{}
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
}

env.Body = &Body{}

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Body></soapenv:Body>
</soapenv:Envelope>

type Created

type Created struct {
	XMLName xml.Name `xml:"wsu:Created,omitempty"`
	Value   string   `xml:",chardata"`
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
			UsernameToken: &UsernameToken{
				WsuId:    "UsernameToken-1",
				Username: &Username{},
				Password: &Password{
					Type: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",
				},
				Nonce: &Nonce{
					EncodingType: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary",
				},
				Created: &Created{},
			},
		},
	},
}

env.Header.WsseSecurity.UsernameToken.Username.Value = "test"
env.Header.WsseSecurity.UsernameToken.Password.Value = "pass"
env.Header.WsseSecurity.UsernameToken.Nonce.Value = "nvKKZ20LNP8wpCa4vAeQhQ=="
env.Header.WsseSecurity.UsernameToken.Created.Value = "2015-09-10T12:25:55.121Z"

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1">
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">nvKKZ20LNP8wpCa4vAeQhQ==</wsse:Nonce>
            <wsu:Created>2015-09-10T12:25:55.121Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
</soapenv:Envelope>

type Envelope

type Envelope struct {
	XMLName      xml.Name `xml:"SOAP-ENV:Envelope"`
	XmlnsSoapenv string   `xml:"xmlns:SOAP-ENV,attr"`
	XmlnsUniv    string   `xml:"xmlns:univ,attr"`

	Header *Header
	Body   *Body
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
}

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal"></soapenv:Envelope>

type Fault

type Fault struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"`
	Code    string   `xml:"faultcode,omitempty"`
	String  string   `xml:"faultstring,omitempty"`
	Actor   string   `xml:"faultactor,omitempty"`
	Detail  string   `xml:"detail,omitempty"`
}
type Header struct {
	XMLName      xml.Name `xml:"SOAP-ENV:Header"`
	WsseSecurity *WsseSecurity
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header:       &Header{},
}

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header></soapenv:Header>
</soapenv:Envelope>

type Nonce

type Nonce struct {
	XMLName      xml.Name `xml:"wsse:Nonce,omitempty"`
	EncodingType string   `xml:"EncodingType,attr,omitempty"`
	Value        string   `xml:",chardata"`
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
			UsernameToken: &UsernameToken{
				WsuId:    "UsernameToken-1",
				Username: &Username{},
				Password: &Password{
					Type: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",
				},
				Nonce: &Nonce{
					EncodingType: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary",
				},
			},
		},
	},
}

env.Header.WsseSecurity.UsernameToken.Username.Value = "test"
env.Header.WsseSecurity.UsernameToken.Password.Value = "pass"
env.Header.WsseSecurity.UsernameToken.Nonce.Value = "nvKKZ20LNP8wpCa4vAeQhQ=="

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1">
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">nvKKZ20LNP8wpCa4vAeQhQ==</wsse:Nonce>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
</soapenv:Envelope>

type Password

type Password struct {
	XMLName xml.Name `xml:"wsse:Password"`
	Type    string   `xml:"Type,attr"`
	Value   string   `xml:",chardata"`
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
			UsernameToken: &UsernameToken{
				WsuId: "UsernameToken-1",
				Password: &Password{
					Type: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",
				},
			},
		},
	},
}

env.Header.WsseSecurity.UsernameToken.Password.Value = "pass"

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1">
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
</soapenv:Envelope>

type ResponseBody

type ResponseBody struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	Fault   Fault    `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"`
}

type ResponseEnvelope

type ResponseEnvelope struct {
	XMLName          xml.Name     `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
	ResponseBodyBody ResponseBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}

type Username

type Username struct {
	XMLName xml.Name `xml:"wsse:Username"`
	Value   string   `xml:",chardata"`
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
			UsernameToken: &UsernameToken{
				WsuId:    "UsernameToken-1",
				Username: &Username{},
			},
		},
	},
}

env.Header.WsseSecurity.UsernameToken.Username.Value = "test"

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1">
            <wsse:Username>test</wsse:Username>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
</soapenv:Envelope>

type UsernameToken

type UsernameToken struct {
	XMLName  xml.Name `xml:"wsse:UsernameToken"`
	WsuId    string   `xml:"wsu:Id,attr,omitempty"`
	Username *Username
	Password *Password
	Nonce    *Nonce
	Created  *Created
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
			UsernameToken: &UsernameToken{
				WsuId: "UsernameToken-1",
			},
		},
	},
}

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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 wsu:Id="UsernameToken-1"></wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
</soapenv:Envelope>

type WsseSecurity

type WsseSecurity struct {
	MustUnderstand string   `xml:"SOAP-ENV:mustUnderstand,attr"`
	XMLName        xml.Name `xml:"wsse:Security"`
	XmlnsWsse      string   `xml:"xmlns:wsse,attr"`
	XmlnsWsu       string   `xml:"xmlns:wsu,attr"`

	UsernameToken *UsernameToken
}
Example
env := &Envelope{
	XmlnsSoapenv: "http://schemas.xmlsoap.org/soap/envelope/",
	XmlnsUniv:    namespaceUniv,
	Header: &Header{
		WsseSecurity: &WsseSecurity{
			MustUnderstand: "1",
			XmlnsWsse:      "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
			XmlnsWsu:       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
		},
	},
}

output, err := xml.MarshalIndent(env, prefix, indent)
if err != nil {
	fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:univ="http://www.example.pl/ws/test/universal">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" 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:Security>
   </soapenv:Header>
</soapenv:Envelope>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL