gSOAP HTTP Digest Authentication

gSOAP HTTP Digest Authentication

ONVIF 的 gSOAP HTTP摘要认证实现步骤。

gSOAP官网教程

添加plugin/httpda.c, plugin/smdevp.c, plugin/threads.c到工程。

按照官网教程将认证代码添加到工程中。(例如添加到GetDeviceInfo接口中)

#include "httpda.c"
struct soap *soap = soap_new();
soap_register_plugin(soap, http_da); //after new_soap() and before soap_bind
...
soap_serve(soap); // see gSOAP documentation and examples on how to serve requests
...
int ns__method(struct soap *soap, ...)
{
  if (soap->userid && soap->passwd) // Basic authentication
  {
    if (!strcmp(soap->userid, userid) && !strcmp(soap->passwd, passwd))
    {
      ... // can also check soap->authrealm 
      ... // process request and produce response
      return SOAP_OK;
    }
  }
  else if (soap->authrealm && soap->userid) // Digest authentication
  {
    passwd = ... // database lookup on userid and authrealm to find passwd
    if (!strcmp(soap->authrealm, authrealm) && !strcmp(soap->userid, userid))
    { 
      if (!http_da_verify_post(soap, passwd)) // HTTP POST DA verification
      {
        ... // process request and produce response
        return SOAP_OK;
      }
    }
  }
  soap->authrealm = authrealm; // realm to send to client
  return 401; // Not authorized, challenge with digest authentication
}

这时Server给Client回的HTTP请求头部格式存在问题,Client无法识别,导致给Server回的信息中不带useridauthrealm,因此会导致认证失败。解决方法是修改httpda.c中Server给Client回HTTP消息时的请求头格式。

httpda.c中,修改http_da_post_header(struct soap *soap, const char *key, const char *val)soap->tmpbuf中的格式信息,修改如下:

    if (data->option > 0)
    { 
      // (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->authrealm) + strlen(nonce) + strlen(opaque) + 59), "Digest algorithm=%s, realm=\"%s\", qop=\"auth,auth-int\", nonce=\"%s\", opaque=\"%s\"", alg, soap->authrealm, nonce, opaque);
      (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->authrealm) + strlen(nonce) strlen(opaque) + 59), "Digest qop=\"auth\", realm=\"%s\",  nonce=\"%s\", opaque=\"%s\", stale=\"FALSE\"", soap->authrealm, nonce, opaque);
      // if (data->fposthdr(soap, key, soap->tmpbuf))
      //   return soap->error;
    }

    //(SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->authrealm) + strlen(nonce) + strlen(opaque) + 59), "Digest algorithm=MD5, realm=\"%s\", qop=\"auth,auth-int\", nonce=\"%s\", opaque=\"%s\"", soap->authrealm, nonce, opaque);

    return data->fposthdr(soap, key, soap->tmpbuf);

此时通过ONVIF 测试工具的check选项(默认HTTP Digest Authentication)进行HTTP Digest鉴权认证,能够调用GetDeviceInfo得到设备信息。此时,和海康威视的摄像头一样,前面会进行两次发送,告知Client需要带上鉴权信息。(这里的check是需要Server进行三次发送。但是按照HTTP Digest的规定是发送两次。 后续的Digest也确实是发送两次,check除外,不清楚原因)

river@river-vm:/mnt/hgfs/onvif_3/build$ ./server 1
INFO: Not find key [ipv6]
INFO: Not find key [ipv6]

 onvif_start_httpserver
[http_da_init]  ....
SIOCADDRT: 不允许的操作

 onvif_start_discovery
ip:192.168.1.100
__tds__GetUsers 
http soap_serve
__tds__GetDeviceInformation 
opaque: c7cd8887
Starting session realm=IPC_CAM nonce=5f23d1ffca530cc0538a
SOAP 1.2 fault SOAP-ENV:Sender [no subcode]
"Error 401: HTTP 401 Unauthorized"
Detail: [no detail]
__tds__GetDeviceInformation 
opaque: 4b07732e
Starting session realm=IPC_CAM nonce=5f23d1ffca54e1db7e37
SOAP 1.2 fault SOAP-ENV:Sender [no subcode]
"Error 401: HTTP 401 Unauthorized"
Detail: [no detail]
__tds__GetDeviceInformation 
INFO  userid: user5
INFO  authrealm: IPC_CAM
INFO  Digest id and passwd not null 
Debug message: updating session realm=IPC_CAM nonce=5f23d1ffca54e1db7e37
INFO  Digest
http soap_serve
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
gSOAP是一个用于开发基于SOAP(Simple Object Access Protocol)的Web服务的工具包。它最初是为C和C++开发的,但也提供了Python的绑定。 要在Python中使用gSOAP,您需要执行以下步骤: 1. 安装gSOAP:首先,您需要从gSOAP的官方网站下载并安装gSOAP工具包。根据您的操作系统,选择合适的版本进行安装。 2. 生成SOAP客户端和服务器代码:使用gSOAP工具包提供的命令行工具,根据您的Web服务定义(通常是使用WSDL描述的),生成相应的SOAP客户端和服务器代码。例如,可以使用`soapcpp2`命令生成C++代码,然后使用`wsdl2h`和`soapcpp2`命令生成C和C++代码。 3. 使用Python绑定:gSOAP提供了Python绑定,使您可以在Python中使用SOAP客户端和服务器。您可以使用`swig`工具为生成的C或C++代码生成Python绑定。然后,将生成的绑定文件编译为Python模块。 4. 开发和使用Python代码:一旦您完成了上述步骤,您就可以在Python中编写代码来使用gSOAP生成的SOAP客户端和服务器。您可以调用相应的函数来执行SOAP调用和处理返回结果。 需要注意的是,使用gSOAP进行Python开发需要一些额外的工作,并且涉及到多个步骤。因此,在决定使用gSOAP之前,建议先评估您的项目需求,以确定是否真正需要使用SOAP和gSOAP。在许多情况下,使用更简单的RESTful API或其他Web服务技术可能更加合适和方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值