public Task UpdateByUserCodeAsync(string userCode, is4Models.DeviceCode data)
        {
            var existing = _deviceFlowCodeService.GetSingle(x => x.UserCode == userCode);

            if (existing == null)
            {
                _logger.LogError("{userCode} not found in database", userCode);
                throw new InvalidOperationException("Could not update device code");
            }

            var entity = ToEntity(data, existing.DeviceCode, userCode);

            _logger.LogDebug("{userCode} found in database", userCode);

            existing.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value;
            existing.Data      = entity.Data;

            try
            {
                _deviceFlowCodeService.Update(entity);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogWarning("exception updating {userCode} user code in database: {error}", userCode, ex.Message);
            }

            return(Task.FromResult(0));
        }
Exemple #2
0
        public async Task DeviceFlowStore_RemoveByKeyNegativeTest()
        {
            var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger);

            Assert.IsNotNull(store);
            string userCode   = Guid.NewGuid().ToString("n");
            string deviceCode = Guid.NewGuid().ToString("n");

            Console.WriteLine($"userCode: {userCode}");
            Console.WriteLine($"deviceCode: {deviceCode}");

            DeviceCode deviceCodeData = CreateTestObject();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            await store.RemoveByDeviceCodeAsync(deviceCode);

            stopwatch.Stop();
            Console.WriteLine($"DeviceFlowStore.RemoveByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms");
        }
