Skip to content
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
19 changes: 13 additions & 6 deletions HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using HwProj.AuthService.Client;
using System.Text;
using HwProj.AuthService.Client;
using HwProj.ContentService.Client;
using HwProj.CoursesService.Client;
using HwProj.NotificationsService.Client;
using HwProj.SolutionsService.Client;
using HwProj.Utils.Auth;
using HwProj.Utils.Configuration;
using HwProj.APIGateway.API.Filters;
using Microsoft.AspNetCore.Builder;
Expand All @@ -13,6 +13,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using IStudentsInfo;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using StudentsInfo;

namespace HwProj.APIGateway.API
Expand All @@ -35,10 +36,15 @@ public void ConfigureServices(IServiceCollection services)
Configuration["StudentsInfo:Password"],
Configuration["StudentsInfo:LdapHost"], int.Parse(Configuration["StudentsInfo:LdapPort"]),
Configuration["StudentsInfo:SearchBase"]));
const string authenticationProviderKey = "GatewayKey";

services.AddAuthentication()
.AddJwtBearer(authenticationProviderKey, x =>
var appSettings = Configuration.GetSection("Security");

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, x =>
{
x.RequireHttpsMetadata = false;
x.TokenValidationParameters = new TokenValidationParameters
Expand All @@ -47,7 +53,8 @@ public void ConfigureServices(IServiceCollection services)
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = AuthorizationKey.SecurityKey,
IssuerSigningKey =
new SymmetricSecurityKey(Encoding.ASCII.GetBytes(appSettings["SecurityKey"])),
ValidateIssuerSigningKey = true
};
});
Expand Down
5 changes: 4 additions & 1 deletion HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"Notifications": "http://localhost:5006",
"Solutions": "http://localhost:5007",
"Content": "http://localhost:5008"
},
},
"Security": {
"SecurityKey": "U8_.wpvk93fPWG<f2$Op[vwegmQGF25_fNG2V0ijnm2e0igv24g"
},
"StudentsInfo": {
"Login": "",
"Password": "",
Expand Down
10 changes: 5 additions & 5 deletions HwProj.AuthService/HwProj.AuthService.API/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

COPY ["Directory.Build.props", "Directory.Build.props"]

COPY ["HwProj.AuthService/HwProj.AuthService.API/", "HwProj.AuthService/HwProj.AuthService.API/"]
COPY ["HwProj.Common/HwProj.Utils/", "HwProj.Common/HwProj.Utils/"]
COPY ["HwProj.EventBus/HwProj.EventBus.Client/", "HwProj.EventBus/HwProj.EventBus.Client/"]
COPY ["HwProj.Common/HwProj.Common.Net8/", "HwProj.Common/HwProj.Common.Net8/"]
COPY ["HwProj.Common/HwProj.Models/", "HwProj.Common/HwProj.Models/"]
COPY ["HwProj.Common/HwProj.Repositories/", "HwProj.Common/HwProj.Repositories/"]
COPY ["HwProj.EventBus/HwProj.EventBus.Client/", "HwProj.EventBus/HwProj.EventBus.Client/"]
COPY ["HwProj.NotificationsService/HwProj.NotificationService.Events/", "HwProj.NotificationsService/HwProj.NotificationService.Events/"]

WORKDIR "/src/HwProj.AuthService/HwProj.AuthService.API"
RUN dotnet publish "HwProj.AuthService.API.csproj" -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS final
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app

COPY --from=build /app/publish .
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
<DockerfileContext>..\..</DockerfileContext>
<LangVersion>$(CSharpLanguageVersion)</LangVersion>
<UserSecretsId>603911e4-ace8-4439-96f8-1705a0dae761</UserSecretsId>
<Nullable>$(NullableReferenceTypes)</Nullable>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.20" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
<PackageReference Include="Octokit" Version="10.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\HwProj.Common\HwProj.Common.Net8\HwProj.Common.Net8.csproj" />
<ProjectReference Include="..\..\HwProj.Common\HwProj.Models\HwProj.Models.csproj" />
<ProjectReference Include="..\..\HwProj.Common\HwProj.Utils\HwProj.Utils.csproj" />
<ProjectReference Include="..\..\HwProj.EventBus\HwProj.EventBus.Client\HwProj.EventBus.Client.csproj" />
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationService.Events\HwProj.NotificationService.Events.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using HwProj.AuthService.API.Models;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Repositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace HwProj.AuthService.API.Repositories
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Threading.Tasks;
using HwProj.AuthService.API.Models;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Repositories;

namespace HwProj.AuthService.API.Repositories
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using HwProj.AuthService.API.Events;
using HwProj.EventBus.Client.Interfaces;
using HwProj.Models.Roles;
using HwProj.NotificationService.Events.AuthService;
using User = HwProj.AuthService.API.Models.User;

namespace HwProj.AuthService.API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
using AutoMapper;
using HwProj.AuthService.API.Extensions;
using HwProj.Models.Roles;
using HwProj.AuthService.API.Events;
using HwProj.AuthService.API.Repositories;
using HwProj.EventBus.Client.Interfaces;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Result;
using HwProj.Utils.Authorization;
using HwProj.NotificationService.Events.AuthService;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Octokit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using HwProj.Models.Result;
using HwProj.Models.Roles;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore.Internal;
using User = HwProj.AuthService.API.Models.User;

namespace HwProj.AuthService.API.Services
Expand Down Expand Up @@ -69,7 +68,7 @@ await _expertsRepository.AddAsync(new ExpertData
Id = user.Id,
LecturerId = lecturerId,
IsProfileEdited = false,
Tags = model.Tags.Join(";")
Tags = string.Join(';', model.Tags)
});

