Beispiel #1
0
 public PacketReader(Stream s, SerializationOptions options)
 {
     InputStream = s;
     Reader      = new BinaryReader(s);
     Options     = options;
 }
Beispiel #2
0
 /// <summary>
 /// Deserialize a byte array into a macaroon.
 /// </summary>
 /// <param name="data">Input bytes.</param>
 /// <param name="options">Serialization options.</param>
 /// <returns>Deserialized macaroon.</returns>
 public static Macaroon Deserialize(byte[] data, SerializationOptions options = null)
 {
     using (MemoryStream m = new MemoryStream(data))
         return(Deserialize(m, options));
 }
Beispiel #3
0
        /// <summary>
        /// Deserialize macaroon from a stream.
        /// </summary>
        /// <param name="s">Input stream.</param>
        /// <param name="options">Serialization options.</param>
        /// <returns>Deserialized macaroon.</returns>
        public static Macaroon Deserialize(Stream s, SerializationOptions options = null)
        {
            if (options == null)
            {
                options = SerializationOptions.Default;
            }

            using (PacketReader r = new PacketReader(s, options))
            {
                Packet location   = r.ReadLocationPacket();
                Packet identifier = r.ReadIdentifierPacket();
                Packet signature  = null;

                // Start by reading the first packet
                KeyValuePair <byte[], byte[]> packet = r.ReadKVPacket();

                List <Caveat> caveats = new List <Caveat>();
                while (true)
                {
                    bool handled = false;

                    if (Utility.ByteArrayEquals(PacketSerializerBase.CIdID, packet.Key))
                    {
                        Packet cid = new Packet(packet.Value, options.CaveatIdentifierEncoding);
                        Packet vid = null;
                        Packet cl  = null;

                        // Done with this package, now read next one
                        packet = r.ReadKVPacket();

                        if (Utility.ByteArrayEquals(PacketSerializerBase.VIdID, packet.Key))
                        {
                            vid = new Packet(packet.Value, DataEncoding.Base64UrlSafe);

                            // Done with this package, now read next one
                            packet = r.ReadKVPacket();
                        }

                        if (Utility.ByteArrayEquals(PacketSerializerBase.ClID, packet.Key))
                        {
                            cl = new Packet(packet.Value, DataEncoding.UTF8);

                            // Done with this package, now read next one
                            packet = r.ReadKVPacket();
                        }

                        Caveat c = new Caveat(cid, vid, cl);
                        caveats.Add(c);

                        handled = true;
                    }

                    if (Utility.ByteArrayEquals(PacketSerializerBase.SignatureID, packet.Key))
                    {
                        signature = new Packet(packet.Value, DataEncoding.Hex);

                        // Done with this package - don't read more packages since signature should be the last one
                        break;
                    }

                    if (!handled)
                    {
                        throw new InvalidDataException("Unexpected data in input (did not find Cid or signature).");
                    }
                }

                if (signature == null)
                {
                    throw new InvalidDataException("Missing signature packet");
                }

                return(new Macaroon()
                {
                    Location = location,
                    Identifier = identifier,
                    Signature = signature,
                    CaveatsList = caveats
                });
            }
        }
Beispiel #4
0
 /// <summary>
 /// Deserialize a string into a macaroon.
 /// </summary>
 /// <param name="s">Input string.</param>
 /// <param name="options">Serialization options.</param>
 /// <returns>Deserialized macaroon.</returns>
 public static Macaroon Deserialize(string s, SerializationOptions options = null)
 {
     Condition.Requires(s, "s").IsNotNullOrEmpty();
     byte[] data = Utility.FromBase64UrlSafe(s);
     return(Deserialize(data));
 }