Exemple #3
0
        public async Task DeviceFlowStore_SaveUpdateGetByCodesTest()
        {
            var storageContext = Services.BuildServiceProvider().GetService <DeviceFlowStorageContext>();

            Assert.IsNotNull(storageContext);

            var store = new DeviceFlowStore(storageContext, _persistentGrantSerializer, _logger);

            Assert.IsNotNull(store);
            string userCode   = Guid.NewGuid().ToString("n");
            string deviceCode = Guid.NewGuid().ToString("n");

            Console.WriteLine($"userCode: {userCode}");
            Console.WriteLine($"deviceCode: {deviceCode}");

            DeviceCode deviceCodeData = CreateTestObject();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            await store.StoreDeviceAuthorizationAsync(deviceCode, userCode, deviceCodeData);

            stopwatch.Stop();
            Console.WriteLine($"DeviceFlowStore.StoreDeviceAuthorizationAsync({deviceCode},{userCode}): {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Reset();
            stopwatch.Start();
            string oldClientId = deviceCodeData.ClientId;
            string newClientId = Guid.NewGuid().ToString("n");

            deviceCodeData.ClientId = newClientId;

            await store.UpdateByUserCodeAsync(userCode, deviceCodeData);

            stopwatch.Stop();
            Console.WriteLine($"DeviceFlowStore.UpdateByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Reset();
            stopwatch.Start();
            var findByDeviceCode = await store.FindByDeviceCodeAsync(deviceCode);

            stopwatch.Stop();
            Console.WriteLine($"DeviceFlowStore.FindByDeviceCodeAsync({deviceCode}): {stopwatch.ElapsedMilliseconds} ms");
            AssertDeviceCodesEqual(deviceCodeData, findByDeviceCode);
            Assert.AreEqual <string>(newClientId, findByDeviceCode.ClientId);
            Assert.AreNotEqual <string>(oldClientId, findByDeviceCode.ClientId);

            stopwatch.Reset();

            stopwatch.Start();
            var findByUserCode = await store.FindByUserCodeAsync(userCode);

            stopwatch.Stop();
            Console.WriteLine($"DeviceFlowStore.FindByUserCodeAsync({userCode}): {stopwatch.ElapsedMilliseconds} ms");
            AssertDeviceCodesEqual(deviceCodeData, findByUserCode);
            Assert.AreEqual <string>(newClientId, findByUserCode.ClientId);
            Assert.AreNotEqual <string>(oldClientId, findByUserCode.ClientId);

            AssertDeviceCodesEqual(findByDeviceCode, findByUserCode);
        }
Exemple #4
0
        public void AssertDeviceCodesEqual(DeviceCode expected, DeviceCode actual)
        {
            Assert.IsNotNull(expected);
            Assert.IsNotNull(actual);

            Assert.AreEqual <string>(_persistentGrantSerializer.Serialize(expected),
                                     _persistentGrantSerializer.Serialize(actual));
        }
        private is4Entity.DeviceFlowCode ToEntity(is4Models.DeviceCode model, string deviceCode, string userCode)
        {
            if (model == null || deviceCode == null || userCode == null)
            {
                return(null);
            }

            return(new is4Entity.DeviceFlowCode
            {
                DeviceCode = deviceCode,
                UserCode = userCode,
                ClientId = model.ClientId,
                SubjectId = model.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
                CreationTime = model.CreationTime,
                Expiration = model.CreationTime.AddSeconds(model.Lifetime),
                Data = _serializer.Serialize(model)
            });
        }
        public async Task UpdateByUserCodeAsync(string userCode, Models.DeviceCode data)
        {
            userCode = userCode ?? throw new ArgumentNullException(nameof(userCode));
            data     = data ?? throw new ArgumentNullException(nameof(data));

            var entity = await _context.DeviceCodes
                         .FirstOrDefaultAsync(d => d.UserCode == userCode)
                         .ConfigureAwait(false);

            if (entity == null)
            {
                throw new InvalidOperationException($"Device code for {userCode} not found");
            }

            entity.Data       = _serializer.Serialize(data);
            entity.Expiration = data.CreationTime.AddSeconds(data.Lifetime);
            entity.SubjectId  = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value;

            await _context.SaveChangesAsync().ConfigureAwait(false);
        }
        public async Task UpdateByUserCodeAsync(string userCode, Models.DeviceCode data)
        {
            userCode = userCode ?? throw new ArgumentNullException(nameof(userCode));
            data     = data ?? throw new ArgumentNullException(nameof(data));

            var response = await _store.GetAsync(new PageRequest
            {
                Filter = $"{nameof(DeviceCode.UserCode)} eq '{userCode}'"
            }).ConfigureAwait(false);

            if (response.Items.Any())
            {
                var entity = response.Items.First();
                entity.Data       = _serializer.Serialize(data);
                entity.Expiration = data.CreationTime.AddSeconds(data.Lifetime);
                entity.SubjectId  = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value;
                await _store.UpdateAsync(entity).ConfigureAwait(false);

                return;
            }

            throw new InvalidOperationException($"Device code for {userCode} not found");
        }
        public async Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, Models.DeviceCode data)
        {
            deviceCode = deviceCode ?? throw new ArgumentNullException(nameof(deviceCode));
            userCode   = userCode ?? throw new ArgumentNullException(nameof(userCode));
            data       = data ?? throw new ArgumentNullException(nameof(data));

            var entity = new DeviceCode
            {
                Id         = Guid.NewGuid().ToString(),
                Code       = deviceCode,
                UserCode   = userCode,
                Data       = _serializer.Serialize(data),
                ClientId   = data.ClientId,
                SubjectId  = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
                Expiration = data.CreationTime.AddSeconds(data.Lifetime),
            };

            await _context.DeviceCodes.AddAsync(entity).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);
        }
        public Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, is4Models.DeviceCode data)
        {
            _deviceFlowCodeService.Add(ToEntity(data, deviceCode, userCode));

            return(Task.FromResult(0));
        }
        public Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, Models.DeviceCode data)
        {
            deviceCode = deviceCode ?? throw new ArgumentNullException(nameof(deviceCode));
            userCode   = userCode ?? throw new ArgumentNullException(nameof(userCode));
            data       = data ?? throw new ArgumentNullException(nameof(data));

            var entity = new DeviceCode
            {
                Code       = deviceCode,
                UserCode   = userCode,
                Data       = _serializer.Serialize(data),
                ClientId   = data.ClientId,
                SubjectId  = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
                Expiration = data.CreationTime.AddSeconds(data.Lifetime),
            };

            return(_store.CreateAsync(entity));
        }