luat_lib_iotauth.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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(&ctx,product_key,device_name,device_secret,cur_timestamp,method,is_tls);
  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. iotauth_ctx_t ctx = {0};
  83. // char password[PASSWORD_LEN] = {0};
  84. size_t len = 0;
  85. iotauth_onenet_t onenet = {
  86. .cur_timestamp = 32472115200
  87. };
  88. onenet.product_id = luaL_checkstring(L, 1);
  89. onenet.device_name = luaL_checkstring(L, 2);
  90. onenet.device_secret = luaL_checkstring(L, 3);
  91. onenet.method = luaL_optstring(L, 4, "md5");
  92. // if (lua_type(L, (5)) == LUA_TNUMBER){
  93. // cur_timestamp = luaL_checkinteger(L, 5);
  94. // }
  95. onenet.version = luaL_optlstring(L, 6, "2018-10-31", &len);
  96. if (lua_type(L, 7) == LUA_TSTRING) {
  97. onenet.res = luaL_checkstring(L, 7);
  98. }
  99. luat_onenet_token(&ctx,&onenet);
  100. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  101. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  102. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  103. return 3;
  104. }
  105. /*
  106. 华为物联网平台三元组生成
  107. @api iotauth.iotda(device_id,device_secret,cur_timestamp)
  108. @string device_id
  109. @string device_secret
  110. @number cur_timestamp 可选 如不填则不校验时间戳
  111. @return string mqtt三元组 client_id
  112. @return string mqtt三元组 user_name
  113. @return string mqtt三元组 password
  114. @usage
  115. local client_id,user_name,password = iotauth.iotda("6203cc94c7fb24029b110408_88888888","123456789")
  116. print(client_id,user_name,password)
  117. */
  118. static int l_iotauth_iotda(lua_State *L) {
  119. iotauth_ctx_t ctx = {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(&ctx,device_id,device_secret,cur_timestamp,ins_timestamp);
  130. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  131. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  132. lua_pushlstring(L, ctx.password, strlen(ctx.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(&ctx,product_id, device_name,device_secret,cur_timestamp,method,sdk_appid);
  164. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  165. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  166. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  167. return 3;
  168. }
  169. /*
  170. 涂鸦联网平台三元组生成
  171. @api iotauth.tuya(device_id,device_secret,cur_timestamp)
  172. @string device_id
  173. @string device_secret
  174. @number cur_timestamp 可选 默认7258089600(2200-01-01 0:0:0)
  175. @return string mqtt三元组 client_id
  176. @return string mqtt三元组 user_name
  177. @return string mqtt三元组 password
  178. @usage
  179. local client_id,user_name,password = iotauth.tuya("6c95875d0f5ba69607nzfl","fb803786602df760")
  180. print(client_id,user_name,password)
  181. */
  182. static int l_iotauth_tuya(lua_State *L) {
  183. iotauth_ctx_t ctx = {0};
  184. size_t len = 0;
  185. long long cur_timestamp = 7258089600;
  186. const char* device_id = luaL_checklstring(L, 1, &len);
  187. const char* device_secret = luaL_checklstring(L, 2, &len);
  188. if (lua_type(L, (3)) == LUA_TNUMBER){
  189. cur_timestamp = luaL_checkinteger(L, 3);
  190. }
  191. luat_tuya_token(&ctx,device_id,device_secret,cur_timestamp);
  192. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  193. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  194. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  195. return 3;
  196. }
  197. /*
  198. 百度物联网平台三元组生成
  199. @api iotauth.baidu(iot_core_id, device_key,device_secret,method,cur_timestamp)
  200. @string iot_core_id
  201. @string device_key
  202. @string device_secret
  203. @string method 加密方式,"MD5" "SHA256" 可选,默认"MD5"
  204. @number cur_timestamp 可选 如不填则不校验时间戳
  205. @return string mqtt三元组 client_id
  206. @return string mqtt三元组 user_name
  207. @return string mqtt三元组 password
  208. @usage
  209. local client_id,user_name,password = iotauth.baidu("abcd123","mydevice","ImSeCrEt0I1M2jkl")
  210. print(client_id,user_name,password)
  211. */
  212. static int l_iotauth_baidu(lua_State *L) {
  213. iotauth_ctx_t ctx = {0};
  214. size_t len = 0;
  215. const char* iot_core_id = luaL_checklstring(L, 1, &len);
  216. const char* device_key = luaL_checklstring(L, 2, &len);
  217. const char* device_secret = luaL_checklstring(L, 3, &len);
  218. const char* method = luaL_optlstring(L, 4, "MD5", &len);
  219. long long cur_timestamp = luaL_optinteger(L, 5, 0);
  220. luat_baidu_token(&ctx,iot_core_id,device_key,device_secret,method,cur_timestamp);
  221. lua_pushlstring(L, ctx.client_id, strlen(ctx.client_id));
  222. lua_pushlstring(L, ctx.user_name, strlen(ctx.user_name));
  223. lua_pushlstring(L, ctx.password, strlen(ctx.password));
  224. return 3;
  225. }
  226. #include "rotable2.h"
  227. static const rotable_Reg_t reg_iotauth[] =
  228. {
  229. { "aliyun" , ROREG_FUNC(l_iotauth_aliyun)},
  230. { "onenet" , ROREG_FUNC(l_iotauth_onenet)},
  231. { "iotda" , ROREG_FUNC(l_iotauth_iotda)},
  232. { "qcloud" , ROREG_FUNC(l_iotauth_qcloud)},
  233. { "tuya" , ROREG_FUNC(l_iotauth_tuya)},
  234. { "baidu" , ROREG_FUNC(l_iotauth_baidu)},
  235. { NULL, ROREG_INT(0)}
  236. };
  237. LUAMOD_API int luaopen_iotauth( lua_State *L ) {
  238. luat_newlib2(L, reg_iotauth);
  239. return 1;
  240. }