Added ip locate. Fixed oauth2 mail send. Fixed mail send when generating tokens

This commit is contained in:
Marcel Baumgartner
2023-04-12 17:06:29 +02:00
parent fdacc60ad4
commit ba373b86ee
2 changed files with 23 additions and 10 deletions

View File

@@ -58,7 +58,7 @@ public class OAuth2Controller : Controller
} }
else else
{ {
token = await UserService.GenerateToken(user); token = await UserService.GenerateToken(user, true);
} }
Response.Cookies.Append("token", token, new () Response.Cookies.Append("token", token, new ()
@@ -116,7 +116,7 @@ public class OAuth2Controller : Controller
} }
else else
{ {
token = await UserService.GenerateToken(user); token = await UserService.GenerateToken(user, true);
} }
Response.Cookies.Append("token", token, new () Response.Cookies.Append("token", token, new ()

View File

@@ -18,6 +18,7 @@ public class UserService
private readonly AuditLogService AuditLogService; private readonly AuditLogService AuditLogService;
private readonly MailService MailService; private readonly MailService MailService;
private readonly IdentityService IdentityService; private readonly IdentityService IdentityService;
private readonly IpLocateService IpLocateService;
private readonly string JwtSecret; private readonly string JwtSecret;
@@ -28,7 +29,7 @@ public class UserService
SecurityLogService securityLogService, SecurityLogService securityLogService,
AuditLogService auditLogService, AuditLogService auditLogService,
MailService mailService, MailService mailService,
IdentityService identityService) IdentityService identityService, IpLocateService ipLocateService)
{ {
UserRepository = userRepository; UserRepository = userRepository;
TotpService = totpService; TotpService = totpService;
@@ -36,6 +37,7 @@ public class UserService
AuditLogService = auditLogService; AuditLogService = auditLogService;
MailService = mailService; MailService = mailService;
IdentityService = identityService; IdentityService = identityService;
IpLocateService = ipLocateService;
JwtSecret = configService JwtSecret = configService
.GetSection("Moonlight") .GetSection("Moonlight")
@@ -77,6 +79,7 @@ public class UserService
}); });
await MailService.SendMail(user!, "register", values => {}); await MailService.SendMail(user!, "register", values => {});
await AuditLogService.Log(AuditLogType.Register, x => await AuditLogService.Log(AuditLogType.Register, x =>
{ {
x.Add<User>(user.Email); x.Add<User>(user.Email);
@@ -177,11 +180,13 @@ public class UserService
} }
else else
{ {
var location = await IpLocateService.GetLocation();
await MailService.SendMail(user!, "passwordChange", values => await MailService.SendMail(user!, "passwordChange", values =>
{ {
values.Add("Ip", IdentityService.GetIp()); values.Add("Ip", IdentityService.GetIp());
values.Add("Device", IdentityService.GetDevice()); values.Add("Device", IdentityService.GetDevice());
values.Add("Location", "In your walls"); values.Add("Location", location);
}); });
await AuditLogService.Log(AuditLogType.ChangePassword, x => await AuditLogService.Log(AuditLogType.ChangePassword, x =>
@@ -201,6 +206,7 @@ public class UserService
{ {
x.Add<int>(id); x.Add<int>(id);
}); });
throw new Exception("Invalid username"); throw new Exception("Invalid username");
} }
@@ -223,12 +229,17 @@ public class UserService
public async Task<string> GenerateToken(User user, bool sendMail = false) public async Task<string> GenerateToken(User user, bool sendMail = false)
{ {
await MailService.SendMail(user!, "login", values => var location = await IpLocateService.GetLocation();
if (sendMail)
{ {
values.Add("Ip", IdentityService.GetIp()); await MailService.SendMail(user!, "login", values =>
values.Add("Device", IdentityService.GetDevice()); {
values.Add("Location", "In your walls"); values.Add("Ip", IdentityService.GetIp());
}); values.Add("Device", IdentityService.GetDevice());
values.Add("Location", location);
});
}
var token = JwtBuilder.Create() var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm()) .WithAlgorithm(new HMACSHA256Algorithm())
@@ -257,11 +268,13 @@ public class UserService
await AuditLogService.Log(AuditLogType.PasswordReset, x => {}); await AuditLogService.Log(AuditLogType.PasswordReset, x => {});
var location = await IpLocateService.GetLocation();
await MailService.SendMail(user, "passwordReset", values => await MailService.SendMail(user, "passwordReset", values =>
{ {
values.Add("Ip", IdentityService.GetIp()); values.Add("Ip", IdentityService.GetIp());
values.Add("Device", IdentityService.GetDevice()); values.Add("Device", IdentityService.GetDevice());
values.Add("Location", "In your walls"); values.Add("Location", location);
values.Add("Password", newPassword); values.Add("Password", newPassword);
}); });
} }