From 5ec0362dd15c0d526167ded7ce2616c6ad6b7a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 2 Jan 2020 20:28:47 +0300 Subject: [PATCH] Complete the DeviceFlowStore --- .../AbpIdentityServerDomainModule.cs | 14 ++-- .../IdentityServer/Devices/DeviceFlowStore.cs | 84 ++++++++++++++----- .../Devices/IDeviceFlowCodesRepository.cs | 5 ++ .../IdentityServerBuilderExtensions.cs | 2 + .../Devices/DeviceFlowCodesRepository.cs | 9 ++ .../MongoDB/MongoDeviceFlowCodesRepository.cs | 7 ++ 6 files changed, 97 insertions(+), 24 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index f6f811ad96..7c22dccf0e 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/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()) { - identityServerBuilder.AddInMemoryPersistedGrants(); + services.TryAddSingleton(); + } + + if (!services.IsAdded()) + { + services.TryAddSingleton(); } if (!services.IsAdded()) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs index bfc5e62f7c..771e3acbde 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs +++ b/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 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 FindByDeviceCodeAsync(string deviceCode) + public async Task 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) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs index f40f8f0353..41c7e92574 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs +++ b/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 FindByDeviceCodeAsync( + string deviceCode, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs index 2168b2067e..516d400db6 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs +++ b/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(); + builder.Services.AddTransient(); return builder .AddClientStore() diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs index c722d3b99a..d5ac9ae815 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs +++ b/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 FindByDeviceCodeAsync( + string deviceCode, + CancellationToken cancellationToken = default) + { + return await DbSet + .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs index 8357c116f4..3ad3ecf104 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs +++ b/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 FindByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } \ No newline at end of file