luat_lib_iotauth.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_iotauth.h"
  11. #define LUAT_LOG_TAG "iotauth"
  12. #include "luat_log.h"
  13. /*
  14. 阿里云物联网平台三元组生成
  15. @api iotauth.aliyun(product_key, device_name,device_secret,method,cur_timestamp)
  16. @string product_key
  17. @string device_name
  18. @string device_secret
  19. @string method 加密方式,"hmacmd5" "hmacsha1" "hmacsha256" 可选,默认"hmacmd5"
  20. @number cur_timestamp 可选 默认为 32472115200(2999-01-01 0:0:0)
  21. @bool istls 是否TLS直连 true:TLS直连 false:TCP直连模式 默认TCP直连模式
  22. @return string mqtt三元组 client_id
  23. @return string mqtt三元组 user_name
  24. @return string mqtt三元组 password
  25. @usage
  26. local client_id,user_name,password = iotauth.aliyun("123456789","abcdefg","Y877Bgo8X5owd3lcB5wWDjryNPoB")
  27. print(client_id,user_name,password)
  28. */
  29. static int l_iotauth_aliyun(lua_State *L) {
  30. iotauth_ctx_t ctx = {0};
  31. size_t len;
  32. uint8_t is_tls = 0;
  33. long long cur_timestamp = 32472115200;
  34. const char* product_key = luaL_checklstring(L, 1, &len);
  35. const char* device_name = luaL_checklstring(L, 2, &len);
  36. const char* device_secret = luaL_checklstring(L, 3, &len);
  37. const char* method = luaL_optlstring(L, 4, "hmacmd5", &len);
  38. if (lua_type(L, (5)) == LUA_TNUMBER){
  39. cur_timestamp = luaL_checkinteger(L, 5);
  40. }
  41. if (lua_isboolean(L, 6)){
  42. is_tls = lua_toboolean(L, 6);
  43. }
  44. luat_aliyun_token(product_key,device_name,device_secret,cur_timestamp,method,is_tls,ctx.client_id,ctx.user_name,ctx.password);
  45. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  46. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  47. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  48. return 3;
  49. }
  50. /*
  51. 中国移动物联网平台三元组生成
  52. @api iotauth.onenet(produt_id, device_name,key,method,cur_timestamp,version)
  53. @string produt_id 产品id
  54. @string device_name 设备名称
  55. @string key 设备密钥或者项目的acess_key
  56. @string method 加密方式,"md5" "sha1" "sha256" 可选,默认"md5"
  57. @number 时间戳, 不用填
  58. @string version 可选 默认"2018-10-31"
  59. @string 当key是access_key时, 填 "products/" .. product_id . 本参数于2024.1.29新增
  60. @return string mqtt三元组 client_id
  61. @return string mqtt三元组 user_name
  62. @return string mqtt三元组 password
  63. @usage
  64. -- OneNet平台官网: https://open.iot.10086.cn/
  65. -- OneNet有多种版本, 注意区分, 一般来说produt_id纯数字就是老版本, 否则就是新版本
  66. -- 新版OneNET平台, 产品id是英文字母字符串
  67. -- 对应demo/onenet/studio
  68. local produt_id = "Ck2AF9QD2K"
  69. local device_name = "test"
  70. local device_key = "KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw="
  71. local client_id,user_name,password = iotauth.onenet(produt_id, device_name, device_key)
  72. log.info("onenet.new", client_id,user_name,password)
  73. -- 旧版OneNET平台, 产品id是数字字符串. 2024.1.29新增
  74. -- 对应demo/onenet/old_mqtt
  75. local produt_id = "12342334"
  76. local device_name = "test"
  77. local access_key = "adfasdfadsfadsf="
  78. local client_id,user_name,password = iotauth.onenet(produt_id, device_name, access_key, nil, nil, nil, "products/" .. produt_id)
  79. log.info("onenet.old", client_id,user_name,password)
  80. */
  81. static int l_iotauth_onenet(lua_State *L) {
  82. char password[PASSWORD_LEN] = {0};
  83. size_t len = 0;
  84. iotauth_onenet_t onenet = {
  85. .cur_timestamp = 32472115200
  86. };
  87. onenet.product_id = luaL_checkstring(L, 1);
  88. onenet.device_name = luaL_checkstring(L, 2);
  89. onenet.device_secret = luaL_checkstring(L, 3);
  90. onenet.method = luaL_optstring(L, 4, "md5");
  91. // if (lua_type(L, (5)) == LUA_TNUMBER){
  92. // cur_timestamp = luaL_checkinteger(L, 5);
  93. // }
  94. onenet.version = luaL_optlstring(L, 6, "2018-10-31", &len);
  95. if (lua_type(L, 7) == LUA_TSTRING) {
  96. onenet.res = luaL_checkstring(L, 7);
  97. }
  98. luat_onenet_token(&onenet, password);
  99. lua_pushlstring(L, onenet.device_name, strlen(onenet.device_name));
  100. lua_pushlstring(L, onenet.product_id, strlen(onenet.product_id));
  101. lua_pushlstring(L, password, strlen(password));
  102. return 3;
  103. }
  104. /*
  105. 华为物联网平台三元组生成
  106. @api iotauth.iotda(device_id,device_secret,cur_timestamp)
  107. @string device_id
  108. @string device_secret
  109. @number cur_timestamp 可选 如不填则不校验时间戳
  110. @return string mqtt三元组 client_id
  111. @return string mqtt三元组 user_name
  112. @return string mqtt三元组 password
  113. @usage
  114. local client_id,user_name,password = iotauth.iotda("6203cc94c7fb24029b110408_88888888","123456789")
  115. print(client_id,user_name,password)
  116. */
  117. static int l_iotauth_iotda(lua_State *L) {
  118. char client_id[CLIENT_ID_LEN] = {0};
  119. char password[PASSWORD_LEN] = {0};
  120. size_t len = 0;
  121. long long cur_timestamp = 32472115200;
  122. int ins_timestamp = 0;
  123. const char* device_id = luaL_checklstring(L, 1, &len);
  124. const char* device_secret = luaL_checklstring(L, 2, &len);
  125. if (lua_type(L, (3)) == LUA_TNUMBER){
  126. cur_timestamp = luaL_checkinteger(L, 3);
  127. ins_timestamp = 1;
  128. }
  129. luat_iotda_token(device_id,device_secret,cur_timestamp,ins_timestamp,client_id,password);
  130. lua_pushlstring(L, client_id, strlen(client_id));
  131. lua_pushlstring(L, device_id, strlen(device_id));
  132. lua_pushlstring(L, password, strlen(password));
  133. return 3;
  134. }
  135. /*
  136. 腾讯联网平台三元组生成
  137. @api iotauth.qcloud(product_id, device_name,device_secret,method,cur_timestamp,sdk_appid)
  138. @string 产品id,创建项目后可以查看到,类似于LD8S5J1L07
  139. @string 设备名称,例如设备的imei号
  140. @string 设备密钥,创建设备后,查看设备详情可得到
  141. @string method 加密方式,"sha1" "sha256" 可选,默认"sha256"
  142. @number cur_timestamp 可选 默认为 32472115200(2999-01-01 0:0:0)
  143. @string sdk_appid 可选 默认为"12010126"
  144. @return string mqtt三元组 client_id
  145. @return string mqtt三元组 user_name
  146. @return string mqtt三元组 password
  147. @usage
  148. local client_id,user_name,password = iotauth.qcloud("LD8S5J1L07","test","acyv3QDJrRa0fW5UE58KnQ==")
  149. print(client_id,user_name,password)
  150. */
  151. static int l_iotauth_qcloud(lua_State *L) {
  152. iotauth_ctx_t ctx = {0};
  153. size_t len = 0;
  154. long long cur_timestamp = 32472115200;
  155. const char* product_id = luaL_checklstring(L, 1, &len);
  156. const char* device_name = luaL_checklstring(L, 2, &len);
  157. const char* device_secret = luaL_checklstring(L, 3, &len);
  158. const char* method = luaL_optlstring(L, 4, "sha256", &len);
  159. if (lua_type(L, (5)) == LUA_TNUMBER){
  160. cur_timestamp = luaL_checkinteger(L, 5);
  161. }
  162. const char* sdk_appid = luaL_optlstring(L, 6, "12010126", &len);
  163. luat_qcloud_token(product_id, device_name,device_secret,cur_timestamp,method,sdk_appid,ctx.user_name,ctx.password);
  164. snprintf_(ctx.client_id, CLIENT_ID_LEN,"%s%s", product_id,device_name);
  165. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  166. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  167. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  168. return 3;
  169. }
  170. /*
  171. 涂鸦联网平台三元组生成
  172. @api iotauth.tuya(device_id,device_secret,cur_timestamp)
  173. @string device_id
  174. @string device_secret
  175. @number cur_timestamp 可选 默认7258089600(2200-01-01 0:0:0)
  176. @return string mqtt三元组 client_id
  177. @return string mqtt三元组 user_name
  178. @return string mqtt三元组 password
  179. @usage
  180. local client_id,user_name,password = iotauth.tuya("6c95875d0f5ba69607nzfl","fb803786602df760")
  181. print(client_id,user_name,password)
  182. */
  183. static int l_iotauth_tuya(lua_State *L) {
  184. iotauth_ctx_t ctx = {0};
  185. size_t len = 0;
  186. long long cur_timestamp = 7258089600;
  187. const char* device_id = luaL_checklstring(L, 1, &len);
  188. const char* device_secret = luaL_checklstring(L, 2, &len);
  189. if (lua_type(L, (3)) == LUA_TNUMBER){
  190. cur_timestamp = luaL_checkinteger(L, 3);
  191. }
  192. luat_tuya_token(device_id,device_secret,cur_timestamp,ctx.password);
  193. snprintf_(ctx.client_id, CLIENT_ID_LEN, "tuyalink_%s", device_id);
  194. snprintf_(ctx.user_name, USER_NAME_LEN, "%s|signMethod=hmacSha256,timestamp=%lld,secureMode=1,accessType=1", device_id,cur_timestamp);
  195. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  196. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  197. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  198. return 3;
  199. }
  200. /*
  201. 百度物联网平台三元组生成
  202. @api iotauth.baidu(iot_core_id, device_key,device_secret,method,cur_timestamp)
  203. @string iot_core_id
  204. @string device_key
  205. @string device_secret
  206. @string method 加密方式,"MD5" "SHA256" 可选,默认"MD5"
  207. @number cur_timestamp 可选 如不填则不校验时间戳
  208. @return string mqtt三元组 client_id
  209. @return string mqtt三元组 user_name
  210. @return string mqtt三元组 password
  211. @usage
  212. local client_id,user_name,password = iotauth.baidu("abcd123","mydevice","ImSeCrEt0I1M2jkl")
  213. print(client_id,user_name,password)
  214. */
  215. static int l_iotauth_baidu(lua_State *L) {
  216. char user_name[USER_NAME_LEN] = {0};
  217. char password[PASSWORD_LEN] = {0};
  218. size_t len = 0;
  219. const char* iot_core_id = luaL_checklstring(L, 1, &len);
  220. const char* device_key = luaL_checklstring(L, 2, &len);
  221. const char* device_secret = luaL_checklstring(L, 3, &len);
  222. const char* method = luaL_optlstring(L, 4, "MD5", &len);
  223. long long cur_timestamp = luaL_optinteger(L, 5, 0);
  224. luat_baidu_token(iot_core_id,device_key,device_secret,method,cur_timestamp,user_name,password);
  225. lua_pushlstring(L, iot_core_id, strlen(iot_core_id));
  226. lua_pushlstring(L, user_name, strlen(user_name));
  227. lua_pushlstring(L, password, strlen(password));
  228. return 3;
  229. }
  230. #include "rotable2.h"
  231. static const rotable_Reg_t reg_iotauth[] =
  232. {
  233. { "aliyun" , ROREG_FUNC(l_iotauth_aliyun)},
  234. { "onenet" , ROREG_FUNC(l_iotauth_onenet)},
  235. { "iotda" , ROREG_FUNC(l_iotauth_iotda)},
  236. { "qcloud" , ROREG_FUNC(l_iotauth_qcloud)},
  237. { "tuya" , ROREG_FUNC(l_iotauth_tuya)},
  238. { "baidu" , ROREG_FUNC(l_iotauth_baidu)},
  239. { NULL, ROREG_INT(0)}
  240. };
  241. LUAMOD_API int luaopen_iotauth( lua_State *L ) {
  242. luat_newlib2(L, reg_iotauth);
  243. return 1;
  244. }