Exemple #1
0
        /// <summary>
        /// 授权客户端
        /// </summary>
        /// <param name="client">设备客户端</param>
        /// <param name="num">机身号</param>
        /// <param name="password">明文密码</param>
        /// <returns></returns>
        public static async Task AuthAclAsync(IRemoteClient client, string num, string password)
        {
            // HSET mqtt_user:<username> is_superuser 0
            // HSET mqtt_user:<username> password "passwd"

            var db = redisClient.GetMultiplexer().GetDatabase();

            var authKey       = string.Format("mqtt_user:{0}", num);
            var superEntry    = new HashEntry("is_superuser", 0);
            var passwordEntry = new HashEntry("password", Encryption.GetMD5(password).ToLower());

            await db.KeyDeleteAsync(authKey);

            await db.HashSetAsync(authKey, new[] { superEntry, passwordEntry });


            // 1: subscribe, 2: publish, 3: pubsub
            // HSET mqtt_acl:<username> topic1 1
            // HSET mqtt_acl:<username> topic2 2
            // HSET mqtt_acl:<username> topic3 3

            var subTopics = client.GetAllSubTopics();
            var pubTopics = client.GetAllPubTopics();

            var aclKey     = string.Format("mqtt_acl:{0}", num);
            var subEntries = subTopics.Select(topic => new HashEntry(topic.Value, 1));
            var pubEntries = pubTopics.Select(topic => new HashEntry(topic.Value, 2));
            var entries    = subEntries.Concat(pubEntries).ToArray();

            await db.KeyDeleteAsync(aclKey);

            await db.HashSetAsync(aclKey, entries);
        }