Browse Source

Complete the DeviceFlowStore

pull/2545/head
Halil İbrahim Kalkan 7 years ago
parent
commit
5ec0362dd1
  1. 14
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs
  2. 84
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs
  3. 5
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs
  4. 2
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs
  5. 9
      modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs
  6. 7
      modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs

14
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs

@ -1,11 +1,10 @@
using System.Security.Cryptography.X509Certificates;
using IdentityServer4.Services;
using IdentityServer4.Services;
using IdentityServer4.Stores;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.AutoMapper;
using Volo.Abp.Caching;
using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.Modularity;
using Volo.Abp.Security;
using Volo.Abp.Validation;
@ -33,7 +32,7 @@ namespace Volo.Abp.IdentityServer
AddIdentityServer(context.Services);
}
private static void AddIdentityServer(IServiceCollection services)
{
var configuration = services.GetConfiguration();
@ -58,7 +57,12 @@ namespace Volo.Abp.IdentityServer
if (!services.IsAdded<IPersistedGrantService>())
{
identityServerBuilder.AddInMemoryPersistedGrants();
services.TryAddSingleton<IPersistedGrantStore, InMemoryPersistedGrantStore>();
}
if (!services.IsAdded<IDeviceFlowStore>())
{
services.TryAddSingleton<IDeviceFlowStore, InMemoryDeviceFlowStore>();
}
if (!services.IsAdded<IClientStore>())

84
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs

@ -33,25 +33,29 @@ namespace Volo.Abp.IdentityServer.Devices
Check.NotNull(userCode, nameof(userCode));
Check.NotNull(data, nameof(data));
await DeviceFlowCodesRepository.InsertAsync(
new DeviceFlowCodes(GuidGenerator.Create())
{
DeviceCode = deviceCode,
UserCode = userCode,
ClientId = data.ClientId,
SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
CreationTime = data.CreationTime,
Expiration = data.CreationTime.AddSeconds(data.Lifetime),
Data = Serialize(data)
}
).ConfigureAwait(false);
await DeviceFlowCodesRepository
.InsertAsync(
new DeviceFlowCodes(GuidGenerator.Create())
{
DeviceCode = deviceCode,
UserCode = userCode,
ClientId = data.ClientId,
SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
CreationTime = data.CreationTime,
Expiration = data.CreationTime.AddSeconds(data.Lifetime),
Data = Serialize(data)
}
).ConfigureAwait(false);
}
public async Task<DeviceCode> FindByUserCodeAsync(string userCode)
{
Check.NotNull(userCode, nameof(userCode));
var deviceCodes = await DeviceFlowCodesRepository.FindByUserCodeAsync(userCode).ConfigureAwait(false);
var deviceCodes = await DeviceFlowCodesRepository
.FindByUserCodeAsync(userCode)
.ConfigureAwait(false);
if (deviceCodes == null)
{
return null;
@ -60,19 +64,61 @@ namespace Volo.Abp.IdentityServer.Devices
return DeserializeToDeviceCode(deviceCodes.Data);
}
public Task<DeviceCode> FindByDeviceCodeAsync(string deviceCode)
public async Task<DeviceCode> FindByDeviceCodeAsync(string deviceCode)
{
throw new NotImplementedException();
Check.NotNull(deviceCode, nameof(deviceCode));
var deviceCodes = await DeviceFlowCodesRepository
.FindByDeviceCodeAsync(deviceCode)
.ConfigureAwait(false);
if (deviceCodes == null)
{
return null;
}
return DeserializeToDeviceCode(deviceCodes.Data);
}
public Task UpdateByUserCodeAsync(string userCode, DeviceCode data)
public async Task UpdateByUserCodeAsync(string userCode, DeviceCode data)
{
throw new NotImplementedException();
Check.NotNull(userCode, nameof(userCode));
Check.NotNull(data, nameof(data));
var deviceCodes = await DeviceFlowCodesRepository
.FindByUserCodeAsync(userCode)
.ConfigureAwait(false);
if (deviceCodes == null)
{
throw new InvalidOperationException($"Could not update device code by the given userCode: {userCode}");
}
deviceCodes.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value;
deviceCodes.Data = Serialize(data);
await DeviceFlowCodesRepository
.UpdateAsync(deviceCodes, autoSave: true)
.ConfigureAwait(false);
}
public Task RemoveByDeviceCodeAsync(string deviceCode)
public async Task RemoveByDeviceCodeAsync(string deviceCode)
{
throw new NotImplementedException();
Check.NotNull(deviceCode, nameof(deviceCode));
var deviceCodes = await DeviceFlowCodesRepository
.FindByDeviceCodeAsync(deviceCode)
.ConfigureAwait(false);
if (deviceCodes == null)
{
return;
}
await DeviceFlowCodesRepository
.DeleteAsync(deviceCodes, autoSave: true)
.ConfigureAwait(false);
}
private string Serialize([CanBeNull] DeviceCode deviceCode)

5
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs

@ -11,5 +11,10 @@ namespace Volo.Abp.IdentityServer.Devices
string userCode,
CancellationToken cancellationToken = default
);
Task<DeviceFlowCodes> FindByDeviceCodeAsync(
string deviceCode,
CancellationToken cancellationToken = default
);
}
}

2
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs

@ -1,6 +1,7 @@
using IdentityServer4.Stores;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants;
namespace Volo.Abp.IdentityServer
@ -10,6 +11,7 @@ namespace Volo.Abp.IdentityServer
public static IIdentityServerBuilder AddAbpStores(this IIdentityServerBuilder builder)
{
builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
builder.Services.AddTransient<IDeviceFlowStore, DeviceFlowStore>();
return builder
.AddClientStore<ClientStore>()

9
modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs

@ -25,5 +25,14 @@ namespace Volo.Abp.IdentityServer.Devices
.FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
public async Task<DeviceFlowCodes> FindByDeviceCodeAsync(
string deviceCode,
CancellationToken cancellationToken = default)
{
return await DbSet
.FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
}
}

7
modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs

@ -25,5 +25,12 @@ namespace Volo.Abp.IdentityServer.MongoDB
.FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
public async Task<DeviceFlowCodes> FindByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
}
}
Loading…
Cancel
Save