mirror of
https://github.com/kerberos-io/onvif.git
synced 2025-10-07 08:40:56 +08:00
api: fix bugs
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
11
api/api.go
11
api/api.go
@@ -22,12 +22,15 @@ func RunApi () {
|
||||
serviceName := c.Param("service")
|
||||
methodName := c.Param("method")
|
||||
//todo: login, pass, deviceXaddr
|
||||
username := c.GetHeader("username")
|
||||
pass := c.GetHeader("password")
|
||||
xaddr := c.GetHeader("xaddr")
|
||||
acceptedData, err := c.GetRawData()
|
||||
if err != nil {
|
||||
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 {
|
||||
c.XML(http.StatusBadRequest, err.Error())
|
||||
} 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 err error
|
||||
|
||||
@@ -77,7 +80,7 @@ func callNecessaryMethod(serviceName string, methodName string, acceptedData str
|
||||
return "", err
|
||||
}
|
||||
|
||||
endpoint, err := getEndpoint(serviceName, deviceXaddr)
|
||||
endpoint, err := getEndpoint(serviceName, xaddr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -85,7 +88,7 @@ func callNecessaryMethod(serviceName string, methodName string, acceptedData str
|
||||
soap := gosoap.NewEmptySOAP()
|
||||
soap.AddStringBodyContent(*resp)
|
||||
soap.AddRootNamespaces(goonvif.Xlmns)
|
||||
soap.AddWSSecurity("admin", "Supervisor")
|
||||
soap.AddWSSecurity(username, password)
|
||||
|
||||
servResp, err := networking.SendSoap(endpoint, soap.String())
|
||||
if err != nil {
|
||||
|
@@ -3,11 +3,9 @@ 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))
|
||||
@@ -15,18 +13,5 @@ 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))
|
||||
}*/
|
||||
//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
|
||||
}
|
||||
|
Reference in New Issue
Block a user