public void LinkSensor(int id, string sensorId) { // Has a sensor already been linked? var brewWithSensor = BrewRepository.GetAll().FirstOrDefault(b => b.SensorId == sensorId); if (brewWithSensor != null && brewWithSensor.Id != id) { throw new ArgumentException( string.Format("The sensor with id {0} can not be linked to the brew because the sensor has already been linked to another brew.", sensorId), "sensorId"); } // Get the brew var brew = GetBrew(id); // First unlink the current sensor if (!string.IsNullOrEmpty(brew.SensorId)) { UnlinkSensor(id, brew.SensorId); } // Link sensor in our datastore brew.SensorId = sensorId; brew.LastModified = DateTime.UtcNow; BrewRepository.CommitChanges(); // We want a custom identity for the sensor which only allows sending to the service bus. var serviceManagementWrapper = new ServiceManagementWrapper(AcsNamespace, ManagementIssuer, ManagementKey); var client = serviceManagementWrapper.CreateManagementServiceClient(); client.IgnoreResourceNotFoundException = true; // Clean up if we already exist as a sensor var existingRule = client.Rules.AddQueryOption("$filter", "Description eq '" + string.Format("Add Send claim value for sensor id {0}", sensorId) + "'").FirstOrDefault(); if (existingRule != null) { client.DeleteObject(existingRule); client.SaveChanges(SaveChangesOptions.Batch); } serviceManagementWrapper.RemoveServiceIdentity(sensorId); // Create a new identity var serviceIdentity = new ServiceIdentity { Name = sensorId, Description = string.Format("Sensor id: {0}", sensorId) }; var serviceIdentityKey = new ServiceIdentityKey { DisplayName = string.Format("Credentials for {0}", sensorId), Value = Encoding.UTF8.GetBytes(sensorId), Type = IdentityKeyTypes.Symmetric.ToString(), Usage = IdentityKeyUsages.Password.ToString(), StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddMonths(2) // sensors can be linked for up to 2 months }; // Process modifications to the namespace client.AddToServiceIdentities(serviceIdentity); client.AddRelatedObject(serviceIdentity, "ServiceIdentityKeys", serviceIdentityKey); client.SaveChanges(SaveChangesOptions.Batch); // Add a Send claim var issuer = client.Issuers.AddQueryOption("$filter", "Name eq 'LOCAL AUTHORITY'").FirstOrDefault(); var ruleGroup = client.RuleGroups.AddQueryOption("$filter", "Name eq 'Default Rule Group for ServiceBus'").FirstOrDefault(); var rule = new Rule { Description = string.Format("Add Send claim value for sensor id {0}", sensorId), InputClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", InputClaimValue = sensorId, OutputClaimType = "net.windows.servicebus.action", OutputClaimValue = "Send", IssuerId = issuer.Id, RuleGroupId = ruleGroup.Id, RuleGroup = ruleGroup, Issuer = issuer }; client.AddToRules(rule); client.SaveChanges(SaveChangesOptions.Batch); }
public void AddSimpleRuleToRuleGroup(string description, string ruleGroupName, string identityProviderName, string inputClaimType = null, string inputClaimValue = null, string outputClaimType = null, string outputClaimValue = null) { try { var rule = new Rule { Description = description, InputClaimType = inputClaimType, InputClaimValue = inputClaimValue, OutputClaimType = outputClaimType, OutputClaimValue = outputClaimValue }; this.AddRuleToRuleGroup(ruleGroupName, identityProviderName, rule); } catch (Exception ex) { throw TryGetExceptionDetails(ex); } }
private void AddRuleToRuleGroup(string ruleGroupName, string identityProviderName, Rule rule) { var client = this.CreateManagementServiceClient(); RuleGroup ruleGroup = client.RuleGroups.AddQueryOption("$filter", "Name eq '" + ruleGroupName + "'").FirstOrDefault(); if (ruleGroup == null) { throw new InvalidOperationException("Rule Group: " + ruleGroupName + " does not exist"); } Issuer issuer; if (identityProviderName.Equals("LOCAL AUTHORITY")) { issuer = client.Issuers.Where(m => m.Name == "LOCAL AUTHORITY").Single(); } else { IdentityProvider identityProvider = client.IdentityProviders.Expand("Issuer").Where(ip => ip.DisplayName.Equals(identityProviderName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); if (identityProvider == null) { throw new InvalidOperationException("Identity Provider: " + identityProviderName + " does not exist"); } issuer = identityProvider.Issuer; } rule.IssuerId = issuer.Id; rule.RuleGroup = ruleGroup; client.AddRelatedObject(ruleGroup, "Rules", rule); client.SaveChanges(); }
public void AddPassThroughRuleToRuleGroup(string ruleGroupName, string identityProviderName, string inputClaimType = null, string outputClaimType = null) { try { var rule = new Rule { InputClaimType = inputClaimType, OutputClaimType = outputClaimType }; this.AddRuleToRuleGroup(ruleGroupName, identityProviderName, rule); } catch (Exception ex) { throw TryGetExceptionDetails(ex); } }
private void AddRuleToRuleGroup(string ruleGroupName, Issuer issuer, Rule rule) { var client = this.CreateManagementServiceClient(); RuleGroup ruleGroup = client.RuleGroups.AddQueryOption("$filter", "Name eq '" + ruleGroupName + "'").FirstOrDefault(); if (ruleGroup == null) { throw new InvalidOperationException("Rule Group: " + ruleGroupName + " does not exist"); } rule.IssuerId = issuer.Id; rule.RuleGroup = ruleGroup; client.AddRelatedObject(ruleGroup, "Rules", rule); client.SaveChanges(); }