-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes several issues related to the v2.4.0 release.
- Loading branch information
1 parent
af0794f
commit 891c0f0
Showing
9 changed files
with
141 additions
and
19 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
2 changes: 1 addition & 1 deletion
2
...ner.StorageProvider.EntityFrameworkCore/Binner.StorageProvider.EntityFrameworkCore.csproj
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Binner.Global.Common; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace Binner.Common.Auth | ||
{ | ||
public class SecurityKeyProvider | ||
{ | ||
public enum KeyTypes | ||
{ | ||
Jwt | ||
} | ||
|
||
private string GetTokenFilename(KeyTypes keyType) | ||
{ | ||
var keyTypeName = ""; | ||
switch (keyType) | ||
{ | ||
case KeyTypes.Jwt: | ||
keyTypeName = "jwt.key"; | ||
break; | ||
default: | ||
throw new NotImplementedException($"Invalid key type: '{keyType}'"); | ||
} | ||
var assembly = Assembly.GetEntryAssembly(); | ||
if (assembly != null) | ||
{ | ||
var path = Path.GetDirectoryName(assembly.Location); | ||
if (string.IsNullOrEmpty(path)) | ||
throw new BinnerConfigurationException("Failed to get assembly path!"); | ||
var filename = Path.Combine(path, keyTypeName); | ||
return filename; | ||
} | ||
throw new BinnerConfigurationException("Error: Could not get entry assembly information to store the JWT key!"); | ||
} | ||
|
||
/// <summary> | ||
/// Load the key from disk | ||
/// </summary> | ||
/// <param name="keyType"></param> | ||
/// <param name="length"></param> | ||
/// <returns>The key or null if it does not exist</returns> | ||
public string LoadOrGenerateKey(KeyTypes keyType, int length) | ||
{ | ||
var filename = GetTokenFilename(keyType); | ||
if (File.Exists(filename)) | ||
{ | ||
var key = File.ReadAllText(filename); | ||
return key; | ||
} | ||
|
||
return CreateKey(filename); | ||
} | ||
|
||
/// <summary> | ||
/// Create a new key and save to disk | ||
/// </summary> | ||
/// <param name="filename"></param> | ||
/// <returns>The new key</returns> | ||
public string CreateKey(string filename) | ||
{ | ||
var key = GenerateSecurityKey(); | ||
File.WriteAllText(filename, key); | ||
return key; | ||
} | ||
|
||
private string GenerateSecurityKey() => ConfirmationTokenGenerator.NewSecurityToken(40); | ||
} | ||
} |
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