Skip to content

Commit

Permalink
Merge pull request #3 from buildersoftdev/datastorage-authorization
Browse files Browse the repository at this point in the history
✈ Feature: ReaderHub
  • Loading branch information
eneshoxha authored Jan 18, 2021
2 parents 1792ad6 + 234ec50 commit 53c6487
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public class ComponentDetail
public string TenantName { get; set; }
public string ProductName { get; set; }
public string ComponentName { get; set; }

public string ComponentDescription { get; set; }
public bool ComponentStatus { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ProductDetail
public Guid ProductId { get; set; }
public string TenantName { get; set; }
public string ProductName{ get; set; }
public bool ProductStatus { get; set; }
public string ProductDescription { get; set; }

// TODO... Add properties for this Product
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class TenantDetail
{
public Guid TenantId { get; set; }
public string TenantName { get; set; }
public string TenantDescription { get; set; }
public bool TenantStatus { get; set; }
public Encryption Encryption { get; set; }
public Signature Signature { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
</PropertyGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/Buildersoft.Andy.X.Router/Hubs/Readers/ReaderHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ public override Task OnConnectedAsync()
ReaderAs = (ReaderAs)Enum.Parse(typeof(ReaderAs), headers["x-andy-x-readeras"].ToString())
};

if (_readerRepository.IsTenantProductComponentActive(reader) != true)
{
Context.Abort();
return base.OnDisconnectedAsync(new Exception($"{reader.Tenant}/{reader.Product}/{reader.Component}, tenant, product or component is inactive: reader can not be connected"));
}

if (_readerRepository.IsReaderConnectable(reader) != true)
{
Context.Abort();
return base.OnDisconnectedAsync(new Exception($"This reader is already connected to this book {reader.Book}, please check the dashboard."));
}

//connect only if tenant, product and component states are true.

_readerRepository.Add(clientConnectionId, reader);

_ = _readerService.StoreReaderAsync(new Data.Model.DataStorages.ReaderDetail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,14 @@ public bool IsReaderConnectable(Reader reader)
}
return true;
}

public bool IsTenantProductComponentActive(Reader reader)
{
if (_storageMemoryRepository.GetTenants()[reader.Tenant].Status == true)
{
return true;
}
return false;
}
}
}
13 changes: 11 additions & 2 deletions src/Buildersoft.Andy.X.Utilities/Attributes/AuthorizeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ namespace Buildersoft.Andy.X.Utilities.Attributes
public class AuthorizeAttribute : Attribute, IAuthorizationFilter
{
public bool TenantOnly { get; set; } = false;
public bool DataStorageOnly { get; set; } = false;
public void OnAuthorization(AuthorizationFilterContext context)
{
var tenantName = context.HttpContext.Request.Headers["x-andy-x-tenant"].ToString();

if (TenantOnly == true)
{
var tenantName = context.HttpContext.Request.Headers["x-andy-x-tenant"].ToString();
if (tenantName == "" || context.HttpContext.Items["IsTenantValidated"].ToString() != true.ToString())
context.Result = new JsonResult(new { message = "Token is not valid for this tenant" }) { StatusCode = StatusCodes.Status401Unauthorized };
}

if (DataStorageOnly == true)
{
var dataStorage = context.HttpContext.Request.Headers["x-andyx-datastorage"].ToString();
if(dataStorage== ""||context.HttpContext.Items["IsDataStorageValidated"].ToString()!= true.ToString())
context.Result = new JsonResult(new { message = "Token is not valid for this data storage" }) { StatusCode = StatusCodes.Status401Unauthorized };
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Buildersoft.Andy.X/Buildersoft.Andy.X.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>Buildersoft</Authors>
<Company>Buildersoft</Company>
<Product>Buildersoft Andy</Product>
<Version>1.0.20.73</Version>
<Version>1.0.20.74</Version>
<Description>Buildersoft Andy X is a distributed messaging system. This system will empower developers to move into Event Driven Systems. Andy X is a multi-tenant system.</Description>
<Copyright>Copyright © Buildersoft 2020</Copyright>
<UserSecretsId>773f5b84-4c7d-47d6-919e-a898f66308a9</UserSecretsId>
Expand Down
4 changes: 3 additions & 1 deletion src/Buildersoft.Andy.X/Controllers/ComponentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ await _componentService.CreateComponentAsync(new Data.Model.DataStorages.Compone
ComponentId = component.Id,
TenantName = tenantName,
ProductName = productName,
ComponentName = componentName
ComponentName = componentName,
ComponentDescription = component.Description,
ComponentStatus = component.Status
});

return Ok(component);
Expand Down
9 changes: 8 additions & 1 deletion src/Buildersoft.Andy.X/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ public async Task<ActionResult<Product>> CreateProduct(string tenantName, string
Product product = _productLogic.CreateProduct(productName);
if (product != null)
{
await _productService.CreateProductAsync(new Data.Model.DataStorages.ProductDetail() { ProductId = product.Id, TenantName = tenantName, ProductName = productName });
await _productService.CreateProductAsync(new Data.Model.DataStorages.ProductDetail()
{
ProductId = product.Id,
TenantName = tenantName,
ProductName = productName,
ProductDescription = product.Description,
ProductStatus = product.Status
});
return Ok(product);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Buildersoft.Andy.X/Controllers/TenantsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public ActionResult<List<Tenant>> GetTenant(string tenantName)

Tenant tenant = _tenantLogic.GetTenant(tenantName);
if (tenant != null)
return Ok(tenant);
{
if (tenant.Status == true)
return Ok(tenant);
return BadRequest($"Tenant {tenantName} is inactive");
}

return NotFound("TENANT_NOT_FOUND");
}
Expand All @@ -62,7 +66,9 @@ await _tenantService.CreateTenantAsync(new Data.Model.DataStorages.TenantDetail(
TenantName = tenantName,
Encryption = tenant.GetEncryption(),
Signature = tenant.GetSignature(),
TenantId = tenant.Id
TenantId = tenant.Id,
TenantDescription = tenant.Description,
TenantStatus = tenant.Status
});

return Ok(new
Expand Down
9 changes: 9 additions & 0 deletions src/Buildersoft.Andy.X/Middleware/AuthorizationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ public async Task Invoke(HttpContext context)
{
var tenantName = context.Request.Headers["x-andy-x-tenant"].ToString();
if (tenantName == "")
{
context.Items["IsTenantValidated"] = false;

var dataStorageName = context.Request.Headers["x-andyx-datastorage"].ToString();
if (dataStorageName != "")
{
// Check datastorage service
// TODO... implement token validation for DataStorages
}
}
else
CheckTenantAccessToken(tenantName, context);

Expand Down
5 changes: 3 additions & 2 deletions src/Buildersoft.Andy.X/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public void ConfigureServices(IServiceCollection services)
{
options.AddPolicy("CorsPolicy", builder =>

builder.AllowAnyOrigin()
builder.WithOrigins("http://localhost:4200", "https://localhost:4200", "https://calm-sand-01dfcac03.azurestaticapps.net")
.AllowAnyMethod()
.AllowAnyHeader());
.AllowAnyHeader()
.AllowCredentials());
});

// Add Health Checks
Expand Down

0 comments on commit 53c6487

Please sign in to comment.