-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleApp.cs
106 lines (98 loc) · 2.45 KB
/
ConsoleApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using authenticator.Configuration;
using authenticator.Logging;
using authenticator.Translation;
using authenticator.Repositories;
using authenticator.Models;
using Microsoft.AspNetCore.Identity;
namespace authenticator.ConsoleApp
{
public class App
{
private Config config;
private Translator translator;
private Logger logger;
private UserRepository user_repository;
private PasswordHasher<string> hasher;
private User? user_logged;
public App(Config config,
Translator translator,
Logger logger,
UserRepository user_repository,
PasswordHasher<string> hasher)
{
this.config = config;
this.translator = translator;
this.logger = logger;
this.user_repository = user_repository;
this.hasher = hasher;
}
public void run()
{
bool run = true;
user_logged = null;
while (run)
{
if(user_logged == null)
{
run = Unauthenticated();
}
else
{
run = Authentified();
}
}
}
private bool Unauthenticated()
{
string? option;
Console.WriteLine(translator.Get("UNAUTHENTICATED"));
option = Console.ReadLine();
switch (option)
{
case "1":
Console.WriteLine(translator.Get("INSERT_EMAIL"));
string? email = Console.ReadLine();
Console.WriteLine(translator.Get("INSERT_PASSWORD"));
string? password = Console.ReadLine();
if (email != null && password != null)
{
User? user = user_repository!.GetByEmail(email);
if (user != null && hasher.VerifyHashedPassword(user.email, user.password, password) == PasswordVerificationResult.Success)
{
user_logged = user;
Console.WriteLine(translator.Get("ACCESS_GRANTED"));
logger.info(string.Format("login user: {0} name: {1}", user.userId, user.name));
break;
}
}
Console.WriteLine(translator.Get("ACCESS_DENIED"));
break;
case "2":
return false;
default:
Console.WriteLine(translator.Get("UNVALID"));
break;
}
return true;
}
private bool Authentified()
{
string? option;
Console.WriteLine(translator.Get("AUTHENTICATED"));
option = Console.ReadLine();
switch (option)
{
case "1":
logger.info(string.Format("logout user: {0} name: {1}", user_logged!.userId, user_logged.name));
user_logged = null;
break;
case "2":
return false;
default:
Console.WriteLine(translator.Get("UNVALID"));
break;
}
return true;
}
}
}