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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.FSE.BL.CacheEntities;
using Tango.FSE.BL.Web;
using Tango.FSE.Web.Messages;
using Tango.MachineService.Gateway;
namespace Tango.FSE.BL.Services
{
/// <summary>
/// Represents authentication services used for authenticating FSE users.
/// </summary>
/// <seealso cref="Tango.FSE.BL.FSEServiceBase" />
public class AuthenticationService : FSEServiceBase
{
private const string LOGIN_RESPONSES_COLLECTION = "LoginResponses";
/// <summary>
/// Performs authentication using the specified <see cref="LoginRequest"/>.
/// </summary>
/// <param name="request">The request.</param>
/// <returns></returns>
public Task<LoginResponse> Authenticate(LoginRequest request)
{
LogManager.Log($"Authenticating user '{request.Email}'...");
return DataResolver<LoginResponse>.Builder.New()
.ConfigureCascade(DataResolverNode.Online, DataResolverNode.DiskCache)
.Online((context) =>
{
try
{
var response = WebClient.Login(request).Result;
LogManager.Log("Online authentication was successful. Caching login response...");
try
{
using (var cache = DiskCache.CreateContext())
{
cache.Database.GetCollection<CachedLoginResponse>(LOGIN_RESPONSES_COLLECTION).Upsert(new CachedLoginResponse()
{
Response = response,
Email = request.Email,
Password = request.Password,
EnvironmentId = Authentication.CurrentEnvironment.ID
});
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error caching online logging response.");
}
return response;
}
catch (Exception ex)
{
if (ex is AggregateException)
{
if ((ex as AggregateException).InnerExceptions.ToList().Exists(x => x.GetType() == typeof(AuthenticationException)))
{
//Delete cache and abort cascade if web request was successful but authentication failed.
LogManager.Log(ex, "Online request was successful but authentication failed. Removing cached response and aborting cascade.");
DropCachedResponse(request.Email);
context.AbortCascade();
}
}
throw ex;
}
})
.DiskCache((context) =>
{
try
{
using (var cache = DiskCache.CreateContext())
{
String currentEnvironmentID = Authentication.CurrentEnvironment.ID;
var cachedResponse = cache.Database.GetCollection<CachedLoginResponse>(LOGIN_RESPONSES_COLLECTION).FindOne(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password && x.EnvironmentId == currentEnvironmentID);
if (cachedResponse == null)
{
throw context.LastError;
}
cachedResponse.Response.PasswordChangeRequired = false;
return cachedResponse.Response;
}
}
catch (Exception ex)
{
throw new AuthenticationException("Could not login user online or via cache.", ex);
}
})
.BuildExecuteAsync();
}
/// <summary>
/// Changes the specified user password.
/// </summary>
/// <param name="email">The email.</param>
/// <param name="oldPassword">The old password.</param>
/// <param name="newPassword">The new password.</param>
/// <returns></returns>
public Task ChangePassword(String email, String oldPassword, String newPassword)
{
return Task.Factory.StartNew(() =>
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
var oldPasswordHash = User.GetPasswordHash(oldPassword);
var newPasswordHash = User.GetPasswordHash(newPassword);
var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == oldPasswordHash);
if (user == null)
{
throw new AuthenticationException("Current email and password do not match.");
}
user.Password = newPasswordHash;
user.PasswordChangeRequired = false;
db.SaveChanges();
}
});
}
/// <summary>
/// Sends the specified email a password reset information.
/// </summary>
/// <param name="email">The email.</param>
/// <returns></returns>
public Task SendForgotPasswordEmail(String email, EnvironmentConfiguration environment)
{
return Task.Factory.StartNew(() =>
{
FSEWebClient client = new FSEWebClient(environment.MachineServiceAddress);
var response = client.SendForgotPasswordEmail(new ForgotPasswordRequest()
{
Email = email,
MachineServiceAddress = environment.MachineServiceAddress,
Build = BuildProvider.Build
}).Result;
});
}
/// <summary>
/// Drops the cached login response for the specified user.
/// </summary>
/// <param name="email">The email.</param>
public void DropCachedResponse(String email)
{
LogManager.Log($"Dropping cached response for user '{email}'...");
try
{
using (var cache = DiskCache.CreateContext())
{
cache.Database.GetCollection<CachedLoginResponse>(LOGIN_RESPONSES_COLLECTION).DeleteMany(x => x.Email.ToLower() == email.ToLower());
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error dropping cached response.");
}
}
}
}
|