Beispiel #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);
        }
Beispiel #3
0
        public void FromJson_ParsesStaticEcKeyWithoutExp(string jwk)
        {
            JsonWebKey key = JsonWebKey.FromJson(jwk);

            Assert.AreEqual("fae33c85-e421-40f5-bebb-8ec8ab778be4", key.Id);
            Assert.IsFalse(key.ExpiresAt.HasValue);

            Assert.DoesNotThrow(() => key.ToSecurityToken());
        }
Beispiel #4
0
        public void FromJson_ParsesStaticRsaKeyWithoutExp()
        {
            string id      = Guid.NewGuid().ToString();
            string example =
                @"{""kid"":""" + id + @""",""kty"":""RSA"",""use"":""sig"",""n"":""piXmF9_L0UO4K5APzHqiOYl_KtVXAgPlVHhUopPztaW_JRh2k9MDeupIA1cAF9S_r5qRBWcA1QaP0nlGalw3jm_fSHvtUYYhwUhF9X6I19VRmv_BX9Ne2budt5dafI9DbNs2Ltq0X_yfM1dUL81vaR0rz7jYaQ5bF2CRQHVCcIhWkik85PG5c1yK__As842WqogBpW8-zsEoB6s53FNpDG37_HsZAAngATmTY1At4O7jC6p-c0KVPDf25oLVMOWQubyVgCE9FlsVxprHWqsXenlnHEmhZfEbFB_5KB6hj2yV77jhvLRslNvyKflFBs6AGCiczNDzmoXH2GV3FAVLFQ"",""e"":""AQAB""}";

            JsonWebKey key = JsonWebKey.FromJson(example);

            Assert.AreEqual(id, key.Id);
            Assert.IsFalse(key.ExpiresAt.HasValue);
        }