Browse Source

update: iotauth.onenet支持自定义res,从而支持老版本onenet平台的密钥计算

https://gitee.com/openLuat/LuatOS/issues/I8ZKWJ
Wendal Chen 2 years ago
parent
commit
b5dc2d6dd3

+ 20 - 12
components/iotauth/luat_iotauth.c

@@ -151,34 +151,42 @@ static int url_encoding_for_token(sign_msg* msg,char *token){
     return strlen(token);
 }
 
-void luat_onenet_token(const char* product_id,const char* device_name,const char* device_secret,long long cur_timestamp,char * method,char * version,char *token){
+// void luat_onenet_token(const char* product_id,const char* device_name,const char* device_secret,long long cur_timestamp,char * method,char * version,char *token){
+int luat_onenet_token(const iotauth_onenet_t* onenet, char* token) {
     size_t  declen = 0, enclen =  0,hmac_len = 0;
     char plaintext[64]     = { 0 };
     char hmac[64]          = { 0 };
     char StringForSignature[256] = { 0 };
     sign_msg sign = {0};
-    memcpy(sign.method, method, strlen(method));
-    memcpy(sign.version, version, strlen(version));
-    sprintf_(sign.et,"%lld",cur_timestamp);
-    sprintf_(sign.res,"products/%s/devices/%s",product_id,device_name);
-    luat_str_base64_decode((unsigned char *)plaintext, sizeof(plaintext), &declen, (const unsigned char * )device_secret, strlen((char*)device_secret));
+    memcpy(sign.method, onenet->method, strlen(onenet->method));
+    memcpy(sign.version, onenet->version, strlen(onenet->version));
+    sprintf_(sign.et,"%lld", onenet->cur_timestamp);
+    if (onenet->res) {
+        sprintf_(sign.res, "%s", onenet->res);
+    }
+    else {
+        sprintf_(sign.res,"products/%s/devices/%s", onenet->product_id, onenet->device_name);
+    }
+    
+    luat_str_base64_decode((unsigned char *)plaintext, sizeof(plaintext), &declen, (const unsigned char * )onenet->device_secret, strlen((char*)onenet->device_secret));
     sprintf_(StringForSignature, "%s\n%s\n%s\n%s", sign.et, sign.method, sign.res, sign.version);
-    if (!strcmp("md5", method)||!strcmp("MD5", method)) {
+    if (!strcmp("md5", onenet->method)||!strcmp("MD5", onenet->method)) {
         luat_crypto_hmac_md5_simple(StringForSignature, strlen(StringForSignature), plaintext, declen, hmac);
         hmac_len = 16;
-    }else if (!strcmp("sha1", method)||!strcmp("SHA1", method)) {
+    }else if (!strcmp("sha1", onenet->method)||!strcmp("SHA1", onenet->method)) {
         luat_crypto_hmac_sha1_simple(StringForSignature, strlen(StringForSignature),plaintext, declen,  hmac);
         hmac_len = 20;
-    }else if (!strcmp("sha256", method)||!strcmp("SHA256", method)) {
+    }else if (!strcmp("sha256", onenet->method)||!strcmp("SHA256", onenet->method)) {
         luat_crypto_hmac_sha256_simple(StringForSignature, strlen(StringForSignature),plaintext, declen,  hmac);
         hmac_len = 32;
     }else{
-        LLOGE("not support: %s",method);
-        return;
+        LLOGE("not support: %s", onenet->method);
+        return -1;
     }
     
     luat_str_base64_encode((unsigned char *)sign.sign, sizeof(sign.sign), &enclen, (const unsigned char * )hmac, hmac_len);
-    url_encoding_for_token(&sign,token);
+    url_encoding_for_token(&sign, token);
+    return 0;
 }
 
 void luat_iotda_token(const char* device_id,const char* device_secret,long long cur_timestamp,int ins_timestamp,char* client_id,const char* password){

+ 11 - 1
components/iotauth/luat_iotauth.h

@@ -12,8 +12,18 @@ typedef struct iotauth_ctx
     char password[PASSWORD_LEN];
 }iotauth_ctx_t;
 
+typedef struct iotauth_onenet {
+    const char* product_id;
+    const char* device_name;
+    const char* device_secret;
+    long long cur_timestamp;
+    const char* method;
+    const char* version;
+    const char* res;
+}iotauth_onenet_t;
+
 void luat_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);
-void luat_onenet_token(const char* product_id,const char* device_name,const char* device_secret,long long cur_timestamp,char * method,char * version,char *token);
+int luat_onenet_token(const iotauth_onenet_t* onenet, char* token);
 void luat_iotda_token(const char* device_id,const char* device_secret,long long cur_timestamp,int ins_timestamp,char* client_id,const char* password);
 void luat_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);
 void luat_tuya_token(const char* device_id,const char* device_secret,long long cur_timestamp,const char* password);

