public void VerificationFailsWhenHavingCircularMacaroonReferences() { // Arrange // - Create primary macaroon Macaroon m = new Macaroon(Location, Secret, Identifier); // - Add third party caveat (1) string caveat_key1 = "4; guaranteed random by a fair toss of the dice"; string identifier1 = "this was how we remind auth of key/pred"; m.AddThirdPartyCaveat("http://auth.mybank/", caveat_key1, identifier1); // - Add third party caveat (2) string caveat_key2 = "random key 2"; string identifier2 = "identifier 2"; m.AddThirdPartyCaveat("http://auth.government/", caveat_key2, identifier2); // - Create discharge macaroon (1) with reference to (2) Macaroon d1 = new Macaroon("http://auth.mybank/", caveat_key1, identifier1); d1.AddThirdPartyCaveat("http://auth.government/", caveat_key2, identifier2); Macaroon dp1 = m.PrepareForRequest(d1); // - Create discharge macaroon (2) with reference to (1) Macaroon d2 = new Macaroon("http://auth.mybank/", caveat_key2, identifier2); d2.AddThirdPartyCaveat("http://auth.government/", caveat_key1, identifier1); Macaroon dp2 = m.PrepareForRequest(d2); Verifier v = new Verifier(); // Act VerificationResult result = m.Verify(v, Secret, new List <Macaroon> { dp1, dp2 }); // Assert Assert.IsFalse(result.Success); Assert.AreEqual(2, result.Messages.Count); StringAssert.Contains("circular", result.Messages[0]); }
static void TestCreateAndDecrypt() { //Macaroon.Crypto = new NoEncryption(); Stopwatch w1 = new Stopwatch(); Stopwatch w2 = new Stopwatch(); Stopwatch w3 = new Stopwatch(); Stopwatch w4 = new Stopwatch(); w1.Start(); for (int i = 0; i < 2000; ++i) { w2.Start(); //Macaroon m = new Macaroon(Location2, Secret2, Identifier2); Macaroon m = new Macaroon(LocationBytes, SecretBytes, IdentifierBytes); m.AddFirstPartyCaveat("account = 3735928559"); string caveat_key = "4; guaranteed random by a fair toss of the dice"; string identifier = "this was how we remind auth of key/pred"; m.AddThirdPartyCaveat("http://auth.mybank/", caveat_key, identifier); //m.AddFirstPartyCaveat("account = 3735928559"); w2.Stop(); Macaroon d = new Macaroon("http://auth.mybank/", caveat_key, identifier); //d.AddFirstPartyCaveat("time < 2015-01-01T00:00"); w3.Start(); Macaroon dp = m.PrepareForRequest(d); w3.Stop(); w4.Start(); Verifier v = new Verifier(); v.SatisfyExact("account = 3735928559"); //v.SatisfyGeneral(TimeVerifier); VerificationResult result = m.Verify(v, Secret2, new List <Macaroon> { dp }); w4.Stop(); if (!result.Success) { throw new InvalidOperationException(); } } //Console.WriteLine(result.Success); w1.Stop(); Console.WriteLine("Total: " + w1.Elapsed); Console.WriteLine("Create: " + w2.Elapsed); Console.WriteLine("Prepare: " + w3.Elapsed); Console.WriteLine("Verify: " + w4.Elapsed); }
public void CanPrepareForRequest() { // Arrange Macaroon m = new Macaroon(Location2, Secret2, Identifier2); m.AddFirstPartyCaveat("account = 3735928559"); string caveat_key = "4; guaranteed random by a fair toss of the dice"; string identifier = "this was how we remind auth of key/pred"; m.AddThirdPartyCaveat("http://auth.mybank/", caveat_key, identifier); Macaroon d = new Macaroon("http://auth.mybank/", caveat_key, identifier); d.AddFirstPartyCaveat("time < 2015-01-01T00:00"); // Act Macaroon dp = m.PrepareForRequest(d); // Assert Assert.Equal("82A80681F9F32D419AF12F6A71787A1BAC3AB199DF934ED950DDF20C25AC8C65", d.Signature.ToString().ToUpperInvariant()); Assert.Equal("2EB01D0DD2B4475330739140188648CF25DDA0425EA9F661F1574CA0A9EAC54E", dp.Signature.ToString().ToUpperInvariant()); }
static void Main(string[] args) { Macaroon.Crypto = new SecretBoxCryptoAlgorithm(false); string secret = "this is our super secret key; only we should know it"; string pubid = "we used our secret key"; string location = "http://mybank/"; Macaroon m = new Macaroon(location, secret, pubid); Console.WriteLine(m.Identifier); Console.WriteLine(m.Location); Console.WriteLine(m.Signature); Console.WriteLine(m.Serialize()); Console.WriteLine(m.Inspect()); m.AddFirstPartyCaveat("account = 3735928559"); Console.WriteLine(m.Inspect()); m.AddFirstPartyCaveat("time < 2015-01-01T00:00"); Console.WriteLine(m.Signature); m.AddFirstPartyCaveat("email = [email protected]"); Console.WriteLine(m.Signature); Console.WriteLine(m.Inspect()); string msg = m.Serialize(); // Send to bank // Receive again m = Macaroon.Deserialize(msg); Console.WriteLine(m.Inspect()); Verifier v = new Verifier(); var result = v.Verify(m, secret); Console.WriteLine("Success: {0}", result.Success); v.SatisfyExact("account = 3735928559"); v.SatisfyExact("email = [email protected]"); v.SatisfyExact("IP = 127.0.0.1"); v.SatisfyExact("browser = Chrome"); v.SatisfyExact("action = deposit"); Console.WriteLine(CheckTime(new Packet("time < 2015-01-01T00:00"))); Console.WriteLine(CheckTime(new Packet("time < 2014-01-01T00:00"))); Console.WriteLine(CheckTime(new Packet("account = 3735928559"))); v.SatisfyGeneral(CheckTime); result = v.Verify(m, secret); Console.WriteLine("Success: {0}", result.Success); Macaroon n = new Macaroon(m).AddFirstPartyCaveat("action = deposit"); result = v.Verify(n, secret); Console.WriteLine("Success: {0}", result.Success); n = new Macaroon(m).AddFirstPartyCaveat("OS = Windows XP"); result = v.Verify(n, secret); Console.WriteLine("Success: {0}", result.Success); n = new Macaroon(m).AddFirstPartyCaveat("time < 2014-01-01T00:00"); result = v.Verify(n, secret); Console.WriteLine("Success: {0}", result.Success); result = v.Verify(m, "this is not the secret we were looking for"); Console.WriteLine("Success: {0}", result.Success); n = Macaroon.Deserialize("MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVzZWQgb3VyIHNlY3JldCBrZXkKMDAxZGNpZCBhY2NvdW50ID0gMzczNTkyODU1OQowMDIwY2lkIHRpbWUgPCAyMDE1LTAxLTAxVDAwOjAwCjAwMjJjaWQgZW1haWwgPSBhbGljZUBleGFtcGxlLm9yZwowMDJmc2lnbmF0dXJlID8f19FL+bkC9p/aoMmIecC7GxdOcLVyUnrv6lJMM7NSCg=="); Console.WriteLine(n.Inspect()); Console.WriteLine("n.Signature == m.Signature: {0}", m.Signature == n.Signature); result = v.Verify(n, secret); Console.WriteLine("Success: {0}", result.Success); string location2 = "http://mybank/"; string secret2 = "this is a different super-secret key; never use the same secret twice"; string pubid2 = "we used our other secret key"; m = new Macaroon(location2, secret2, pubid2); m.AddFirstPartyCaveat("account = 3735928559"); Console.WriteLine(m.Inspect()); string caveat_key = "4; guaranteed random by a fair toss of the dice"; // string predicate = "user = Alice"; // send_to_auth(caveat_key, predicate) // identifier = recv_from_auth() string identifier = "this was how we remind auth of key/pred"; m.AddThirdPartyCaveat("http://auth.mybank/", caveat_key, identifier); Console.WriteLine(m.Inspect()); var caveats = m.ThirdPartyCaveats; Macaroon d = new Macaroon("http://auth.mybank/", caveat_key, identifier); d.AddFirstPartyCaveat("time < 2015-01-01T00:00"); Console.WriteLine(d.Inspect()); Macaroon dp = m.PrepareForRequest(d); Console.WriteLine(d.Signature); Console.WriteLine(dp.Signature); result = v.Verify(m, secret2, new List <Macaroon> { dp }); Console.WriteLine("Success: {0}", result.Success); result = v.Verify(m, secret2, new List <Macaroon> { d }); Console.WriteLine("Success: {0}", result.Success); Console.WriteLine(Macaroon.MACAROON_SUGGESTED_SECRET_LENGTH); byte[] randomSecret = new byte[Macaroon.MACAROON_SUGGESTED_SECRET_LENGTH]; using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) rng.GetBytes(randomSecret); Packet key = new Packet(randomSecret, DataEncoding.Hex); Console.WriteLine(key); m = new Macaroon(new Packet(location), key, new Packet(pubid)); Console.WriteLine(m.Inspect()); }