Beispiel #1
0
        public void Test_Issuer()
        {
            CWT cwt = new CWT();

            Assert.AreEqual(false, cwt.HasClaim(ClaimId.Issuer));
            Assert.AreEqual(false, cwt.HasClaim("iss"));
            cwt.Issuer = "Audience1";
            Assert.AreEqual(true, cwt.HasClaim(ClaimId.Issuer));
            Assert.AreEqual(true, cwt.HasClaim("iss"));

            Assert.AreEqual("Audience1", cwt.GetClaim(ClaimId.Issuer).AsString());
            Assert.AreEqual("Audience1", cwt.GetClaim("iss").AsString());
            Assert.AreEqual("Audience1", cwt[ClaimId.Issuer].AsString());

            CwtException e = Assert.Throws <CwtException>(() =>
                                                          cwt.SetClaim(ClaimId.Issuer, CBORObject.FromObject(1)));

            cwt.SetClaim("iss", "TestValue");
            Assert.AreEqual(cwt.Issuer, "TestValue");
        }
Beispiel #2
0
        public void Test_Audience()
        {
            CWT cwt = new CWT();

            Assert.AreEqual(false, cwt.HasClaim(ClaimId.Audience));
            Assert.AreEqual(false, cwt.HasClaim("aud"));
            cwt.Audience = "Audience1";
            Assert.AreEqual(true, cwt.HasClaim(ClaimId.Audience));
            Assert.AreEqual(true, cwt.HasClaim("aud"));

            Assert.AreEqual("Audience1", cwt.GetClaim(ClaimId.Audience).AsString());
            Assert.AreEqual("Audience1", cwt.GetClaim("aud").AsString());
            Assert.AreEqual("Audience1", cwt[ClaimId.Audience].AsString());

            CwtException e = Assert.Throws <CwtException>(() =>
                                                          cwt.SetClaim(ClaimId.Audience, CBORObject.FromObject(1)));

            cwt.SetClaim("aud", "TestValue");
            Assert.AreEqual(cwt.Audience, "TestValue");
        }
Beispiel #3
0
        void ProcessFile(FileInfo testCase)
        {
            if (testCase.Extension != ".json")
            {
                return;
            }
            if (testCase.Name[0] == '.')
            {
                return;
            }

            Debug.Print($"Working on file {testCase}");
            Console.WriteLine("Working on file '" + testCase + "'");

            string     inputText  = testCase.OpenText().ReadToEnd();
            CBORObject test       = CBORObject.FromJSONString(inputText);
            KeySet     decodeKeys = new KeySet();
            KeySet     signKeys   = new KeySet();

            CBORObject input = test["input"];

            CWT cwt = new CWT();

            if (input.ContainsKey("encrypted"))
            {
                OneKey key = LoadKey(input["encrypted"]["key"]);
                cwt.EncryptionKey = key;
                decodeKeys.AddKey(key);
            }

            if (input.ContainsKey("mac0"))
            {
                OneKey key = LoadKey(input["mac0"]["key"]);
                cwt.MacKey = key;
                decodeKeys.AddKey(key);
            }

            if (input.ContainsKey("sign0"))
            {
                OneKey key = LoadKey(input["sign0"]["key"]);
                cwt.SigningKey = key;
                signKeys.AddKey(key.PublicKey());
            }

            CWT cwt2 = CWT.Decode(FromHex(test["output"]["cbor"].AsString()), decodeKeys, signKeys);



            CBORObject token = input["token"];

            foreach (CBORObject key in token.Keys)
            {
                CBORObject value = token[key];
                CBORObject key2  = key;
                if (key.AsString().EndsWith("_hex"))
                {
                    value = CBORObject.FromObject(FromHex(value.AsString()));
                    key2  = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4));
                }

                cwt.SetClaim(key2, value);

                Assert.True(cwt2.HasClaim(key2), $"Missing Claim {key2}");
                Assert.AreEqual(value, cwt.GetClaim(key2));
            }

            byte[] foo = cwt.EncodeToBytes();

            cwt2 = CWT.Decode(foo, decodeKeys, signKeys);
            foreach (CBORObject key in token.Keys)
            {
                CBORObject value = token[key];
                CBORObject key2  = key;
                if (key.AsString().EndsWith("_hex"))
                {
                    value = CBORObject.FromObject(FromHex(value.AsString()));
                    key2  = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4));
                }

                Assert.True(cwt2.HasClaim(key2));
                Assert.AreEqual(value, cwt.GetClaim(key2));
            }
        }