Пример #1
0
        public async Task FromJson_GeneratedECKeyRoundTrips()
        {
            IPrivateKeyProvider privateKeyProvider = new EcDsaPrivateKeyProvider(
                new D2LSecurityTokenFactory(
                    DateTimeProvider.Instance,
                    TimeSpan.FromHours(1)
                    ),
                ECCurve.NamedCurves.nistP256
                );

            JsonWebKey expectedKey;

            using (D2LSecurityToken token = await privateKeyProvider.GetSigningCredentialsAsync().ConfigureAwait(false)) {
                expectedKey = token.ToJsonWebKey();
            }

            string expectedJson = JsonConvert.SerializeObject(expectedKey.ToJwkDto());

            JsonWebKey actualKey  = JsonWebKey.FromJson(expectedJson);
            string     actualJson = JsonConvert.SerializeObject(actualKey.ToJwkDto());

            Assert.AreEqual(expectedKey.Id, actualKey.Id);
            Assert.AreEqual(expectedKey.ExpiresAt.Value.ToUnixTimeSeconds(), actualKey.ExpiresAt.Value.ToUnixTimeSeconds());
            Assert.AreEqual(expectedJson, actualJson);
        }
        public async Task FromJson_GeneratedECKeyRoundTrips()
        {
            IPrivateKeyProvider privateKeyProvider = new EcDsaPrivateKeyProvider(
                new D2LSecurityTokenFactory(
                    DateTimeProvider.Instance,
                    TimeSpan.FromHours(1)
                    ),
                CngAlgorithm.ECDsaP256
                );

            JsonWebKey expectedKey;

            using (D2LSecurityToken token = await privateKeyProvider.GetSigningCredentialsAsync().SafeAsync()) {
                expectedKey = token.ToJsonWebKey();
            }

            string expectedJson = JsonConvert.SerializeObject(expectedKey.ToJwkDto());

            JsonWebKey actualKey  = JsonWebKey.FromJson(expectedJson);
            string     actualJson = JsonConvert.SerializeObject(actualKey.ToJwkDto());

            Assert.AreEqual(expectedKey.Id, actualKey.Id);
            Assert.AreEqual(( long )expectedKey.ExpiresAt.Value.TimeSinceUnixEpoch().TotalSeconds, ( long )actualKey.ExpiresAt.Value.TimeSinceUnixEpoch().TotalSeconds);
            Assert.AreEqual(expectedJson, actualJson);
        }