Commit 1cb32625 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Include common Api Library in Project Api (coscine/issues#157)

parent 60707448
using Coscine.Api.Project.Models;
using Coscine.ApiCommons.Utils;
using Coscine.Database.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
......
This diff is collapsed.
......@@ -4,10 +4,10 @@ using Microsoft.AspNetCore.Mvc;
using Coscine.Api.Project.Models;
using System.Linq;
using Coscine.Api.Project.ReturnObjects;
using Coscine.Api.Project.Factories;
using System.IO;
using System;
using Coscine.Api.Project.Exceptions;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
namespace Coscine.Api.Project.Tests
{
......
using Coscine.Api.Project.Controllers;
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;
using System;
......
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
......@@ -78,6 +82,20 @@
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="8.0.0" targetFramework="net472" />
<package id="AutoMapper.Extensions.Microsoft.DependencyInjection" version="6.0.0" targetFramework="net472" />
<package id="Castle.Core" version="4.4.0" targetFramework="net472" />
<package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.0.0" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.3.0" targetFramework="net472" />
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="linq2db" version="2.6.4" targetFramework="net472" />
<package id="linq2db.SqlServer" version="2.6.4" targetFramework="net472" />
<package id="linq2db.t4models" version="2.6.4" targetFramework="net472" />
<package id="LinqKit" version="1.1.16" targetFramework="net472" />
<package id="Microsoft.AspNetCore" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Antiforgery" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Authentication.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Authentication.Core" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Authorization" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Authorization.Policy" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Connections.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Cors" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Cryptography.Internal" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.DataProtection" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.DataProtection.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Diagnostics" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Diagnostics.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.HostFiltering" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Hosting.Server.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Html.Abstractions" version="2.2.0" targetFramework="net472" />
......@@ -23,31 +34,79 @@
<package id="Microsoft.AspNetCore.Http.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http.Extensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Http.Features" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.HttpOverrides" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.JsonPatch" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Localization" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Analyzers" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.ApiExplorer" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Core" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Cors" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.DataAnnotations" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Formatters.Json" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Localization" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Razor" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.Razor.Extensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.RazorPages" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.TagHelpers" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Mvc.ViewFeatures" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Razor" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Razor.Design" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Razor.Language" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Razor.Runtime" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.ResponseCaching.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Routing" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Routing.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.IIS" version="2.2.2" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.IISIntegration" version="2.2.1" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.Kestrel" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Core" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Https" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" version="2.2.1" targetFramework="net472" />
<package id="Microsoft.AspNetCore.WebUtilities" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="2.9.2" targetFramework="net472" />
<package id="Microsoft.CodeAnalysis.Common" version="3.0.0" targetFramework="net472" />
<package id="Microsoft.CodeAnalysis.CSharp" version="3.0.0" targetFramework="net472" />
<package id="Microsoft.CodeAnalysis.Razor" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="net472" />
<package id="Microsoft.DiaSymReader.Native" version="1.7.0" targetFramework="net472" />
<package id="Microsoft.DotNet.PlatformAbstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Caching.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Caching.Memory" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Binder" version="2.2.4" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.CommandLine" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.EnvironmentVariables" version="2.2.4" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Json" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.UserSecrets" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyModel" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Composite" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Hosting.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Localization" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Localization.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Configuration" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Console" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Debug" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.EventSource" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.ObjectPool" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Options" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Options.ConfigurationExtensions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Primitives" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Extensions.WebEncoders" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.5.0" targetFramework="net472" />
<package id="Microsoft.IdentityModel.Logging" version="5.5.0" targetFramework="net472" />
<package id="Microsoft.IdentityModel.Tokens" version="5.5.0" targetFramework="net472" />
<package id="Microsoft.Net.Http.Headers" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Win32.Registry" version="4.5.0" targetFramework="net472" />
<package id="Moq" version="4.12.0" targetFramework="net472" />
......@@ -55,17 +114,48 @@
<package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net472" />
<package id="NUnit" version="3.11.0" targetFramework="net472" />
<package id="NUnit3TestAdapter" version="3.13.0" targetFramework="net472" />
<package id="System.AppContext" version="4.3.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
<package id="System.Collections" version="4.3.0" targetFramework="net472" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net472" />
<package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="net472" />
<package id="System.Console" version="4.3.1" targetFramework="net472" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="4.5.1" targetFramework="net472" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net472" />
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net472" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net472" />
<package id="System.IdentityModel.Tokens.Jwt" version="5.5.0" targetFramework="net472" />
<package id="System.IO" version="4.3.0" targetFramework="net472" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net472" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net472" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net472" />
<package id="System.IO.Pipelines" version="4.5.3" targetFramework="net472" />
<package id="System.Linq" version="4.3.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.2" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Reflection.Metadata" version="1.6.0" targetFramework="net472" />
<package id="System.Runtime" version="4.3.1" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
<package id="System.Runtime.Extensions" version="4.3.1" targetFramework="net472" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net472" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net472" />
<package id="System.Security.AccessControl" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net472" />
<package id="System.Security.Cryptography.Cng" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.2" targetFramework="net472" />
<package id="System.Security.Cryptography.Xml" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Permissions" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Principal.Windows" version="4.5.1" targetFramework="net472" />
<package id="System.Text.Encoding.CodePages" version="4.5.1" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.5.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net472" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net472" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="System.Xml.ReaderWriter" version="4.3.1" targetFramework="net472" />
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net472" />
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net472" />
</packages>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Net;
namespace Coscine.Api.Project
{
public class ApplicationInformation
{
public string AppName { get; set; } = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
public string AppType { get; set; } = "apis";
public Version Version { get; set; } = ToSemanticVersion(System.Reflection.Assembly.GetEntryAssembly().GetName().Version);
public string DomainName { get; set; } = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
public string HostName { get; set; } = Dns.GetHostName();
public string PathPrefix { get; set; } = $"coscine/api/{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}";
public Tuple<int, int> PortRange = new Tuple<int, int>(6001, 6999);
public int Port { get; set; } = 0;
public string AppBasePath { get { return $"coscine/{AppType}/{AppName}"; } }
public string TraefikBackendPath { get { return $"traefik/backends/{AppName}/servers/{HostName}"; } }
public string TraefikFrontendPath { get { return $"traefik/frontends/{AppName}"; } }
public Dictionary<string, string> AppValues
{
get
{
return new Dictionary<string, string>()
{
{ $"{AppBasePath}/port", $"{Port}" },
{ $"{AppBasePath}/name", $"{AppName}" },
{ $"{AppBasePath}/version", $"{Version}" }
};
}
}
public Dictionary<string, string> TraefikValues
{
get
{
return new Dictionary<string, string>()
{
{ $"{TraefikBackendPath}/url", $"http://{HostName}.{DomainName}:{Port}"},
{ $"{TraefikBackendPath}/weight", $"{1}"},
{ $"{TraefikFrontendPath}/backend", AppName},
{ $"{TraefikFrontendPath}/routes/{AppName}/rule", $"Host:{HostName}.{DomainName};PathPrefix:/{PathPrefix}"}
};
}
}
private static Version ToSemanticVersion(Version version)
{
return new Version(version.Major, version.Minor, version.Build);
}
}
}
using Coscine.Api.Project.Models;
using Coscine.Database.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
namespace Coscine.Api.Project
{
public class Authenticator
{
private readonly Controller _controller;
public Authenticator(Controller controller)
{
_controller = controller;
}
public bool ValidUser()
{
try
{
GetUserFromToken();
}
catch (Exception)
{
return false;
}
return true;
}
public User GetUserFromToken()
{
var authorization = _controller.Request.Headers["Authorization"].ToArray();
string bearer = null;
foreach (var line in authorization)
{
if (line.Contains("Bearer"))
{
bearer = line;
}
}
if (!string.IsNullOrWhiteSpace(bearer))
{
bearer = bearer.Replace("Bearer", "").Trim();
JWTHandler jwtHandler = new JWTHandler(Program.Configuration);
var claims = jwtHandler.GetContents(bearer);
var userClaim = (from claimObj in claims
where claimObj.Type == "UserId"
select claimObj).First();
UserModel userModel = new UserModel();
return userModel.GetById(Guid.Parse(userClaim.Value));
}
else
{
throw new ArgumentException("Bearer Token is not set!");
}
}
public void ValidateAndExecute(Action<User> action)
{
action.Invoke(GetUserFromToken());
}
public T ValidateAndExecute<T>(Func<User, T> func)
{
return func.Invoke(GetUserFromToken());
}
// Todo: Add roles as parameter
public bool HasRequiredRights()
{
return true;
}
}
}
using Coscine.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
namespace Coscine.Api.Project
{
public class Configurator
{
public ApplicationInformation ApplicationInformation { get; set; } = new ApplicationInformation();
public IConfiguration Configuration { get; set; }
public Configurator(ApplicationInformation applicationInformation, IConfiguration configuration)
{
ApplicationInformation = applicationInformation;
Configuration = configuration;
}
public Configurator(IConfiguration configuration)
{
Configuration = configuration;
}
public void Register()
{
Register(ApplicationInformation);
}
public void Register(ApplicationInformation applicationInformation)
{
// Default app values
var keys = Configuration.Keys(applicationInformation.AppBasePath);
if (keys == null)
{
// No port overwrite
if (applicationInformation.Port == 0)
{
applicationInformation.Port = CalculatePort($"coscine/{applicationInformation.AppType}");
}
foreach (var kv in applicationInformation.AppValues.ToArray())
{
Configuration.Put(kv.Key, kv.Value);
}
}
else
{
// No port overwrite
if (applicationInformation.Port == 0) {
int.TryParse(Configuration.GetString($"{applicationInformation.AppBasePath}/port"), out int port);
applicationInformation.Port = port;
}
}
// Traefik Configuration
foreach (var kv in applicationInformation.TraefikValues.ToArray())
{
Configuration.Put(kv.Key, kv.Value);
}
}
private List<int> GetUsedConsulPorts(string appsPrefix)
{
var ports = new List<int>();
var keys = Configuration.Keys(appsPrefix);
if (keys != null)
{
foreach (var key in keys)
{
// maybe find a better solution
if (key.EndsWith("/port") && Configuration.Get(key) != null)
{
if (int.TryParse(Configuration.GetString(key), out int p))
{
ports.Add(p);
}
}
}
}
return ports;
}
private List<int> GetUsedSystemPorts()
{
return IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections().Select(x => x.LocalEndPoint.Port).ToList();
}
private int CalculatePort(string appsPrefix)
{
var usedPorts = GetUsedConsulPorts(appsPrefix).Union(GetUsedSystemPorts())
.OrderBy(x => x);
return Enumerable.Range(ApplicationInformation.PortRange.Item1, ApplicationInformation.PortRange.Item2 - ApplicationInformation.PortRange.Item1).Except(usedPorts).First();
}
}
}
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
......@@ -14,7 +15,7 @@ namespace Coscine.Api.Project.Controllers
public ProjectController()
{
_authenticator = new Authenticator(this);
_authenticator = new Authenticator(this, Program.Configuration);
}
[Route("[controller]")]
......
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
......@@ -14,7 +15,7 @@ namespace Coscine.Api.Project.Controllers
public ResourceController()
{
_authenticator = new Authenticator(this);
_authenticator = new Authenticator(this, Program.Configuration);
}
[Route("[controller]")]
......
using Coscine.Configuration;
using Coscine.Database.Model;
using System;
namespace Coscine.Api.Project
{
public class DatabaseConnection
{
private IConfiguration _configuration;
public DatabaseConnection(IConfiguration configuration)
{
_configuration = configuration;
}
private const string DbDataSourceKey = "coscine/global/db_data_source";
private const string DbNameKey = "coscine/global/db_name";
private const string DbUserIdKey = "coscine/global/db_user_id";
private const string DbPasswordKey = "coscine/global/db_password";
private string GetDbConnectionString()
{
var dbDataSource = _configuration.GetString(DbDataSourceKey);
var dbDatabase = _configuration.GetString(DbNameKey);
var dbUserId = _configuration.GetString(DbUserIdKey);
var dbPassword = _configuration.GetString(DbPasswordKey);
return $"Data Source={dbDataSource}; Database={dbDatabase}; User Id={dbUserId}; Password={dbPassword};";
}
private string GetProviderName()
{
return "SqlServer.2008";
}
public void ConnectToDatabase(Action<CoscineDB> action)
{
using (var coscineDB = new CoscineDB(GetProviderName(), GetDbConnectionString()))
{
action.Invoke(coscineDB);
}
}
public T ConnectToDatabase<T>(Func<CoscineDB, T> func)
{
T result;
using (var coscineDB = new CoscineDB(GetProviderName(), GetDbConnectionString()))
{
result = func.Invoke(coscineDB);
}
return result;
}
}
}
using System;
namespace Coscine.Api.Project.Exceptions
{
public class InvalidTokenException : Exception
{
public InvalidTokenException(string message) : base(message)
{
}
}
}
using System;
namespace Coscine.Api.Project.Exceptions
{
public class NotAuthorizedException : Exception
{
public NotAuthorizedException(string message) : base(message)
{
}
}
}
using System.IO;
using Newtonsoft.Json;
namespace Coscine.Api.Project.Factories
{
public class ObjectFactory<T>
{
public static Stream SerializeToStream(T o)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
var json = JsonConvert.SerializeObject(o);
writer.Write(json);
writer.Flush();
stream.Position = 0;
return stream;
}
public static T DeserializeFromStream(Stream body)
{
var sr = new StreamReader(body);