-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from CUMGroup/login
LoginService and Controller
- Loading branch information
Showing
14 changed files
with
331 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace PWManager.Application.Services.Interfaces { | ||
public interface ILoginService { | ||
public void Login(string username, string password, string dbPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using PWManager.Application.Exceptions; | ||
|
||
namespace PWManager.CLI.Abstractions { | ||
public class ConfigFileHandler { | ||
public static string ReadDefaultFile() { | ||
var defaultFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); | ||
|
||
try { | ||
return File.ReadAllText(Path.Combine(defaultFilePath, "last.txt")); | ||
} catch (IOException e) { | ||
throw new UserFeedbackException("The file could not be read"); | ||
} | ||
} | ||
public static void WriteDefaultFile(string username, string path) { | ||
var defaultFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); | ||
|
||
try { | ||
File.WriteAllText(Path.Combine(defaultFilePath, "last.txt"), username + '\n' + path); | ||
} catch (IOException e) { | ||
throw new UserFeedbackException("The file could not be written"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using PWManager.Application.Context; | ||
using PWManager.Application.Exceptions; | ||
using PWManager.Application.Services.Interfaces; | ||
using PWManager.CLI.Abstractions; | ||
using PWManager.CLI.Enums; | ||
using PWManager.CLI.Interfaces; | ||
using Sharprompt; | ||
using System.IO; | ||
|
||
namespace PWManager.CLI.Controllers { | ||
public class LoginController : IController { | ||
private readonly IApplicationEnvironment _env; | ||
private readonly ILoginService _loginService; | ||
public LoginController(IApplicationEnvironment env, ILoginService loginService) { | ||
_env = env; | ||
_loginService = loginService; | ||
} | ||
public ExitCondition Handle(string[] args) { | ||
if (_env.RunningSession) { | ||
throw new UserFeedbackException("Command not available in a session!"); | ||
} | ||
|
||
(var username, var path) = ParseArgs(args); | ||
|
||
var lastUser = ConfigFileHandler.ReadDefaultFile(); | ||
if (String.IsNullOrWhiteSpace(username)) { | ||
username = lastUser.Split('\n')[0]; | ||
} | ||
if (String.IsNullOrWhiteSpace(path)) { | ||
path = lastUser.Split('\n')[1]; | ||
} | ||
|
||
var pass = Prompt.Password("Enter your password"); | ||
_loginService.Login(username, pass, path); | ||
|
||
ConfigFileHandler.WriteDefaultFile(username, path); | ||
Console.WriteLine($"Welcome {username} :)"); | ||
|
||
_env.RunningSession = true; | ||
return ExitCondition.CONTINUE; | ||
} | ||
|
||
public (string, string) ParseArgs(string[] args) { | ||
string username = ""; | ||
string path = ""; | ||
|
||
int basepointer = 0; | ||
while (basepointer < args.Length) { | ||
if ((args[basepointer].Equals("-u") || args[basepointer].Equals("--username"))) { | ||
if ((args.Length - basepointer <= 1) || args[basepointer + 1].StartsWith('-')) { | ||
username = AskForInput("Please enter your username"); | ||
} else { | ||
username = args[basepointer + 1]; | ||
basepointer++; | ||
} | ||
} else if ((args[basepointer].Equals("-d") || args[basepointer].Equals("--directory"))) { | ||
if ((args.Length - basepointer <= 1) || args[basepointer + 1].StartsWith('-')) { | ||
path = AskForInput("Please enter the location of your databasefile"); | ||
} else { | ||
path = args[basepointer + 1]; | ||
basepointer++; | ||
} | ||
} | ||
basepointer++; | ||
} | ||
|
||
return (username, path); | ||
} | ||
|
||
public virtual string AskForInput(string prompt) { | ||
return Prompt.Input<string>(prompt); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using PWManager.Application.Context; | ||
using PWManager.Application.Exceptions; | ||
using PWManager.Application.Services.Interfaces; | ||
using PWManager.Data.Abstraction; | ||
using PWManager.Domain.Exceptions; | ||
using PWManager.Domain.Repositories; | ||
using PWManager.Domain.Services.Interfaces; | ||
|
||
namespace PWManager.Data.Services { | ||
public class LoginService : ILoginService { | ||
|
||
private readonly IUserRepository _userRepository; | ||
private readonly IGroupRepository _groupRepository; | ||
private readonly ISettingsRepository _settingsRepository; | ||
private readonly ICryptService _cryptService; | ||
private readonly IApplicationEnvironment _env; | ||
|
||
private readonly DataContextWrapper _dataContext; | ||
|
||
internal LoginService(DataContextWrapper wrapper, IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IApplicationEnvironment env) : this( | ||
userRepository, groupRepository, cryptService, settingsRepository, env) { | ||
_dataContext = wrapper; | ||
} | ||
public LoginService(IUserRepository userRepository, IGroupRepository groupRepository, ICryptService cryptService, ISettingsRepository settingsRepository, IApplicationEnvironment env) { | ||
_userRepository = userRepository; | ||
_groupRepository = groupRepository; | ||
_settingsRepository = settingsRepository; | ||
_cryptService = cryptService; | ||
_env = env; | ||
_dataContext = new DataContextWrapper(); | ||
} | ||
public void Login(string username, string password, string dbPath) { | ||
if(!_dataContext.DatabaseExists(dbPath)) { | ||
throw new UserFeedbackException("Database not found."); | ||
} | ||
|
||
_dataContext.InitDataContext(dbPath); | ||
|
||
var user = _userRepository.CheckPasswordAttempt(username, password); | ||
if(user is null) { | ||
throw new UserFeedbackException("No such user found."); | ||
} | ||
|
||
_env.CurrentUser = user; | ||
_env.EncryptionKey = _cryptService.DeriveKeyFrom(password, username); | ||
|
||
var mainGroup = _settingsRepository.GetSettings().MainGroup; | ||
_env.CurrentGroup = _groupRepository.GetGroup(mainGroup.MainGroupIdentifier); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using NSubstitute; | ||
using PWManager.Application.Context; | ||
using PWManager.Application.Services.Interfaces; | ||
using PWManager.CLI.Controllers; | ||
using PWManager.Data.Repositories; | ||
using PWManager.Data.Services; | ||
using PWManager.Domain.Entities; | ||
using PWManager.Domain.Repositories; | ||
using PWManager.Domain.ValueObjects; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PWManager.UnitTests.Cli { | ||
public class LoginControllerTest { | ||
|
||
private LoginController _sut; | ||
|
||
public LoginControllerTest() | ||
{ | ||
_sut = Substitute.ForPartsOf<LoginController>(null, null); | ||
_sut.AskForInput(Arg.Any<string>()).Returns("Test"); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_ShouldBe_Empty_WithoutArgs() { | ||
string[] args = { }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal(0, username.Length); | ||
Assert.Equal(0, path.Length); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Path() { | ||
string[] args = { "-d", "TestPath" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal(0, username.Length); | ||
Assert.Equal("TestPath", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Username() { | ||
string[] args = { "-u", "TestUserName" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("TestUserName", username); | ||
Assert.Equal(0, path.Length); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Arguments_Should_Return_Path_And_PromptName() { | ||
string[] args = { "-d", "TestPath", "-u" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("Test", username); | ||
Assert.Equal("TestPath", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_Username_And_PromptPath() { | ||
string[] args = { "-d", "-u", "TestUserName" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("TestUserName", username); | ||
Assert.Equal("Test", path); | ||
} | ||
|
||
[Fact] | ||
public void Arguments_Should_Return_From_Prompts() { | ||
string[] args = { "-d", "-u" }; | ||
|
||
(var username, var path) = _sut.ParseArgs(args); | ||
|
||
Assert.Equal("Test", username); | ||
Assert.Equal("Test", path); | ||
} | ||
} | ||
} |
Oops, something went wrong.