aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-02-21 15:10:37 +0800
committercrupest <crupest@outlook.com>2020-02-21 15:10:37 +0800
commitf389661667a510d6accfb412482578b66527e6e4 (patch)
tree57a8579bc8b6d2be2831c8e55d5b5b0f552def35 /Timeline/Services
parent3fa9899e17df4b1012e8b645915ac15022b84f9b (diff)
downloadtimeline-f389661667a510d6accfb412482578b66527e6e4.tar.gz
timeline-f389661667a510d6accfb412482578b66527e6e4.tar.bz2
timeline-f389661667a510d6accfb412482578b66527e6e4.zip
Move jwt token key from configuration to database and auto generatable.
Diffstat (limited to 'Timeline/Services')
-rw-r--r--Timeline/Services/PathProvider.cs41
-rw-r--r--Timeline/Services/UserTokenService.cs19
2 files changed, 52 insertions, 8 deletions
diff --git a/Timeline/Services/PathProvider.cs b/Timeline/Services/PathProvider.cs
new file mode 100644
index 00000000..15e66972
--- /dev/null
+++ b/Timeline/Services/PathProvider.cs
@@ -0,0 +1,41 @@
+using Microsoft.Extensions.Configuration;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Timeline.Services
+{
+ public interface IPathProvider
+ {
+ public string GetWorkingDirectory();
+ public string GetDatabaseFilePath();
+ }
+
+ public class PathProvider : IPathProvider
+ {
+ const string DatabaseFileName = "timeline.db";
+
+ private readonly IConfiguration _configuration;
+
+ private readonly string _workingDirectory;
+
+
+ public PathProvider(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ _workingDirectory = configuration.GetValue<string>("WorkDir");
+ }
+
+ public string GetWorkingDirectory()
+ {
+ return _workingDirectory;
+ }
+
+ public string GetDatabaseFilePath()
+ {
+ return Path.Combine(_workingDirectory, DatabaseFileName);
+ }
+ }
+}
diff --git a/Timeline/Services/UserTokenService.cs b/Timeline/Services/UserTokenService.cs
index 731eb1db..86f3a0f7 100644
--- a/Timeline/Services/UserTokenService.cs
+++ b/Timeline/Services/UserTokenService.cs
@@ -3,9 +3,10 @@ using Microsoft.IdentityModel.Tokens;
using System;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
+using System.Linq;
using System.Security.Claims;
-using System.Text;
using Timeline.Configs;
+using Timeline.Entities;
namespace Timeline.Services
{
@@ -49,16 +50,19 @@ namespace Timeline.Services
private readonly JwtSecurityTokenHandler _tokenHandler = new JwtSecurityTokenHandler();
private SymmetricSecurityKey _tokenSecurityKey;
- public JwtUserTokenService(IOptionsMonitor<JwtConfiguration> jwtConfig, IClock clock)
+ public JwtUserTokenService(IOptionsMonitor<JwtConfiguration> jwtConfig, IClock clock, DatabaseContext database)
{
_jwtConfig = jwtConfig;
_clock = clock;
- _tokenSecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.CurrentValue.SigningKey));
- jwtConfig.OnChange(config =>
+ var key = database.JwtToken.Select(t => t.Key).SingleOrDefault();
+
+ if (key == null)
{
- _tokenSecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SigningKey));
- });
+ throw new InvalidOperationException(Resources.Services.UserTokenService.JwtKeyNotExist);
+ }
+
+ _tokenSecurityKey = new SymmetricSecurityKey(key);
}
public string GenerateToken(UserTokenInfo tokenInfo)
@@ -77,8 +81,7 @@ namespace Timeline.Services
Subject = identity,
Issuer = config.Issuer,
Audience = config.Audience,
- SigningCredentials = new SigningCredentials(
- new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SigningKey)), SecurityAlgorithms.HmacSha384),
+ SigningCredentials = new SigningCredentials(_tokenSecurityKey, SecurityAlgorithms.HmacSha384),
IssuedAt = _clock.GetCurrentTime(),
Expires = tokenInfo.ExpireAt.GetValueOrDefault(_clock.GetCurrentTime().AddSeconds(config.DefaultExpireOffset)),
NotBefore = _clock.GetCurrentTime() // I must explicitly set this or it will use the current time by default and mock is not work in which case test will not pass.