Beispiel #1
0
        public async Task <IActionResult> Update([FromBody] UpdateDeviceViewModel model)
        {
            if (ModelState.IsValid)
            {
                var device = _dbContext.Devices.Find(new Guid(model.DeviceId));
                if (device == null)
                {
                    // If device can't be located in the database return 404
                    return(NotFound());
                }

                if (device.Registered)
                {
                    // If device has already been registered then it can't be re-registered
                    // i.e. the public key and device token are immutable
                    return(BadRequest());
                }

                // Plan to allow ModelState.IsValid perform HMAC validation to ensure correctness at this point
                // Initialize a HMAC object for calculating the hash from the Symmetric Key stored with the device
                // Calculate the HMAC of the submitted data against the provided HMAC to ensure the submitting entity has the symmetric key
                // If the HMAC is valid then the submission is authorized to update the device
                var key          = Convert.FromBase64String(device.SymmetricKey);
                var hmac         = new HMACSHA256(key);
                var computedHash = hmac.ComputeHash(model.GetCombinedByteString());
                var providedHash = Convert.FromBase64String(model.HMAC);
                if (!computedHash.SequenceEqual(providedHash))
                {
                    return(BadRequest());
                }

                // Valid submission so apply the values to the database
                device.DeviceToken = model.DeviceToken;
                device.PublicKey   = model.PublicKey;
                device.Registered  = true;

                _dbContext.Update(device);
                var result = await _dbContext.SaveChangesAsync();

                if (result != 1)
                {
                    // Invalid update number
                    return(StatusCode(500));
                }

                // Update success
                return(Ok());
            }

            // Model validation did not pass
            return(BadRequest());
        }
Beispiel #2
0
        public IActionResult GetHMAC(UpdateDeviceViewModel model)
        {
            var device = _dbContext.Devices.Find(new Guid(model.DeviceId));

            if (device == null)
            {
                // If device can't be located in the database return 404
                return(NotFound());
            }

            var data = model.GetCombinedByteString();
            var key  = Convert.FromBase64String(device.SymmetricKey);
            var hmac = new HMACSHA256(key);

            var result = new GetHMACModel
            {
                HMAC = Convert.ToBase64String(hmac.ComputeHash(data))
            };

            return(View(result));
        }
Beispiel #3
0
        public async Task <DeviceInfoModel> UpdateDeviceInfo(UpdateDeviceViewModel updateDeviceViewModel, AccessPolicyModel accessPolicyModel, string deviceId, string access_token)
        {
            DeviceInfoModel deviceInfoModel = await GetDeviceInfo(accessPolicyModel, deviceId, access_token);

            deviceInfoModel.status = updateDeviceViewModel.status;
            deviceInfoModel.authentication.symmetricKey.primaryKey   = updateDeviceViewModel.primaryKey;
            deviceInfoModel.authentication.symmetricKey.secondaryKey = updateDeviceViewModel.secondaryKey;
            RequestDeviceModel requestDeviceModel = new RequestDeviceModel
            {
                apiVersion             = "2018-08-30-preview",
                authorizationPolicyKey = accessPolicyModel.SharedAccessKey,
                etag = deviceInfoModel.etag,
                authorizationPolicyName = accessPolicyModel.SharedAccessKeyName,
                hostName    = accessPolicyModel.HostName,
                requestBody = JsonConvert.SerializeObject(deviceInfoModel),
                requestPath = string.Format("/devices/{0}", deviceId)
            };
            string url     = "https://main.iothub.ext.azure.cn/api/dataPlane/put";
            var    request = new HttpRequestMessage(HttpMethod.Post, url);
            var    client  = this._clientFactory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
            string requestbody = JsonConvert.SerializeObject(requestDeviceModel);

            request.Content = new StringContent(requestbody, UnicodeEncoding.UTF8, "application/json");
            var response = await client.SendAsync(request);

            string result = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                TModel <DeviceInfoModel> job = JsonConvert.DeserializeObject <TModel <DeviceInfoModel> >(result);
                return(job.body);
            }
            else
            {
                return(null);
            }
        }
 public async Task <object> UpdateDeviceInfo(string email, string subid, string resourceGroupName, string resourceName, string deviceId, [FromBody] UpdateDeviceViewModel updateDeviceViewModel)
 {
     return(await this._ioTHubResourceBus.UpdateDeviceInfo(email, subid, resourceGroupName, resourceName, deviceId, updateDeviceViewModel));
 }
Beispiel #5
0
        public async Task <DeviceInfoModel> UpdateDeviceInfo(string email, string subid, string resourceGroupName, string resourceName, string deviceId, UpdateDeviceViewModel updateDeviceViewModel)
        {
            string     access_token = _tokenDto.GetTokenString(email, _tokenResource.Value.manage);
            IoTHubKeys ioTHubKeys   = await this._ioTHubResourceDto.GetIoTHubKeys(subid, resourceGroupName, resourceName, access_token);

            IoTHubInfoModel ioTHubInfoModel = await this._ioTHubResourceDto.GetIoTHubInfo(subid, resourceGroupName, resourceName, access_token);

            AccessPolicyModel accessPolicyModel = new AccessPolicyModel()
            {
                HostName            = ioTHubInfoModel.properties.hostName,
                SharedAccessKeyName = ioTHubKeys.value[0].keyName,
                SharedAccessKey     = ioTHubKeys.value[0].primaryKey
            };

            return(await this._ioTHubResourceDto.UpdateDeviceInfo(updateDeviceViewModel, accessPolicyModel, deviceId, access_token));
        }