예제 #1
0
        public async Task UpdateByUserCodeAsync_WhenDeviceCodeAuthorized_ExpectSubjectAndDataUpdated()
        {
            var testDeviceCode = $"device_{Guid.NewGuid().ToString()}";
            var testUserCode   = $"user_{Guid.NewGuid().ToString()}";

            var expectedSubject        = $"sub_{Guid.NewGuid().ToString()}";
            var unauthorizedDeviceCode = new DeviceCode
            {
                ClientId        = "device_flow",
                RequestedScopes = new[] { "openid", "api1" },
                CreationTime    = new DateTime(2018, 10, 19, 16, 14, 29),
                Lifetime        = 300,
                IsOpenId        = true
            };

            var repo   = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>();
            var entity = new Storage.Entities.DeviceFlowCodes
            {
                DeviceCode   = testDeviceCode,
                UserCode     = testUserCode,
                ClientId     = unauthorizedDeviceCode.ClientId,
                CreationTime = unauthorizedDeviceCode.CreationTime,
                Expiration   = unauthorizedDeviceCode.CreationTime.AddSeconds(unauthorizedDeviceCode.Lifetime),
                Data         = serializer.Serialize(unauthorizedDeviceCode)
            };

            repo.Insert(entity);


            var authorizedDeviceCode = new DeviceCode
            {
                ClientId         = unauthorizedDeviceCode.ClientId,
                RequestedScopes  = unauthorizedDeviceCode.RequestedScopes,
                AuthorizedScopes = unauthorizedDeviceCode.RequestedScopes,
                Subject          = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> {
                    new Claim(JwtClaimTypes.Subject, expectedSubject)
                })),
                IsAuthorized = true,
                IsOpenId     = true,
                CreationTime = new DateTime(2018, 10, 19, 16, 14, 29),
                Lifetime     = 300
            };
            var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create());
            await store.UpdateByUserCodeAsync(testUserCode, authorizedDeviceCode);

            Storage.Entities.DeviceFlowCodes updatedCodes;
            updatedCodes = repo.Where(x => x.UserCode == testUserCode).First();

            // should be unchanged
            updatedCodes.DeviceCode.Should().Be(testDeviceCode);
            updatedCodes.ClientId.Should().Be(unauthorizedDeviceCode.ClientId);
            updatedCodes.CreationTime.Should().Be(unauthorizedDeviceCode.CreationTime);
            updatedCodes.Expiration.Should().Be(unauthorizedDeviceCode.CreationTime.AddSeconds(authorizedDeviceCode.Lifetime));

            // should be changed
            var parsedCode = serializer.Deserialize <DeviceCode>(updatedCodes.Data);

            parsedCode.Should().BeEquivalentTo(authorizedDeviceCode, assertionOptions => assertionOptions.Excluding(x => x.Subject));
            parsedCode.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull();
        }
예제 #2
0
        public async Task RemoveByDeviceCodeAsync_WhenDeviceCodeExists_ExpectDeviceCodeDeleted()
        {
            var testDeviceCode = $"device_{Guid.NewGuid().ToString()}";
            var testUserCode   = $"user_{Guid.NewGuid().ToString()}";

            var existingDeviceCode = new DeviceCode
            {
                ClientId        = "device_flow",
                RequestedScopes = new[] { "openid", "api1" },
                CreationTime    = new DateTime(2018, 10, 19, 16, 14, 29),
                Lifetime        = 300,
                IsOpenId        = true
            };

            var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>();

            var entity = new Storage.Entities.DeviceFlowCodes
            {
                DeviceCode   = testDeviceCode,
                UserCode     = testUserCode,
                ClientId     = existingDeviceCode.ClientId,
                CreationTime = existingDeviceCode.CreationTime,
                Expiration   = existingDeviceCode.CreationTime.AddSeconds(existingDeviceCode.Lifetime),
                Data         = serializer.Serialize(existingDeviceCode)
            };

            repo.Insert(entity);


            var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create());
            await store.RemoveByDeviceCodeAsync(testDeviceCode);


            repo.Select.Where(x => x.UserCode == testUserCode).First().Should().BeNull();
        }
예제 #3
0
        public async Task FindByUserCodeAsync_WhenUserCodeExists_ExpectDataRetrievedCorrectly()
        {
            var testDeviceCode = $"device_{Guid.NewGuid().ToString()}";
            var testUserCode   = $"user_{Guid.NewGuid().ToString()}";

            var expectedSubject        = $"sub_{Guid.NewGuid().ToString()}";
            var expectedDeviceCodeData = new DeviceCode
            {
                ClientId        = "device_flow",
                RequestedScopes = new[] { "openid", "api1" },
                CreationTime    = new DateTime(2018, 10, 19, 16, 14, 29),
                Lifetime        = 300,
                IsOpenId        = true,
                Subject         = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> {
                    new Claim(JwtClaimTypes.Subject, expectedSubject)
                }))
            };

            var repo = g.operationalDb.GetRepository <Storage.Entities.DeviceFlowCodes>();

            var entity = new Storage.Entities.DeviceFlowCodes
            {
                DeviceCode   = testDeviceCode,
                UserCode     = testUserCode,
                ClientId     = expectedDeviceCodeData.ClientId,
                SubjectId    = expectedDeviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value,
                CreationTime = expectedDeviceCodeData.CreationTime,
                Expiration   = expectedDeviceCodeData.CreationTime.AddSeconds(expectedDeviceCodeData.Lifetime),
                Data         = serializer.Serialize(expectedDeviceCodeData)
            };

            repo.Insert(entity);


            DeviceCode code;

            var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create());

            code = await store.FindByUserCodeAsync(testUserCode);


            code.Should().BeEquivalentTo(expectedDeviceCodeData,
                                         assertionOptions => assertionOptions.Excluding(x => x.Subject));

            code.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject && x.Value == expectedSubject).Should().NotBeNull();
        }
예제 #4
0
        public async Task StoreDeviceAuthorizationAsync_WhenUserCodeAlreadyExists_ExpectException()
        {
            var existingUserCode = $"user_{Guid.NewGuid().ToString()}";
            var deviceCodeData   = new DeviceCode
            {
                ClientId        = "device_flow",
                RequestedScopes = new[] { "openid", "api1" },
                CreationTime    = new DateTime(2018, 10, 19, 16, 14, 29),
                Lifetime        = 300,
                IsOpenId        = true,
                Subject         = new ClaimsPrincipal(new ClaimsIdentity(
                                                          new List <Claim> {
                    new Claim(JwtClaimTypes.Subject, $"sub_{Guid.NewGuid().ToString()}")
                }))
            };


            var entity = new Storage.Entities.DeviceFlowCodes
            {
                DeviceCode   = $"device_{Guid.NewGuid().ToString()}",
                UserCode     = existingUserCode,
                ClientId     = deviceCodeData.ClientId,
                SubjectId    = deviceCodeData.Subject.FindFirst(JwtClaimTypes.Subject).Value,
                CreationTime = deviceCodeData.CreationTime,
                Expiration   = deviceCodeData.CreationTime.AddSeconds(deviceCodeData.Lifetime),
                Data         = serializer.Serialize(deviceCodeData)
            };

            g.operationalDb.Insert(entity).ExecuteAffrows();


            var store = new DeviceFlowStore(g.operationalDb, new PersistentGrantSerializer(), FakeLogger <DeviceFlowStore> .Create());


            await Assert.ThrowsAsync <Exception>(() =>
                                                 store.StoreDeviceAuthorizationAsync($"device_{Guid.NewGuid().ToString()}", existingUserCode, deviceCodeData));
        }