api: fix bugs

This commit is contained in:
George Palanjyan
2018-04-10 14:20:39 +03:00
parent 715eb71927
commit 3eb889f611
3 changed files with 7 additions and 88 deletions

View File

@@ -1,69 +0,0 @@
package goonvif
import (
"encoding/xml"
"time"
"encoding/base64"
"crypto/sha1"
"github.com/elgs/gostrgen"
)
/*************************
WS-Security types
*************************/
const (passwordType = "https://www.oasis-open.org/committees/download.php/13392/wss-v1.1-spec-pr-UsernameTokenProfile-01.htm#PasswordDigest")
type security struct {
XMLName xml.Name `xml:"wsse:Security"`
Auth wsAuth
}
type password struct {
XMLName xml.Name `xml:"wsse:Password"`
Type string `xml:"Type,attr"`
Password string `xml:",chardata"`
}
type wsAuth struct {
XMLName xml.Name `xml:"wsse:UsernameToken"`
Username string `xml:"wsse:Username"`
Password password `xml:"wsse:Password"`
Nonce string `xml:"wsse:Nonce"`
Created string `xml:"wsse:Created"`
}
func NewSecurity(username, passwd string) security {
/** Generating Nonce sequence **/
charsToGenerate := 16
charSet := gostrgen.Lower | gostrgen.Digit
nonce, _ := gostrgen.RandGen(charsToGenerate, charSet, "", "")
auth := security{
Auth:wsAuth{
Username:username,
Password:password {
Type:passwordType,
Password:generateToken(username, nonce, time.Now(), passwd),
},
Nonce: nonce,
Created: time.Now().Format(time.RFC3339),
},
}
return auth
}
//Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) )
func generateToken(Username string, Nonce string, Created time.Time, Password string) string {
sDec, _ := base64.StdEncoding.DecodeString(Nonce)
hasher := sha1.New()
//hasher.Write([]byte((base64.StdEncoding.EncodeToString([]byte(Nonce)) + Created.Format(time.RFC3339) + Password)))
hasher.Write([]byte(string(sDec) + Created.Format(time.RFC3339) + Password))
return base64.StdEncoding.EncodeToString(hasher.Sum(nil))
}

View File

@@ -22,12 +22,15 @@ func RunApi () {
serviceName := c.Param("service") serviceName := c.Param("service")
methodName := c.Param("method") methodName := c.Param("method")
//todo: login, pass, deviceXaddr //todo: login, pass, deviceXaddr
username := c.GetHeader("username")
pass := c.GetHeader("password")
xaddr := c.GetHeader("xaddr")
acceptedData, err := c.GetRawData() acceptedData, err := c.GetRawData()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
message, err := callNecessaryMethod(serviceName, methodName, string(acceptedData), "192.168.13.12") message, err := callNecessaryMethod(serviceName, methodName, string(acceptedData), username, pass, xaddr)
if err != nil { if err != nil {
c.XML(http.StatusBadRequest, err.Error()) c.XML(http.StatusBadRequest, err.Error())
} else { } else {
@@ -57,7 +60,7 @@ func RunApi () {
//} //}
func callNecessaryMethod(serviceName string, methodName string, acceptedData string, deviceXaddr string) (string, error) { func callNecessaryMethod(serviceName, methodName, acceptedData, username, password, xaddr string) (string, error) {
var methodStruct interface{} var methodStruct interface{}
var err error var err error
@@ -77,7 +80,7 @@ func callNecessaryMethod(serviceName string, methodName string, acceptedData str
return "", err return "", err
} }
endpoint, err := getEndpoint(serviceName, deviceXaddr) endpoint, err := getEndpoint(serviceName, xaddr)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -85,7 +88,7 @@ func callNecessaryMethod(serviceName string, methodName string, acceptedData str
soap := gosoap.NewEmptySOAP() soap := gosoap.NewEmptySOAP()
soap.AddStringBodyContent(*resp) soap.AddStringBodyContent(*resp)
soap.AddRootNamespaces(goonvif.Xlmns) soap.AddRootNamespaces(goonvif.Xlmns)
soap.AddWSSecurity("admin", "Supervisor") soap.AddWSSecurity(username, password)
servResp, err := networking.SendSoap(endpoint, soap.String()) servResp, err := networking.SendSoap(endpoint, soap.String())
if err != nil { if err != nil {

View File

@@ -3,11 +3,9 @@ package networking
import ( import (
"net/http" "net/http"
"bytes" "bytes"
"fmt"
) )
func SendSoap(endpoint, message string) (*http.Response, error) { func SendSoap(endpoint, message string) (*http.Response, error) {
fmt.Println(message)
httpClient := new(http.Client) httpClient := new(http.Client)
resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message)) resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message))
@@ -15,18 +13,5 @@ func SendSoap(endpoint, message string) (*http.Response, error) {
return resp, err 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))
}*/
//b, err := ioutil.ReadAll(resp.Body)
//if err != nil {
// return resp, err
//}
//fmt.Println(endpoint)
//fmt.Println(string(b))
//log.Println(resp.StatusCode)
return resp,nil return resp,nil
} }