luat_lib_iotauth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. @module iotauth
  3. @summary IoT鉴权库, 用于生成各种云平台的参数
  4. @version core V0007
  5. @date 2022.08.06
  6. @demo iotauth
  7. */
  8. #include "luat_base.h"
  9. #include "luat_crypto.h"
  10. #include "luat_malloc.h"
  11. #include "time.h"
  12. #include "luat_str.h"
  13. #include "luat_mcu.h"
  14. #define LUAT_LOG_TAG "iotauth"
  15. #include "luat_log.h"
  16. #define CLIENT_ID_LEN 64
  17. #define USER_NAME_LEN 128
  18. #define PASSWORD_LEN 256
  19. static char client_id[CLIENT_ID_LEN]={0};
  20. static char user_name[USER_NAME_LEN]={0};
  21. static char password[PASSWORD_LEN]={0};
  22. static const unsigned char hexchars[] = "0123456789abcdef";
  23. static void str_tohex(const char* str, size_t str_len, char* hex) {
  24. for (size_t i = 0; i < str_len; i++)
  25. {
  26. char ch = *(str+i);
  27. hex[i*2] = hexchars[(unsigned char)ch >> 4];
  28. hex[i*2+1] = hexchars[(unsigned char)ch & 0xF];
  29. }
  30. }
  31. static void aliyun_token(const char* product_key,const char* device_name,const char* device_secret,long long cur_timestamp,const char* method,char* client_id, char* user_name, char* password){
  32. char deviceId[64] = {0};
  33. char macSrc[200] = {0};
  34. char macRes[32] = {0};
  35. char timestamp_value[20] = {0};
  36. char mqtt_clinetid_kv[96] = {0};
  37. sprintf_(timestamp_value,"%lld",cur_timestamp);
  38. if (!strcmp("hmacmd5", method)||!strcmp("HMACMD5", method)) {
  39. sprintf_(mqtt_clinetid_kv,"|timestamp=%s,_v=paho-c-1.0.0,securemode=3,signmethod=%s,lan=C|",timestamp_value,"hmacmd5");
  40. }else if (!strcmp("hmacsha1", method)||!strcmp("HMACSHA1", method)) {
  41. sprintf_(mqtt_clinetid_kv,"|timestamp=%s,_v=paho-c-1.0.0,securemode=3,signmethod=%s,lan=C|",timestamp_value,"hmacsha1");
  42. }else if (!strcmp("hmacsha256", method)||!strcmp("HMACSHA256", method)) {
  43. sprintf_(mqtt_clinetid_kv,"|timestamp=%s,_v=paho-c-1.0.0,securemode=3,signmethod=%s,lan=C|",timestamp_value,"hmacsha256");
  44. }else{
  45. LLOGE("not support: %s",method);
  46. return;
  47. }
  48. /* setup deviceId */
  49. memcpy(deviceId, device_name, strlen(device_name));
  50. memcpy(deviceId + strlen(deviceId), "&", strlen("&"));
  51. memcpy(deviceId + strlen(deviceId), product_key, strlen(product_key));
  52. /* setup clientid */
  53. memcpy(client_id, deviceId, strlen(deviceId));
  54. memcpy(client_id + strlen(deviceId), mqtt_clinetid_kv, strlen(mqtt_clinetid_kv));
  55. memset(client_id + strlen(deviceId) + strlen(mqtt_clinetid_kv), 0, 1);
  56. /* setup username */
  57. memcpy(user_name, deviceId, strlen(deviceId));
  58. memset(user_name + strlen(deviceId), 0, 1);
  59. /* setup password */
  60. memcpy(macSrc, "clientId", strlen("clientId"));
  61. memcpy(macSrc + strlen(macSrc), deviceId, strlen(deviceId));
  62. memcpy(macSrc + strlen(macSrc), "deviceName", strlen("deviceName"));
  63. memcpy(macSrc + strlen(macSrc), device_name, strlen(device_name));
  64. memcpy(macSrc + strlen(macSrc), "productKey", strlen("productKey"));
  65. memcpy(macSrc + strlen(macSrc), product_key, strlen(product_key));
  66. memcpy(macSrc + strlen(macSrc), "timestamp", strlen("timestamp"));
  67. memcpy(macSrc + strlen(macSrc), timestamp_value, strlen(timestamp_value));
  68. if (!strcmp("hmacmd5", method)||!strcmp("HMACMD5", method)) {
  69. luat_crypto_hmac_md5_simple(macSrc, strlen(macSrc),device_secret, strlen(device_secret), macRes);
  70. }else if (!strcmp("hmacsha1", method)||!strcmp("HMACSHA1", method)) {
  71. luat_crypto_hmac_sha1_simple(macSrc, strlen(macSrc),device_secret, strlen(device_secret), macRes);
  72. }else if (!strcmp("hmacsha256", method)||!strcmp("HMACSHA256", method)) {
  73. luat_crypto_hmac_sha256_simple(macSrc, strlen(macSrc),device_secret, strlen(device_secret), macRes);
  74. }else{
  75. LLOGE("not support: %s",method);
  76. return;
  77. }
  78. luat_str_tohex(macRes, sizeof(macRes), password);
  79. }
  80. /*
  81. 阿里云物联网平台三元组生成
  82. @api iotauth.aliyun(product_key, device_name,device_secret,method,cur_timestamp)
  83. @string product_key
  84. @string device_name
  85. @string device_secret
  86. @string method 加密方式,"hmacmd5" "hmacsha1" "hmacsha256" 可选,默认"hmacsha256"
  87. @number cur_timestamp 可选
  88. @return string mqtt三元组 client_id
  89. @return string mqtt三元组 user_name
  90. @return string mqtt三元组 password
  91. @usage
  92. local client_id,user_name,password = iotauth.aliyun("123456789","abcdefg","Y877Bgo8X5owd3lcB5wWDjryNPoB")
  93. print(client_id,user_name,password)
  94. */
  95. static int l_iotauth_aliyun(lua_State *L) {
  96. memset(client_id, 0, CLIENT_ID_LEN);
  97. memset(user_name, 0, USER_NAME_LEN);
  98. memset(password, 0, PASSWORD_LEN);
  99. size_t len;
  100. const char* product_key = luaL_checklstring(L, 1, &len);
  101. const char* device_name = luaL_checklstring(L, 2, &len);
  102. const char* device_secret = luaL_checklstring(L, 3, &len);
  103. const char* method = luaL_optlstring(L, 4, "hmacsha256", &len);
  104. long long cur_timestamp = luaL_optinteger(L, 5,time(NULL) + 3600);
  105. aliyun_token(product_key,device_name,device_secret,cur_timestamp,method,client_id,user_name,password);
  106. lua_pushlstring(L, client_id, strlen(client_id));
  107. lua_pushlstring(L, user_name, strlen(user_name));
  108. lua_pushlstring(L, password, strlen(password));
  109. return 3;
  110. }
  111. typedef struct {
  112. char et[32];
  113. char version[12];
  114. char method[12];
  115. char res[64];
  116. char sign[64];
  117. } sign_msg;
  118. typedef struct {
  119. char* old_str;
  120. char* str;
  121. }URL_PARAMETES;
  122. static int url_encoding_for_token(sign_msg* msg,char *token){
  123. int i,j,k,slen;
  124. sign_msg* temp_msg = msg;
  125. URL_PARAMETES url_patametes[] = {
  126. {"+","%2B"},
  127. {" ","%20"},
  128. {"/","%2F"},
  129. {"?","%3F"},
  130. {"%","%25"},
  131. {"#","%23"},
  132. {"&","%26"},
  133. {"=","%3D"},
  134. };
  135. char temp[64] = {0};
  136. slen = strlen(temp_msg->res);
  137. for (i = 0,j = 0; i < slen; i++) {
  138. for(k = 0; k < 8; k++){
  139. if(temp_msg->res[i] == url_patametes[k].old_str[0]) {
  140. memcpy(&temp[j],url_patametes[k].str,strlen(url_patametes[k].str));
  141. j+=3;
  142. break;
  143. }
  144. }
  145. if (k == 8) {
  146. temp[j++] = temp_msg->res[i];
  147. }
  148. }
  149. memcpy(temp_msg->res,temp,strlen(temp));
  150. temp_msg->res[strlen(temp)] = 0;
  151. memset(temp,0x00,sizeof(temp));
  152. slen = strlen(temp_msg->sign);
  153. for (i = 0,j = 0; i < slen; i++) {
  154. for(k = 0; k < 8; k++){
  155. if(temp_msg->sign[i] == url_patametes[k].old_str[0]) {
  156. memcpy(&temp[j],url_patametes[k].str,strlen(url_patametes[k].str));
  157. j+=3;
  158. break;
  159. }
  160. }
  161. if(k == 8){
  162. temp[j++] = temp_msg->sign[i];
  163. }
  164. }
  165. memcpy(temp_msg->sign,temp,strlen(temp));
  166. temp_msg->sign[strlen(temp)] = 0;
  167. if(snprintf_(token,PASSWORD_LEN, "version=%s&res=%s&et=%s&method=%s&sign=%s", temp_msg->version, temp_msg->res, temp_msg->et, temp_msg->method, temp_msg->sign)<0){
  168. return -1;
  169. }
  170. return strlen(token);
  171. }
  172. static void onenet_token(const char* product_id,const char* device_name,const char* device_secret,long long cur_timestamp,char * method,char * version,char *token){
  173. size_t declen = 0, enclen = 0;
  174. char plaintext[64] = { 0 };
  175. char hmac[64] = { 0 };
  176. char StringForSignature[256] = { 0 };
  177. sign_msg sign = {0};
  178. memcpy(sign.method, method, strlen(method));
  179. memcpy(sign.version, version, strlen(version));
  180. sprintf_(sign.et,"%lld",cur_timestamp);
  181. sprintf_(sign.res,"products/%s/devices/%s",product_id,device_name);
  182. luat_str_base64_decode((unsigned char *)plaintext, sizeof(plaintext), &declen, (const unsigned char * )device_secret, strlen((char*)device_secret));
  183. sprintf_(StringForSignature, "%s\n%s\n%s\n%s", sign.et, sign.method, sign.res, sign.version);
  184. if (!strcmp("md5", method)||!strcmp("MD5", method)) {
  185. luat_crypto_hmac_md5_simple(StringForSignature, strlen(StringForSignature), plaintext, declen, hmac);
  186. }else if (!strcmp("sha1", method)||!strcmp("SHA1", method)) {
  187. luat_crypto_hmac_sha1_simple(StringForSignature, strlen(StringForSignature),plaintext, declen, hmac);
  188. }else if (!strcmp("sha256", method)||!strcmp("SHA256", method)) {
  189. luat_crypto_hmac_sha256_simple(StringForSignature, strlen(StringForSignature),plaintext, declen, hmac);
  190. }else{
  191. LLOGE("not support: %s",method);
  192. return;
  193. }
  194. luat_str_base64_encode((unsigned char *)sign.sign, sizeof(sign.sign), &enclen, (const unsigned char * )hmac, strlen(hmac));
  195. url_encoding_for_token(&sign,token);
  196. }
  197. /*
  198. 中国移动物联网平台三元组生成
  199. @api iotauth.onenet(produt_id, device_name,key,method,cur_timestamp,version)
  200. @string produt_id
  201. @string device_name
  202. @string key
  203. @string method 加密方式,"md5" "sha1" "sha256" 可选,默认"md5"
  204. @number cur_timestamp 可选
  205. @string version 可选 默认"2018-10-31"
  206. @return string mqtt三元组 client_id
  207. @return string mqtt三元组 user_name
  208. @return string mqtt三元组 password
  209. @usage
  210. local client_id,user_name,password = iotauth.onenet("123456789","test","KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw=","md5",1658920369,"2018-10-31")
  211. print(client_id,user_name,password)
  212. */
  213. static int l_iotauth_onenet(lua_State *L) {
  214. memset(password, 0, PASSWORD_LEN);
  215. size_t len;
  216. const char* produt_id = luaL_checklstring(L, 1, &len);
  217. const char* device_name = luaL_checklstring(L, 2, &len);
  218. const char* key = luaL_checklstring(L, 3, &len);
  219. const char* method = luaL_optlstring(L, 4, "md5", &len);
  220. long long cur_timestamp = luaL_optinteger(L, 5,time(NULL) + 3600);
  221. const char* version = luaL_optlstring(L, 6, "2018-10-31", &len);
  222. onenet_token(produt_id,device_name,key,cur_timestamp,method,version,password);
  223. lua_pushlstring(L, device_name, strlen(device_name));
  224. lua_pushlstring(L, produt_id, strlen(produt_id));
  225. lua_pushlstring(L, password, strlen(password));
  226. return 3;
  227. }
  228. static void iotda_token(const char* device_id,const char* device_secret,long long cur_timestamp,int ins_timestamp,char* client_id,const char* password){
  229. char hmac[64] = {0};
  230. char timestamp[12] = {0};
  231. struct tm *timeinfo = localtime( &cur_timestamp );
  232. if(snprintf_(timestamp, 12, "%04d%02d%02d%02d", (timeinfo->tm_year)+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour)<0){
  233. return;
  234. }
  235. snprintf_(client_id, CLIENT_ID_LEN, "%s_0_%d_%s", device_id,ins_timestamp,timestamp);
  236. luat_crypto_hmac_sha256_simple(device_secret, strlen(device_secret),timestamp, strlen(timestamp), hmac);
  237. str_tohex(hmac, strlen(hmac), password);
  238. }
  239. /*
  240. 华为物联网平台三元组生成
  241. @api iotauth.iotda(device_id,device_secret,ins_timestamp,cur_timestamp)
  242. @string device_id
  243. @string device_secret
  244. @number ins_timestamp 是否校验时间戳 1:校验 0:不校验
  245. @number cur_timestamp 可选
  246. @return string mqtt三元组 client_id
  247. @return string mqtt三元组 user_name
  248. @return string mqtt三元组 password
  249. @usage
  250. local client_id,user_name,password = iotauth.iotda("6203cc94c7fb24029b110408_88888888","123456789",1,1659495778)
  251. print(client_id,user_name,password)
  252. */
  253. static int l_iotauth_iotda(lua_State *L) {
  254. memset(client_id, 0, CLIENT_ID_LEN);
  255. memset(password, 0, PASSWORD_LEN);
  256. size_t len;
  257. const char* device_id = luaL_checklstring(L, 1, &len);
  258. const char* device_secret = luaL_checklstring(L, 2, &len);
  259. int ins_timestamp = luaL_optinteger(L, 3, 0);
  260. long long cur_timestamp = luaL_optinteger(L, 4,time(NULL));
  261. ins_timestamp = ins_timestamp==0?0:1;
  262. iotda_token(device_id,device_secret,cur_timestamp,ins_timestamp,client_id,password);
  263. lua_pushlstring(L, client_id, strlen(client_id));
  264. lua_pushlstring(L, device_id, strlen(device_id));
  265. lua_pushlstring(L, password, strlen(password));
  266. return 3;
  267. }
  268. /* Max size of base64 encoded PSK = 64, after decode: 64/4*3 = 48*/
  269. #define DECODE_PSK_LENGTH 48
  270. /* Max size of conn Id */
  271. #define MAX_CONN_ID_LEN (6)
  272. static void get_next_conn_id(char *conn_id){
  273. int i;
  274. srand((unsigned)luat_mcu_ticks());
  275. for (i = 0; i < MAX_CONN_ID_LEN - 1; i++) {
  276. int flag = rand() % 3;
  277. switch (flag) {
  278. case 0:
  279. conn_id[i] = (rand() % 26) + 'a';
  280. break;
  281. case 1:
  282. conn_id[i] = (rand() % 26) + 'A';
  283. break;
  284. case 2:
  285. conn_id[i] = (rand() % 10) + '0';
  286. break;
  287. }
  288. }
  289. conn_id[MAX_CONN_ID_LEN - 1] = '\0';
  290. }
  291. static void qcloud_token(const char* product_id,const char* device_name,const char* device_secret,long long cur_timestamp,const char* method,const char* sdk_appid,char* username,char* password){
  292. char conn_id[MAX_CONN_ID_LEN] = {};
  293. char username_sign[41] = {0};
  294. char psk_base64decode[DECODE_PSK_LENGTH];
  295. size_t psk_base64decode_len = 0;
  296. luat_str_base64_decode((unsigned char *)psk_base64decode, DECODE_PSK_LENGTH, &psk_base64decode_len,(unsigned char *)device_secret, strlen(device_secret));
  297. get_next_conn_id(conn_id);
  298. sprintf_(username, "%s%s;%s;%s;%lld", product_id, device_name, sdk_appid,conn_id, cur_timestamp);
  299. if (!strcmp("sha1", method)||!strcmp("SHA1", method)) {
  300. luat_crypto_hmac_sha1_simple(username, strlen(username),psk_base64decode, psk_base64decode_len, username_sign);
  301. }else if (!strcmp("sha256", method)||!strcmp("SHA256", method)) {
  302. luat_crypto_hmac_sha256_simple(username, strlen(username),psk_base64decode, psk_base64decode_len, username_sign);
  303. }else{
  304. LLOGE("not support: %s",method);
  305. return;
  306. }
  307. char *username_sign_hex = (char *)luat_heap_malloc(strlen(username_sign)*2+1);
  308. memset(username_sign_hex, 0, strlen(username_sign)*2+1);
  309. str_tohex(username_sign, strlen(username_sign), username_sign_hex);
  310. if (!strcmp("sha1", method)||!strcmp("SHA1", method)) {
  311. sprintf_(password, "%s;hmacsha1", username_sign_hex);
  312. }else if (!strcmp("sha256", method)||!strcmp("SHA256", method)) {
  313. sprintf_(password, "%s;hmacsha256", username_sign_hex);
  314. }
  315. luat_heap_free(username_sign_hex);
  316. }
  317. /*
  318. 腾讯联网平台三元组生成
  319. @api iotauth.qcloud(product_id, device_name,device_secret,method,cur_timestamp,sdk_appid)
  320. @string product_id
  321. @string device_name
  322. @string device_secret
  323. @string method 加密方式,"sha1" "sha256" 可选,默认"sha256"
  324. @number cur_timestamp 可选
  325. @string sdk_appid 可选 默认为"12010126"
  326. @return string mqtt三元组 client_id
  327. @return string mqtt三元组 user_name
  328. @return string mqtt三元组 password
  329. @usage
  330. local client_id,user_name,password = iotauth.qcloud("LD8S5J1L07","test","acyv3QDJrRa0fW5UE58KnQ==", "sha1",1660103393)
  331. print(client_id,user_name,password)
  332. */
  333. static int l_iotauth_qcloud(lua_State *L) {
  334. memset(client_id, 0, CLIENT_ID_LEN);
  335. memset(user_name, 0, USER_NAME_LEN);
  336. memset(password, 0, PASSWORD_LEN);
  337. size_t len;
  338. const char* product_id = luaL_checklstring(L, 1, &len);
  339. const char* device_name = luaL_checklstring(L, 2, &len);
  340. const char* device_secret = luaL_checklstring(L, 3, &len);
  341. const char* method = luaL_optlstring(L, 4, "sha256", &len);
  342. long long cur_timestamp = luaL_optinteger(L, 5,time(NULL) + 3600);
  343. const char* sdk_appid = luaL_optlstring(L, 6, "12010126", &len);
  344. qcloud_token(product_id, device_name,device_secret,cur_timestamp,method,sdk_appid,user_name,password);
  345. snprintf_(client_id, CLIENT_ID_LEN,"%s%s", product_id,device_name);
  346. lua_pushlstring(L, client_id, strlen(client_id));
  347. lua_pushlstring(L, user_name, strlen(user_name));
  348. lua_pushlstring(L, password, strlen(password));
  349. return 3;
  350. }
  351. static void tuya_token(const char* device_id,const char* device_secret,long long cur_timestamp,const char* password){
  352. char hmac[64] = {0};
  353. char *token_temp = (char *)luat_heap_malloc(100);
  354. memset(token_temp, 0, 100);
  355. snprintf_(token_temp, 100, "deviceId=%s,timestamp=%lld,secureMode=1,accessType=1", device_id, cur_timestamp);
  356. luat_crypto_hmac_sha256_simple(token_temp, strlen(token_temp),device_secret, strlen(device_secret), hmac);
  357. for (int i = 0; i < 32; i++) {
  358. sprintf_(password + 2*i, "%02x", hmac[i]);
  359. }
  360. luat_heap_free(token_temp);
  361. }
  362. /*
  363. 涂鸦联网平台三元组生成
  364. @api iotauth.tuya(device_id,device_secret,cur_timestamp)
  365. @string device_id
  366. @string device_secret
  367. @number cur_timestamp 可选
  368. @return string mqtt三元组 client_id
  369. @return string mqtt三元组 user_name
  370. @return string mqtt三元组 password
  371. @usage
  372. local client_id,user_name,password = iotauth.tuya("6c95875d0f5ba69607nzfl","fb803786602df760",1607635284)
  373. print(client_id,user_name,password)
  374. */
  375. static int l_iotauth_tuya(lua_State *L) {
  376. memset(client_id, 0, CLIENT_ID_LEN);
  377. memset(user_name, 0, USER_NAME_LEN);
  378. memset(password, 0, PASSWORD_LEN);
  379. size_t len;
  380. const char* device_id = luaL_checklstring(L, 1, &len);
  381. const char* device_secret = luaL_checklstring(L, 2, &len);
  382. long long cur_timestamp = luaL_optinteger(L, 3,time(NULL) + 3600);
  383. tuya_token(device_id,device_secret,cur_timestamp,password);
  384. snprintf_(client_id, CLIENT_ID_LEN, "tuyalink_%s", device_id);
  385. snprintf_(user_name, USER_NAME_LEN, "%s|signMethod=hmacSha256,timestamp=%lld,secureMode=1,accessType=1", device_id,cur_timestamp);
  386. lua_pushlstring(L, client_id, strlen(client_id));
  387. lua_pushlstring(L, user_name, strlen(user_name));
  388. lua_pushlstring(L, password, strlen(password));
  389. return 3;
  390. }
  391. static void baidu_token(const char* iot_core_id,const char* device_key,const char* device_secret,const char* method,long long cur_timestamp,char* username,char* password){
  392. char crypto[64] = {0};
  393. char *token_temp = (char *)luat_heap_malloc(100);
  394. memset(token_temp, 0, 100);
  395. if (!strcmp("MD5", method)||!strcmp("md5", method)) {
  396. sprintf_(username, "thingidp@%s|%s|%lld|%s",iot_core_id,device_key,cur_timestamp,"MD5");
  397. snprintf_(token_temp, 100, "%s&%lld&%s%s",device_key,cur_timestamp,"MD5",device_secret);
  398. luat_crypto_md5_simple(token_temp, strlen(token_temp),crypto);
  399. }else if (!strcmp("SHA256", method)||!strcmp("sha256", method)) {
  400. sprintf_(username, "thingidp@%s|%s|%lld|%s",iot_core_id,device_key,cur_timestamp,"SHA256");
  401. snprintf_(token_temp, 100, "%s&%lld&%s%s",device_key,cur_timestamp,"SHA256",device_secret);
  402. luat_crypto_sha256_simple(token_temp, strlen(token_temp),crypto);
  403. }else{
  404. LLOGE("not support: %s",method);
  405. return;
  406. }
  407. str_tohex(crypto, strlen(crypto), password);
  408. luat_heap_free(token_temp);
  409. }
  410. /*
  411. 百度物联网平台三元组生成
  412. @api iotauth.baidu(iot_core_id, device_key,device_secret,method,cur_timestamp)
  413. @string iot_core_id
  414. @string device_key
  415. @string device_secret
  416. @string method 加密方式,"MD5" "SHA256" 可选,默认"MD5"
  417. @number cur_timestamp 可选
  418. @return string mqtt三元组 client_id
  419. @return string mqtt三元组 user_name
  420. @return string mqtt三元组 password
  421. @usage
  422. local client_id,user_name,password = iotauth.baidu("abcd123","mydevice","ImSeCrEt0I1M2jkl","MD5")
  423. print(client_id,user_name,password)
  424. */
  425. static int l_iotauth_baidu(lua_State *L) {
  426. memset(user_name, 0, USER_NAME_LEN);
  427. memset(password, 0, PASSWORD_LEN);
  428. size_t len;
  429. const char* iot_core_id = luaL_checklstring(L, 1, &len);
  430. const char* device_key = luaL_checklstring(L, 2, &len);
  431. const char* device_secret = luaL_checklstring(L, 3, &len);
  432. const char* method = luaL_optlstring(L, 4, "MD5", &len);
  433. long long cur_timestamp = luaL_optinteger(L, 5,time(NULL) + 3600);
  434. baidu_token(iot_core_id,device_key,device_secret,method,cur_timestamp,user_name,password);
  435. lua_pushlstring(L, iot_core_id, strlen(iot_core_id));
  436. lua_pushlstring(L, user_name, strlen(user_name));
  437. lua_pushlstring(L, password, strlen(password));
  438. return 3;
  439. }
  440. #include "rotable2.h"
  441. static const rotable_Reg_t reg_iotauth[] =
  442. {
  443. { "aliyun" , ROREG_FUNC(l_iotauth_aliyun)},
  444. { "onenet" , ROREG_FUNC(l_iotauth_onenet)},
  445. { "iotda" , ROREG_FUNC(l_iotauth_iotda)},
  446. { "qcloud" , ROREG_FUNC(l_iotauth_qcloud)},
  447. { "tuya" , ROREG_FUNC(l_iotauth_tuya)},
  448. { "baidu" , ROREG_FUNC(l_iotauth_baidu)},
  449. { NULL, ROREG_INT(0)}
  450. };
  451. LUAMOD_API int luaopen_iotauth( lua_State *L ) {
  452. luat_newlib2(L, reg_iotauth);
  453. return 1;
  454. }