+ 41 - 18
components/iotauth/luat_lib_iotauth.c

@@ -54,34 +54,57 @@ static int l_iotauth_aliyun(lua_State *L) {
 /*
 中国移动物联网平台三元组生成
 @api iotauth.onenet(produt_id, device_name,key,method,cur_timestamp,version)
-@string produt_id
-@string device_name
-@string key
+@string produt_id 产品id
+@string device_name 设备名称
+@string key  设备密钥或者项目的acess_key
 @string method 加密方式,"md5" "sha1" "sha256" 可选,默认"md5"
-@number cur_timestamp 可选 默认为 32472115200(2999-01-01 0:0:0)
+@number 时间戳, 不用填
 @string version 可选 默认"2018-10-31"
+@string 当key是access_key时, 填 "products/" .. product_id . 本参数于2024.1.29新增
 @return string mqtt三元组 client_id
 @return string mqtt三元组 user_name
 @return string mqtt三元组 password
 @usage
-local client_id,user_name,password = iotauth.onenet("123456789","test","KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw=")
-print(client_id,user_name,password)
+-- OneNet平台官网: https://open.iot.10086.cn/
+-- OneNet有多种版本, 注意区分, 一般来说produt_id纯数字就是老版本, 否则就是新版本
+
+-- 新版OneNET平台, 产品id是英文字母字符串
+-- 对应demo/onenet/studio
+local produt_id = "Ck2AF9QD2K"
+local device_name = "test"
+local device_key = "KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw="
+local client_id,user_name,password = iotauth.onenet(produt_id, device_name, device_key)
+log.info("onenet.new", client_id,user_name,password)
+
+-- 旧版OneNET平台, 产品id是数字字符串. 2024.1.29新增
+-- 对应demo/onenet/old_mqtt
+local produt_id = "12342334"
+local device_name = "test"
+local access_key = "adfasdfadsfadsf="
+local client_id,user_name,password = iotauth.onenet(produt_id, device_name, access_key, nil, nil, nil, "products/" .. produt_id)
+log.info("onenet.old", client_id,user_name,password)
+
 */
 static int l_iotauth_onenet(lua_State *L) {
     char password[PASSWORD_LEN] = {0};
-    size_t len;
-    long long cur_timestamp = 32472115200;
-    const char* produt_id = luaL_checklstring(L, 1, &len);
-    const char* device_name = luaL_checklstring(L, 2, &len);
-    const char* key = luaL_checklstring(L, 3, &len);
-    const char* method = luaL_optlstring(L, 4, "md5", &len);
-    if (lua_type(L, (5)) == LUA_TNUMBER){
-        cur_timestamp = luaL_checkinteger(L, 5);
+    size_t len = 0;
+    iotauth_onenet_t onenet = {
+        .cur_timestamp = 32472115200
+    };
+    onenet.product_id = luaL_checkstring(L, 1);
+    onenet.device_name = luaL_checkstring(L, 2);
+    onenet.device_secret = luaL_checkstring(L, 3);
+    onenet.method = luaL_optstring(L, 4, "md5");
+    // if (lua_type(L, (5)) == LUA_TNUMBER){
+    //     cur_timestamp = luaL_checkinteger(L, 5);
+    // }
+    onenet.version = luaL_optlstring(L, 6, "2018-10-31", &len);
+    if (lua_type(L, 7) == LUA_TSTRING) {
+        onenet.res = luaL_checkstring(L, 7);
     }
-    const char* version = luaL_optlstring(L, 6, "2018-10-31", &len);
-    luat_onenet_token(produt_id,device_name,key,cur_timestamp,method,version, password);
-    lua_pushlstring(L, device_name, strlen(device_name));
-    lua_pushlstring(L, produt_id, strlen(produt_id));
+    luat_onenet_token(&onenet, password);
+    lua_pushlstring(L, onenet.device_name, strlen(onenet.device_name));
+    lua_pushlstring(L, onenet.product_id, strlen(onenet.product_id));
     lua_pushlstring(L, password, strlen(password));
     return 3;
 }