return Result.Success();
Expand Down Expand Up @@ -172,7 +171,7 @@ public async Task<Result> UpdateExpertTags(string lecturerId, UpdateExpertTagsDT

await _expertsRepository.UpdateAsync(updateExpertTagsDto.ExpertId, data => new ExpertData()
{
Tags = updateExpertTagsDto.Tags.Join(";")
Tags = string.Join(';', updateExpertTagsDto.Tags)
});

return Result.Success();
Expand Down
47 changes: 10 additions & 37 deletions HwProj.AuthService/HwProj.AuthService.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using HwProj.AuthService.API.Models;
using HwProj.AuthService.API.Repositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using HwProj.AuthService.API.Services;
using HwProj.Common.Net8;
using HwProj.EventBus.Client;
using HwProj.EventBus.Client.Interfaces;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using HwProj.Utils.Configuration;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Roles;
using HwProj.Utils.Auth;
using Microsoft.Extensions.Hosting;
using User = HwProj.AuthService.API.Models.User;

namespace HwProj.AuthService.API
Expand All @@ -31,25 +26,6 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureHwProjServices("AuthService API");

//var appSettingsSection = Configuration.GetSection("AppSettings");
//services.Configure<AppSettings>(appSettingsSection);

services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false; //TODO: dev env setting
x.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "AuthService",
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = AuthorizationKey.SecurityKey,
ValidateIssuerSigningKey = true
};
});

services.AddHttpClient();

var connectionString = ConnectionString.GetConnectionString(Configuration);
Expand Down Expand Up @@ -79,22 +55,19 @@ public void ConfigureServices(IServiceCollection services)
.AddScoped<IExpertsRepository, ExpertsRepository>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IdentityContext context)
public void Configure(IApplicationBuilder app, IHostEnvironment env, IdentityContext context)
{
app.ConfigureHwProj(env, "AuthService API", context);

using (var scope = app.ApplicationServices.CreateScope())
{
var userManager = scope.ServiceProvider.GetService(typeof(UserManager<User>)) as UserManager<User>;
using var scope = app.ApplicationServices.CreateScope();
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();

var rolesManager =
scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>)) as RoleManager<IdentityRole>;
var eventBus = scope.ServiceProvider.GetService<IEventBus>();
var rolesManager = scope.ServiceProvider.GetService<RoleManager<IdentityRole>>();
var eventBus = scope.ServiceProvider.GetService<IEventBus>();

if (env.IsDevelopment())
{
RoleInitializer.InitializeAsync(userManager, rolesManager, eventBus).Wait();
}
if (env.IsDevelopment())
{
RoleInitializer.InitializeAsync(userManager, rolesManager, eventBus).Wait();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion HwProj.AuthService/HwProj.AuthService.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnectionForWindows": "Server=(localdb)\\mssqllocaldb;Database=AuthServiceDB;Trusted_Connection=True;",
"DefaultConnectionForWindows": "Server=(localdb)\\mssqllocaldb;Database=AuthServiceDB;Trusted_Connection=True;TrustServerCertificate=true;",
"DefaultConnectionForLinux": "Server=localhost,1433;Database=AuthServiceDB;User ID=SA;Password=password_1234;"
},
"Logging": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private Claim[] ValidateToken(Result<TokenCredentials> resultData)
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = AuthorizationKey.SecurityKey,
IssuerSigningKey = null,
ValidateIssuerSigningKey = true
};
var claims = handler.ValidateToken(resultData.Value.AccessToken, validations, out var tokenSecure);
Expand Down
16 changes: 16 additions & 0 deletions HwProj.Common/HwProj.Common.Net8/ConnectionString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.InteropServices;
using Microsoft.Extensions.Configuration;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;

namespace HwProj.Common.Net8;

public static class ConnectionString
{
public static string GetConnectionString(IConfiguration configuration)
{
var option = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? "DefaultConnectionForLinux"
: "DefaultConnectionForWindows";
return configuration.GetConnectionString(option) ?? "";
}
}
13 changes: 13 additions & 0 deletions HwProj.Common/HwProj.Common.Net8/HwProj.Common.Net8.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
</ItemGroup>
</Project>
Loading
Loading