luat_lib_iotauth.c 20 KB

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