diff --git a/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs b/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs index 43c9f48..7301dca 100644 --- a/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs +++ b/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs @@ -1,8 +1,8 @@ -using Microsoft.AspNetCore.Authentication; +using IdentityServer.PublishedLanguage.Dto; +using IdentityServer.Wrapper.Services; +using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using NetworkResurrector.Application.Services; -using NetworkResurrector.Domain.Entities; using System.Net.Http.Headers; using System.Security.Claims; using System.Text.Encodings.Web; @@ -12,12 +12,12 @@ namespace NetworkResurrector.Api.Authentication { public class BasicAuthenticationHandler : AuthenticationHandler { - private readonly IUserService _userService; + private readonly IIdentityService _identityService; - public BasicAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserService userService) + public BasicAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService) : base(options, logger, encoder, clock) { - _userService = userService; + _identityService = identityService; } protected override async Task HandleAuthenticateAsync() @@ -30,7 +30,7 @@ namespace NetworkResurrector.Api.Authentication { var authorizationHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); var token = authorizationHeader.Parameter; - user = await _userService.Authenticate(token); + user = await _identityService.Authorize(token); } catch { diff --git a/NetworkResurrector.Api/Controllers/ResurrectorController.cs b/NetworkResurrector.Api/Controllers/ResurrectorController.cs index 2515887..1080256 100644 --- a/NetworkResurrector.Api/Controllers/ResurrectorController.cs +++ b/NetworkResurrector.Api/Controllers/ResurrectorController.cs @@ -2,7 +2,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using NetworkResurrector.Application.Commands; -using NetworkResurrector.Application.Queries; using System.Threading.Tasks; namespace NetworkResurrector.Api.Controllers @@ -19,20 +18,6 @@ namespace NetworkResurrector.Api.Controllers _mediator = mediator; } - [AllowAnonymous] - [HttpGet("token/{userName}/{password}")] - public async Task GetToken([FromRoute] GetToken.Query query) - { - var result = await _mediator.Send(query); - return Ok(result); - } - - [HttpGet("validate-token")] - public IActionResult ValidateToken() - { - return Ok("Valid"); - } - [HttpPost("wake")] public async Task WakeMachine([FromBody] WakeMachine wakeMachine) { diff --git a/NetworkResurrector.Api/NetworkResurrector.Api.csproj b/NetworkResurrector.Api/NetworkResurrector.Api.csproj index ef3c70e..61cad6f 100644 --- a/NetworkResurrector.Api/NetworkResurrector.Api.csproj +++ b/NetworkResurrector.Api/NetworkResurrector.Api.csproj @@ -1,10 +1,11 @@ - + netcoreapp3.1 + diff --git a/NetworkResurrector.Api/Startup.cs b/NetworkResurrector.Api/Startup.cs index 4698504..18625ec 100644 --- a/NetworkResurrector.Api/Startup.cs +++ b/NetworkResurrector.Api/Startup.cs @@ -1,4 +1,5 @@ using AutoMapper; +using IdentityServer.Wrapper; using MediatR; using MediatR.Pipeline; using Microsoft.AspNetCore.Authentication; @@ -44,6 +45,9 @@ namespace NetworkResurrector.Api services.AddAutoMapper( typeof(Application.Mappings.MappingProfile).Assembly); + // Identity server + services.UseIdentityServices(_configuration.GetSection("IdentityServer")["BaseAddress"]); + // Swagger services.AddSwagger("NetworkResurrector API"); @@ -80,7 +84,7 @@ namespace NetworkResurrector.Api private Assembly[] GetMediatRAssemblies() { - var assembly = typeof(Application.Queries.GetToken).Assembly; + var assembly = typeof(Application.Commands.WakeMachine).Assembly; return new Assembly[] { assembly }; } } diff --git a/NetworkResurrector.Api/appsettings.json b/NetworkResurrector.Api/appsettings.json index 6944a6e..23c2097 100644 --- a/NetworkResurrector.Api/appsettings.json +++ b/NetworkResurrector.Api/appsettings.json @@ -11,20 +11,14 @@ } }, "AllowedHosts": "*", - "Users": [ - { - "UserId": 1, - "UserName": "***REMOVED***", - "Password": "***REMOVED***" - } - ], + "IdentityServer": { + "BaseAddress": "http://localhost:5063/" + }, "WakeOnLan": { "Provider": { "Use": "Inhouse", "Options": [ "Inhouse", "Nikeee" ] } }, - "Shutdown": { - - } + "Shutdown": { } } diff --git a/NetworkResurrector.Application/DependencyInjectionExtensions.cs b/NetworkResurrector.Application/DependencyInjectionExtensions.cs index eb31137..5b63694 100644 --- a/NetworkResurrector.Application/DependencyInjectionExtensions.cs +++ b/NetworkResurrector.Application/DependencyInjectionExtensions.cs @@ -10,7 +10,6 @@ namespace NetworkResurrector.Application public static void AddApplicationServices(this IServiceCollection services) { services.AddSingleton(); - services.AddScoped(); services.AddStores(); services.AddSingleton(); services.AddSingleton(); diff --git a/NetworkResurrector.Application/Mappings/MappingProfile.cs b/NetworkResurrector.Application/Mappings/MappingProfile.cs index 76b8990..78ed624 100644 --- a/NetworkResurrector.Application/Mappings/MappingProfile.cs +++ b/NetworkResurrector.Application/Mappings/MappingProfile.cs @@ -1,6 +1,4 @@ using AutoMapper; -using NetworkResurrector.Application.Queries; -using NetworkResurrector.Domain.Models; namespace NetworkResurrector.Application.Mappings { @@ -8,7 +6,7 @@ namespace NetworkResurrector.Application.Mappings { public MappingProfile() { - CreateMap(); + // Add mappings here } } } diff --git a/NetworkResurrector.Application/Queries/GetServiceVersion.cs b/NetworkResurrector.Application/Queries/GetServiceVersion.cs new file mode 100644 index 0000000..82a89e4 --- /dev/null +++ b/NetworkResurrector.Application/Queries/GetServiceVersion.cs @@ -0,0 +1,7 @@ +namespace NetworkResurrector.Application.Queries +{ + class GetServiceVersion + { + //TO DO + } +} diff --git a/NetworkResurrector.Application/Queries/GetToken.cs b/NetworkResurrector.Application/Queries/GetToken.cs deleted file mode 100644 index 7d535f8..0000000 --- a/NetworkResurrector.Application/Queries/GetToken.cs +++ /dev/null @@ -1,48 +0,0 @@ -using AutoMapper; -using MediatR; -using NDB.Application.DataContracts; -using NetworkResurrector.Application.Services; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace NetworkResurrector.Application.Queries -{ - public class GetToken - { - public class Query : Query - { - public string UserName { get; set; } - public string Password { get; set; } - public Query() { } - } - - public class Model - { - public string Token { get; set; } - public DateTime ValidUntil { get; set; } - } - - public class QueryHandler : IRequestHandler - { - private readonly IUserService _userService; - private readonly IMapper _mapper; - - public QueryHandler(IUserService userService, IMapper mapper) - { - _userService = userService; - _mapper = mapper; - } - - public async Task Handle(Query request, CancellationToken cancellationToken) - { - var securityToken = await _userService.Login(request.UserName, request.Password); - if (securityToken == null) - return null; - - var result = _mapper.Map(securityToken); - return result; - } - } - } -} diff --git a/NetworkResurrector.Application/Services/ParamProvider.cs b/NetworkResurrector.Application/Services/ParamProvider.cs index 9a48bff..fbae246 100644 --- a/NetworkResurrector.Application/Services/ParamProvider.cs +++ b/NetworkResurrector.Application/Services/ParamProvider.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Configuration; -using NetworkResurrector.Domain.Entities; using NetworkResurrector.Domain.Services; namespace NetworkResurrector.Application.Services @@ -13,6 +12,6 @@ namespace NetworkResurrector.Application.Services _configuration = configuration; } - public User[] Users => _configuration.GetSection("Users").Get(); + // public User[] Users => _configuration.GetSection("Users").Get(); } } diff --git a/NetworkResurrector.Application/Services/UserService.cs b/NetworkResurrector.Application/Services/UserService.cs deleted file mode 100644 index 20e69eb..0000000 --- a/NetworkResurrector.Application/Services/UserService.cs +++ /dev/null @@ -1,54 +0,0 @@ -using NetworkResurrector.Application.Stores; -using NetworkResurrector.Domain.Entities; -using NetworkResurrector.Domain.Models; -using NetworkResurrector.Domain.Services; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace NetworkResurrector.Application.Services -{ - public interface IUserService - { - Task Login(string userName, string password); - Task Authenticate(string token); - } - - public class UserService : IUserService - { - private readonly IParamProvider _paramProvider; - private readonly ISecurityStore _securityStore; - - public UserService(IParamProvider paramProvider, ISecurityStore securityStore) - { - _paramProvider = paramProvider; - _securityStore = securityStore; - } - - public async Task Login(string userName, string password) - { - var user = _paramProvider.Users.FirstOrDefault(z => z.UserName == userName && z.Password == password); - if (user == null) - return null; - - var token = $"{Guid.NewGuid()}-{Guid.NewGuid()}-{user.UserId}"; - await Task.Run(() =>_securityStore.SetToken(token, user.UserId)); - - var securityToken = new SecurityToken() { UserId = user.UserId, Token = token }; - return securityToken; - } - - public async Task Authenticate(string token) - { - var tokenValidation = await Task.Run(() => _securityStore.ValidateToken(token)); - if (tokenValidation.Success) - { - var user = _paramProvider.Users.FirstOrDefault(z => z.UserId == tokenValidation.UserId); - if (user != null) - return user; - } - - return null; - } - } -} diff --git a/NetworkResurrector.Domain/Entities/User.cs b/NetworkResurrector.Domain/Entities/User.cs deleted file mode 100644 index 355bd0f..0000000 --- a/NetworkResurrector.Domain/Entities/User.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace NetworkResurrector.Domain.Entities -{ - public class User - { - public int UserId { get; set; } - public string UserName { get; set; } - public string Password { get; set; } - } -} diff --git a/NetworkResurrector.Domain/Services/IParamProvider.cs b/NetworkResurrector.Domain/Services/IParamProvider.cs index 1aee4cf..2d54d69 100644 --- a/NetworkResurrector.Domain/Services/IParamProvider.cs +++ b/NetworkResurrector.Domain/Services/IParamProvider.cs @@ -1,9 +1,6 @@ -using NetworkResurrector.Domain.Entities; - -namespace NetworkResurrector.Domain.Services +namespace NetworkResurrector.Domain.Services { public interface IParamProvider { - User[] Users { get; } } }