From ba373b86ee4a0987665f377f8be661f8bc54c899 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Apr 2023 17:06:29 +0200 Subject: [PATCH] Added ip locate. Fixed oauth2 mail send. Fixed mail send when generating tokens --- .../Api/Moonlight/OAuth2Controller.cs | 4 +-- Moonlight/App/Services/UserService.cs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs index 7452ce49..3d1abb4c 100644 --- a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs @@ -58,7 +58,7 @@ public class OAuth2Controller : Controller } else { - token = await UserService.GenerateToken(user); + token = await UserService.GenerateToken(user, true); } Response.Cookies.Append("token", token, new () @@ -116,7 +116,7 @@ public class OAuth2Controller : Controller } else { - token = await UserService.GenerateToken(user); + token = await UserService.GenerateToken(user, true); } Response.Cookies.Append("token", token, new () diff --git a/Moonlight/App/Services/UserService.cs b/Moonlight/App/Services/UserService.cs index 9cf6aa77..6a6fe022 100644 --- a/Moonlight/App/Services/UserService.cs +++ b/Moonlight/App/Services/UserService.cs @@ -18,6 +18,7 @@ public class UserService private readonly AuditLogService AuditLogService; private readonly MailService MailService; private readonly IdentityService IdentityService; + private readonly IpLocateService IpLocateService; private readonly string JwtSecret; @@ -28,7 +29,7 @@ public class UserService SecurityLogService securityLogService, AuditLogService auditLogService, MailService mailService, - IdentityService identityService) + IdentityService identityService, IpLocateService ipLocateService) { UserRepository = userRepository; TotpService = totpService; @@ -36,6 +37,7 @@ public class UserService AuditLogService = auditLogService; MailService = mailService; IdentityService = identityService; + IpLocateService = ipLocateService; JwtSecret = configService .GetSection("Moonlight") @@ -77,6 +79,7 @@ public class UserService }); await MailService.SendMail(user!, "register", values => {}); + await AuditLogService.Log(AuditLogType.Register, x => { x.Add(user.Email); @@ -177,11 +180,13 @@ public class UserService } else { + var location = await IpLocateService.GetLocation(); + await MailService.SendMail(user!, "passwordChange", values => { values.Add("Ip", IdentityService.GetIp()); values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); + values.Add("Location", location); }); await AuditLogService.Log(AuditLogType.ChangePassword, x => @@ -201,6 +206,7 @@ public class UserService { x.Add(id); }); + throw new Exception("Invalid username"); } @@ -223,12 +229,17 @@ public class UserService public async Task GenerateToken(User user, bool sendMail = false) { - await MailService.SendMail(user!, "login", values => + var location = await IpLocateService.GetLocation(); + + if (sendMail) { - values.Add("Ip", IdentityService.GetIp()); - values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); - }); + await MailService.SendMail(user!, "login", values => + { + values.Add("Ip", IdentityService.GetIp()); + values.Add("Device", IdentityService.GetDevice()); + values.Add("Location", location); + }); + } var token = JwtBuilder.Create() .WithAlgorithm(new HMACSHA256Algorithm()) @@ -257,11 +268,13 @@ public class UserService await AuditLogService.Log(AuditLogType.PasswordReset, x => {}); + var location = await IpLocateService.GetLocation(); + await MailService.SendMail(user, "passwordReset", values => { values.Add("Ip", IdentityService.GetIp()); values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); + values.Add("Location", location); values.Add("Password", newPassword); }); }