aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-12 16:24:17 +0800
committer杨宇千 <crupest@outlook.com>2019-08-12 16:24:17 +0800
commitafaad69437dc12ffb706667eb63875b116631234 (patch)
treeaf074659bc9490457f1627c520c1774895a3975f /Timeline/Services
parent4367f283f196d8b6f195855b4592bf65aa10859e (diff)
downloadtimeline-afaad69437dc12ffb706667eb63875b116631234.tar.gz
timeline-afaad69437dc12ffb706667eb63875b116631234.tar.bz2
timeline-afaad69437dc12ffb706667eb63875b116631234.zip
Add username format check.
Diffstat (limited to 'Timeline/Services')
-rw-r--r--Timeline/Services/UserService.cs9
1 files changed, 9 insertions, 0 deletions
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index 50aa4187..0993d3dc 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -278,6 +278,8 @@ namespace Timeline.Services
private readonly IJwtService _jwtService;
private readonly IPasswordService _passwordService;
+ private readonly UsernameValidator _usernameValidator;
+
public UserService(ILogger<UserService> logger, IMemoryCache memoryCache, DatabaseContext databaseContext, IJwtService jwtService, IPasswordService passwordService)
{
_logger = logger;
@@ -285,6 +287,8 @@ namespace Timeline.Services
_databaseContext = databaseContext;
_jwtService = jwtService;
_passwordService = passwordService;
+
+ _usernameValidator = new UsernameValidator();
}
private string GenerateCacheKeyByUserId(long id) => $"user:{id}";
@@ -377,6 +381,11 @@ namespace Timeline.Services
if (password == null)
throw new ArgumentNullException(nameof(password));
+ if (!_usernameValidator.Validate(username, out var message))
+ {
+ throw new UsernameBadFormatException(username, message);
+ }
+
var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync();
if (user == null)