-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't need to configure jwt secret key
- Loading branch information
Showing
7 changed files
with
23 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,19 @@ | ||
using Chats.BE.DB; | ||
using Chats.BE.Services.Configs; | ||
using Microsoft.EntityFrameworkCore; | ||
namespace Chats.BE.Services.Sessions; | ||
|
||
namespace Chats.BE.Services.Sessions; | ||
|
||
public class JwtKeyManager(ChatsDB db) | ||
public class JwtKeyManager | ||
{ | ||
public async Task<string> GetOrCreateSecretKey(CancellationToken cancellationToken) | ||
public string GetOrCreateSecretKey() | ||
{ | ||
// check environment variable first | ||
// if it's not set, then generate a new one and store into database | ||
string? secretKey = Environment.GetEnvironmentVariable("JWT_SECRET_KEY"); | ||
if (secretKey != null) return secretKey; | ||
|
||
string? configText = await db.Configs | ||
.Where(s => s.Key == DBConfigKey.JwtSecretKey) | ||
.Select(x => x.Value) | ||
.SingleOrDefaultAsync(cancellationToken); | ||
if (configText != null) return configText; | ||
|
||
string generated = Guid.NewGuid().ToString(); | ||
db.Configs.Add(new Config { Key = DBConfigKey.JwtSecretKey, Value = generated, Description = $"Generated at {DateTime.Now:O}" }); | ||
await db.SaveChangesAsync(cancellationToken); | ||
|
||
return generated; | ||
if (secretKey != null) | ||
{ | ||
return secretKey; | ||
} | ||
else | ||
{ | ||
string generated = Guid.NewGuid().ToString(); | ||
Environment.SetEnvironmentVariable("JWT_SECRET_KEY", generated); | ||
return generated; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters