From f17bb6f151dd7a6198d69ce0468b3ffb11454c9d Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 24 Dec 2020 04:39:59 +0200 Subject: [PATCH] authentication result update --- .../CommandHandlers/AuthenticateUserHandler.cs | 11 +++++++---- .../Commands/AuthenticateUser.cs | 4 ++-- .../Constants/AuthenticationStatus.cs | 9 +++++++++ .../Events/AuthenticateUserResult.cs | 10 ++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 IdentityServer.PublishedLanguage/Constants/AuthenticationStatus.cs create mode 100644 IdentityServer.PublishedLanguage/Events/AuthenticateUserResult.cs diff --git a/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs b/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs index d377094..4419192 100644 --- a/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs +++ b/IdentityServer.Application/CommandHandlers/AuthenticateUserHandler.cs @@ -1,7 +1,9 @@ using AutoMapper; using IdentityServer.Application.Commands; using IdentityServer.Application.Services; +using IdentityServer.PublishedLanguage.Constants; using IdentityServer.PublishedLanguage.Dto; +using IdentityServer.PublishedLanguage.Events; using MediatR; using Microsoft.Extensions.Logging; using System.Threading; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace IdentityServer.Application.CommandHandlers { - public class AuthenticateUserHandler : IRequestHandler + public class AuthenticateUserHandler : IRequestHandler { private readonly IUserService _userService; private readonly IMapper _mapper; @@ -22,18 +24,19 @@ namespace IdentityServer.Application.CommandHandlers _logger = logger; } - public async Task Handle(AuthenticateUser command, CancellationToken cancellationToken) + public async Task Handle(AuthenticateUser command, CancellationToken cancellationToken) { var internalToken = await _userService.Authenticate(command.UserName, command.Password); if (internalToken == null) { _logger.LogDebug($"Authentication failed for user '{command.UserName}'."); - return null; + return new AuthenticateUserResult() { Status = AuthenticationStatus.BAD_CREDENTIALS }; } _logger.LogDebug($"Authentication succeeded for user '{command.UserName}'."); var token = _mapper.Map(internalToken); - return token; + + return new AuthenticateUserResult() { Token = token, Status = AuthenticationStatus.SUCCESS }; } } } diff --git a/IdentityServer.Application/Commands/AuthenticateUser.cs b/IdentityServer.Application/Commands/AuthenticateUser.cs index 1cfd19c..f433c98 100644 --- a/IdentityServer.Application/Commands/AuthenticateUser.cs +++ b/IdentityServer.Application/Commands/AuthenticateUser.cs @@ -1,9 +1,9 @@ -using IdentityServer.PublishedLanguage.Dto; +using IdentityServer.PublishedLanguage.Events; using NDB.Application.DataContracts; namespace IdentityServer.Application.Commands { - public class AuthenticateUser : Command + public class AuthenticateUser : Command { public string UserName { get; set; } public string Password { get; set; } diff --git a/IdentityServer.PublishedLanguage/Constants/AuthenticationStatus.cs b/IdentityServer.PublishedLanguage/Constants/AuthenticationStatus.cs new file mode 100644 index 0000000..29da4fa --- /dev/null +++ b/IdentityServer.PublishedLanguage/Constants/AuthenticationStatus.cs @@ -0,0 +1,9 @@ +namespace IdentityServer.PublishedLanguage.Constants +{ + public struct AuthenticationStatus + { + public const string + SUCCESS = "SUCCESS", + BAD_CREDENTIALS = "BAD_CREDENTIALS"; + } +} diff --git a/IdentityServer.PublishedLanguage/Events/AuthenticateUserResult.cs b/IdentityServer.PublishedLanguage/Events/AuthenticateUserResult.cs new file mode 100644 index 0000000..9ebd117 --- /dev/null +++ b/IdentityServer.PublishedLanguage/Events/AuthenticateUserResult.cs @@ -0,0 +1,10 @@ +using IdentityServer.PublishedLanguage.Dto; + +namespace IdentityServer.PublishedLanguage.Events +{ + public class AuthenticateUserResult + { + public Token Token { get; set; } + public string Status { get; set; } + } +} \ No newline at end of file