Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #33 get device connection string only on button click #53

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,16 @@ else

public async Task ShowConnectionString()
{
gateway.SymmetricKey = await this.Http.GetStringAsync($"api/Gateways/{gateway.DeviceId}/ConnectionString");

var parameter = new DialogParameters();

parameter.Add(nameof(Gateway.DeviceId), gateway.DeviceId);
parameter.Add(nameof(Gateway.EndPoint), gateway.EndPoint);
parameter.Add(nameof(Gateway.SymmetricKey), gateway.SymmetricKey);

var result = await DialogService.Show<ConnectionString>("ConnectionString gateway ", parameter).Result;

DialogService.Show<ConnectionString>("ConnectionString gateway ", parameter);
}

public async Task DeleteDeviceAsync()
Expand Down
17 changes: 17 additions & 0 deletions src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace AzureIoTHub.Portal.Server.Controllers
using System.Threading.Tasks;
using AzureIoTHub.Portal.Server.Helpers;
using AzureIoTHub.Portal.Server.Interfaces;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models;
using AzureIoTHub.Portal.Shared.Security;
Expand All @@ -36,17 +37,20 @@ public class GatewaysController : ControllerBase
private readonly RegistryManager registryManager;
private readonly IConfiguration configuration;
private readonly IDeviceService devicesService;
private readonly IConnectionStringManager connectionStringManager;

public GatewaysController(
IConfiguration configuration,
ILogger<GatewaysController> logger,
RegistryManager registryManager,
IConnectionStringManager connectionStringManager,
IDeviceService service)
{
this.logger = logger;
this.registryManager = registryManager;
this.configuration = configuration;
this.devicesService = service;
this.connectionStringManager = connectionStringManager;
}

/// <summary>
Expand Down Expand Up @@ -140,6 +144,19 @@ public async Task<IActionResult> Get(string deviceId)
}
}

[HttpGet("{deviceId}/ConnectionString")]
public async Task<IActionResult> GetSymmetricKey(string deviceId)
{
try
{
return this.Ok(await this.connectionStringManager.GetSymmetricKey(deviceId));
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
}

/// <summary>
/// this function create a device with the twin information.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static string RetrieveSymmetricKey(string deviceId, AttestationMechanism
// then we get the symmetricKey
SymmetricKeyAttestation symmetricKey = attestationMechanism.GetAttestation() as SymmetricKeyAttestation;
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(symmetricKey.PrimaryKey));

return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(deviceId)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) CGI France - Grand Est. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Managers
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Server.Helpers;
using AzureIoTHub.Portal.Server.Services;

public class ConnectionStringManager : IConnectionStringManager
{
private readonly IDeviceService deviceService;

public ConnectionStringManager(IDeviceService deviceService)
{
this.deviceService = deviceService;
}

public async Task<string> GetSymmetricKey(string deviceId)
{
try
{
var attestationMechanism = await this.deviceService.GetDpsAttestionMechanism();

return DeviceHelper.RetrieveSymmetricKey(deviceId, attestationMechanism);
}
catch (System.Exception e)
{
throw new System.Exception(e.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) CGI France - Grand Est. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Managers
{
using System.Threading.Tasks;

public interface IConnectionStringManager
{
Task<string> GetSymmetricKey(string deviceId);
}
}
9 changes: 8 additions & 1 deletion src/AzureIoTHub.Portal/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient(sp => new BlobServiceClient(configuration.StorageAccountConnectionString));
services.AddTransient<ITableClientFactory>(sp => new TableClientFactory(configuration.StorageAccountConnectionString));
services.AddTransient<IDeviceModelImageManager, DeviceModelImageManager>();
// services.AddTransient<ILoraDeviceMethodManager, LoraDeviceMethodManager>();
services.AddTransient<IDeviceService, DeviceService>();
services.AddTransient<IDeviceTwinMapper, DeviceTwinMapper>();
services.AddTransient<IDeviceModelCommandMapper, DeviceModelCommandMapper>();
services.AddTransient<IDeviceModelMapper, DeviceModelMapper>();
services.AddTransient<IConnectionStringManager, ConnectionStringManager>();

services.AddTransient<ConfigsServices>();

Expand Down Expand Up @@ -210,6 +210,7 @@ internal abstract class ConfigHandler
{
protected const string IoTHubConnectionStringKey = "IoTHub:ConnectionString";
protected const string DPSConnectionStringKey = "IoTDPS:ConnectionString";
protected const string DPSDefaultEnrollmentGroupeKey = "IoTDPS:DefaultEnrollmentGroupe";

protected const string MsalScopeNameKey = "MsalSettings:ScopeName";
protected const string MsalInstanceKey = "MsalSettings:Instance";
Expand Down Expand Up @@ -242,6 +243,8 @@ internal static ConfigHandler Create(IWebHostEnvironment env, IConfiguration con

internal abstract string DPSConnectionString { get; }

internal abstract string DPSDefaultEnrollmentGroupe { get; }

internal abstract string StorageAccountConnectionString { get; }

internal abstract string MsalScopeName { get; }
Expand Down Expand Up @@ -284,6 +287,8 @@ internal ProductionConfigHandler(IConfiguration config)

internal override string DPSConnectionString => this.config.GetConnectionString(DPSConnectionStringKey);

internal override string DPSDefaultEnrollmentGroupe => this.config[DPSDefaultEnrollmentGroupeKey];

internal override string StorageAccountConnectionString => this.config.GetConnectionString(StorageAccountConnectionStringKey);

internal override string MsalScopeName => this.config[MsalScopeNameKey];
Expand Down Expand Up @@ -326,6 +331,8 @@ internal DevelopmentConfigHandler(IConfiguration config)

internal override string DPSConnectionString => this.config[DPSConnectionStringKey];

internal override string DPSDefaultEnrollmentGroupe => this.config[DPSDefaultEnrollmentGroupeKey];

internal override string StorageAccountConnectionString => this.config[StorageAccountConnectionStringKey];

internal override string MsalScopeName => this.config[MsalScopeNameKey];
Expand Down