forked from fbatogo/go-onvif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soap.go
268 lines (220 loc) · 6.41 KB
/
soap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package onvif
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/clbanning/mxj"
uuid "github.com/gofrs/uuid"
)
var httpClient = &http.Client{Timeout: time.Second * 5}
// SOAP contains data for SOAP request
type SOAP struct {
Body string
XMLNs []string
User string
Password string
TokenAge time.Duration
//authHeaders func(method string) []string
AuthHeaders string
URI string
Method string
}
// SendRequest sends SOAP request to xAddr
func (soap *SOAP) SendRequest(xaddr string) (mxj.Map, error) {
// Create SOAP request
request := soap.createRequest()
// Make sure URL valid and add authentication in xAddr
urlXAddr, err := url.Parse(xaddr)
if err != nil {
return nil, err
}
// Create HTTP request
buffer := bytes.NewBuffer([]byte(request))
req, err := http.NewRequest("POST", urlXAddr.String(), buffer)
req.Header.Set("Content-Type", "application/soap+xml")
req.Header.Set("Charset", "utf-8")
Debugf("[>>>%s]%s", xaddr, buffer.String())
// Send request
resp, err := httpClient.Do(req)
if err != nil {
Error(err)
return nil, err
}
defer resp.Body.Close()
// Read response body
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
Error(err)
return nil, err
}
Debugf("[<<<%s]%s", xaddr, string(responseBody))
if resp.StatusCode != 200 {
if resp.StatusCode == 401 {
fmt.Println("handle401")
err = soap.handle401(resp)
if err != nil {
Error(err)
return nil, err
} else {
buffer = bytes.NewBuffer([]byte(request))
req, err = http.NewRequest("POST", urlXAddr.String(), buffer)
req.Header.Set("Content-Type", "application/soap+xml")
req.Header.Set("Charset", "utf-8")
req.Header.Set("Authorization", string(soap.AuthHeaders))
// Send request
resp, err = httpClient.Do(req)
if err != nil {
Error(err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(resp.Status)
Error(err)
return nil, err
}
}
} else {
err = errors.New(resp.Status)
Error(err)
return nil, err
}
}
// Parse XML to map
mapXML, err := mxj.NewMapXml(responseBody)
if err != nil {
Error(err)
return nil, err
}
//fmt.Println(mapXML)
// Check if SOAP returns fault
fault, _ := mapXML.ValueForPathString("Envelope.Body.Fault.Reason.Text.#text")
if fault != "" {
return nil, errors.New(fault)
}
return mapXML, nil
}
func (soap SOAP) createRequest() string {
// Create request envelope
request := `<?xml version="1.0" encoding="UTF-8"?>`
request += `<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">`
// Set request header
if soap.User != "" {
request += "<s:Header>" + soap.createUserToken() + "</s:Header>"
}
// Set request body
//Set XML namespace
request += "<s:Body"
for _, namespace := range soap.XMLNs {
request += " " + namespace
}
request += ">"
request += soap.Body + "</s:Body>"
// Close request envelope
request += "</s:Envelope>"
// Clean request
request = regexp.MustCompile(`\>\s+\<`).ReplaceAllString(request, "><")
request = regexp.MustCompile(`\s+`).ReplaceAllString(request, " ")
return request
}
func (soap SOAP) createUserToken() string {
//nonce := uuid.NewV4().Bytes()
id := uuid.Must(uuid.NewV4())
nonce := id.Bytes()
nonce64 := base64.StdEncoding.EncodeToString(nonce)
timestamp := time.Now().Add(soap.TokenAge).UTC().Format(time.RFC3339)
token := string(nonce) + timestamp + soap.Password
sha := sha1.New()
sha.Write([]byte(token))
shaToken := sha.Sum(nil)
shaDigest64 := base64.StdEncoding.EncodeToString(shaToken)
return `<Security s:mustUnderstand="1" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>` + soap.User + `</Username>
<Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">` + shaDigest64 + `</Password>
<Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">` + nonce64 + `</Nonce>
<Created xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">` + timestamp + `</Created>
</UsernameToken>
</Security>`
}
func md5hash(s string) string {
h := md5.Sum([]byte(s))
return hex.EncodeToString(h[:])
}
func (soap *SOAP) handle401(res *http.Response) (err error) {
authval := res.Header.Get("WWW-Authenticate")
hdrval := strings.SplitN(authval, " ", 2)
fmt.Println("hdrval:", hdrval)
var realm, qop, nonce string
if len(hdrval) == 2 {
for _, field := range strings.Split(hdrval[1], ",") {
field = strings.Trim(field, ", ")
if keyval := strings.Split(field, "="); len(keyval) == 2 {
key := keyval[0]
val := strings.Trim(keyval[1], `"`)
switch key {
case "realm":
realm = val
case "nonce":
nonce = val
case "qop":
qop = val
}
}
}
fmt.Println("realm,nonce,qop", realm, nonce, qop)
if realm != "" {
var username string
var password string
if soap.User == "" {
err = fmt.Errorf("no username")
return
}
username = soap.User
password = soap.Password
soap.AuthHeaders = fmt.Sprintf(`Basic %s`, base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
if nonce != "" {
var response string
var id uuid.UUID
id, err = uuid.NewV4()
if err != nil {
return
}
cnonce := md5hash(string(id.Bytes()))
fmt.Println("cnonce:", cnonce)
nc := "00000001"
a1 := username + ":" + realm + ":" + password
a2 := soap.Method + ":" + soap.URI
hs1 := md5hash(a1)
switch qop {
case "auth-int":
hs2 := md5hash(a2 + ":" + md5hash("entityBody"))
response = md5hash(hs1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + hs2)
case "auth":
hs2 := md5hash(a2)
response = md5hash(hs1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + hs2)
default:
hs2 := md5hash(a2)
response = md5hash(hs1 + ":" + nonce + ":" + hs2)
}
soap.AuthHeaders = fmt.Sprintf(
`Digest username="%s", realm="%s", qop="%s", algorithm="MD5", uri="%s", nonce="%s", nc=%s, cnonce="%s", opaque="", response="%s"`,
username, realm, qop, soap.URI, nonce, nc, cnonce, response)
}
}
//fmt.Println("soap.AuthHeaders:", soap.AuthHeaders)
} else {
Error("no username")
}
return
}