-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DuendeSoftware/features/ef_key_mgmt
Add EF support for key mgmt
- Loading branch information
Showing
17 changed files
with
278 additions
and
15 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
2 changes: 1 addition & 1 deletion
2
.../20201012150100_Configuration.Designer.cs → .../20201123190954_Configuration.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
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
33 changes: 32 additions & 1 deletion
33
...GrantDb/20201012150055_Grants.Designer.cs → ...GrantDb/20201123190949_Grants.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
#pragma warning disable 1591 | ||
|
||
using System; | ||
|
||
namespace Duende.IdentityServer.EntityFramework.Entities | ||
{ | ||
/// <summary> | ||
/// Models storage for keys. | ||
/// </summary> | ||
public class Key | ||
{ | ||
public string Id { get; set; } | ||
public int Version { get; set; } | ||
public DateTime Created { get; set; } | ||
public string Algorithm { get; set; } | ||
public bool IsX509Certificate { get; set; } | ||
public bool DataProtected { get; set; } | ||
public string Data { get; set; } | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/EntityFramework.Storage/src/Stores/SigningKeyStore.cs
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,110 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Duende.IdentityServer.EntityFramework.DbContexts; | ||
using Duende.IdentityServer.EntityFramework.Entities; | ||
using Duende.IdentityServer.Models; | ||
using Duende.IdentityServer.Stores; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Duende.IdentityServer.EntityFramework.Stores | ||
{ | ||
/// <summary> | ||
/// Implementation of ISigningKeyStore thats uses EF. | ||
/// </summary> | ||
/// <seealso cref="ISigningKeyStore" /> | ||
public class SigningKeyStore : ISigningKeyStore | ||
{ | ||
/// <summary> | ||
/// The DbContext. | ||
/// </summary> | ||
protected readonly PersistedGrantDbContext Context; | ||
|
||
/// <summary> | ||
/// The logger. | ||
/// </summary> | ||
protected readonly ILogger<SigningKeyStore> Logger; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SigningKeyStore"/> class. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
/// <param name="logger">The logger.</param> | ||
/// <exception cref="ArgumentNullException">context</exception> | ||
public SigningKeyStore(PersistedGrantDbContext context, ILogger<SigningKeyStore> logger) | ||
{ | ||
Context = context ?? throw new ArgumentNullException(nameof(context)); | ||
Logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Loads all keys from store. | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<IEnumerable<SerializedKey>> LoadKeysAsync() | ||
{ | ||
var entities = await Context.Keys.ToArrayAsync(); | ||
return entities.Select(key => new SerializedKey | ||
{ | ||
Id = key.Id, | ||
Created = key.Created, | ||
Version = key.Version, | ||
Algorithm = key.Algorithm, | ||
Data = key.Data, | ||
DataProtected = key.DataProtected, | ||
IsX509Certificate = key.IsX509Certificate | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Persists new key in store. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <returns></returns> | ||
public Task StoreKeyAsync(SerializedKey key) | ||
{ | ||
var entity = new Key | ||
{ | ||
Id = key.Id, | ||
Created = key.Created, | ||
Version = key.Version, | ||
Algorithm = key.Algorithm, | ||
Data = key.Data, | ||
DataProtected = key.DataProtected, | ||
IsX509Certificate = key.IsX509Certificate | ||
}; | ||
Context.Keys.Add(entity); | ||
return Context.SaveChangesAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes key from storage. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
public async Task DeleteKeyAsync(string id) | ||
{ | ||
var item = await Context.Keys.FirstOrDefaultAsync(x => x.Id == id); | ||
if (item != null) | ||
{ | ||
try | ||
{ | ||
Context.Keys.Remove(item); | ||
await Context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
Context.Entry(item).State = EntityState.Detached; | ||
// already deleted, so we can eat this exception | ||
Logger.LogDebug("Concurrency exception caught deleting key id {kid}", id); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.