From b302c5c92bc3296e08d4bd0f8166166b8572d00e Mon Sep 17 00:00:00 2001 From: yakovlevdmv Date: Tue, 10 Apr 2018 02:59:48 +0300 Subject: [PATCH] WS-security update(moved to gosoap progect), return error on HTTP digest authentification required --- Device.go | 47 ++++++++++------------------------------ networking/networking.go | 4 ++++ 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/Device.go b/Device.go index 0681cfe..339b0c1 100644 --- a/Device.go +++ b/Device.go @@ -235,22 +235,19 @@ func (dev device) CallMethod(method interface{}) (*http.Response, error) { //TODO: Get endpoint automatically if dev.login != "" && dev.password != "" { - /*resp, err := dev.сallAuthorizedMethod(endpoint, method) - if err != nil { - panic(err) - return resp, err + resp, err := dev.callAuthorizedMethod(endpoint, method) + if resp.StatusCode == 401 { + return resp, errors.New("device requires HTTP digest authentication, which is not implemented there") } - - return resp, err*/ - return dev.callAuthorizedMethod(endpoint, method) + return resp, err + //return dev.callAuthorizedMethod(endpoint, method) } else { - /*resp, err := dev.сallAuthorizedMethod(endpoint, method) - if err != nil { - panic(err) - return resp, err + resp, err := dev.callNonAuthorizedMethod(endpoint, method) + if resp.StatusCode == 401 { + return resp, errors.New("device requires HTTP digest authentication, which is not implemented there") } - return resp, err*/ - return dev.callNonAuthorizedMethod(endpoint, method) + return resp, err + //return dev.callNonAuthorizedMethod(endpoint, method) } } @@ -304,29 +301,7 @@ func (dev device) callAuthorizedMethod(endpoint string, method interface{}) (*ht return nil, err } - /* - Getting an WS-Security struct representation - */ - auth := newSecurity(dev.login, dev.password) - - /* - Adding WS-Security namespaces to root element of SOAP message - */ - soap.AddRootNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext1.0.xsd") - soap.AddRootNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility1.0.xsd") - - soap.AddRootNamespaces(xlmns) - - soapReq, err := xml.MarshalIndent(auth, "", " ") - if err != nil { - //log.Printf("error: %v\n", err.Error()) - return nil, err - } - - /* - Adding WS-Security struct to SOAP header - */ - soap.AddStringHeaderContent(string(soapReq)) + soap.AddWSSecurity(dev.login, dev.password) /* Sending request and returns the response diff --git a/networking/networking.go b/networking/networking.go index ae3bbad..bec2821 100644 --- a/networking/networking.go +++ b/networking/networking.go @@ -3,9 +3,11 @@ package networking import ( "net/http" "bytes" + "fmt" ) func SendSoap(endpoint, message string) (*http.Response, error) { + fmt.Println(message) httpClient := new(http.Client) resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message)) @@ -13,6 +15,8 @@ func SendSoap(endpoint, message string) (*http.Response, error) { return resp, err } + fmt.Println(resp.Header) + /*if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest { return "", errors.New("error: got HTTP response status " + strconv.Itoa(resp.StatusCode)) }*/